tests: Use gjs_context_eval_file to evaluate test scripts

GJS provides utilities to run scripts given a filename, using those
utilities we can remove most of the logic implementing file loading.

Additionally the REPL logic does not make much sense for a test
runner and is unused, so remove it.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2822>
This commit is contained in:
Evan Welsh 2023-07-09 22:24:48 -07:00
parent 011ac6f83c
commit b798efcc1d

View File

@ -36,13 +36,6 @@
#include "shell-global.h" #include "shell-global.h"
#include "shell-global-private.h" #include "shell-global-private.h"
static char *command = NULL;
static GOptionEntry entries[] = {
{ "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
{ NULL }
};
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -50,10 +43,8 @@ main(int argc, char **argv)
GError *error = NULL; GError *error = NULL;
ShellGlobal *global; ShellGlobal *global;
GjsContext *js_context; GjsContext *js_context;
char *script;
const char *filename; const char *filename;
char *title; char *title;
gsize len;
int code; int code;
context = g_option_context_new (NULL); context = g_option_context_new (NULL);
@ -61,7 +52,6 @@ main(int argc, char **argv)
/* pass unknown through to the JS script */ /* pass unknown through to the JS script */
g_option_context_set_ignore_unknown_options (context, TRUE); g_option_context_set_ignore_unknown_options (context, TRUE);
g_option_context_add_main_entries (context, entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) if (!g_option_context_parse (context, &argc, &argv, &error))
g_error ("option parsing failed: %s", error->message); g_error ("option parsing failed: %s", error->message);
@ -79,32 +69,21 @@ main(int argc, char **argv)
exit (1); exit (1);
} }
if (command != NULL) { if (argc < 2) {
script = command; g_printerr ("Missing filename");
len = strlen (script); exit(1);
filename = "<command line>";
} else if (argc <= 1) {
script = g_strdup ("const Console = imports.console; Console.interact();");
len = strlen (script);
filename = "<stdin>";
} else /*if (argc >= 2)*/ {
error = NULL;
if (!g_file_get_contents (argv[1], &script, &len, &error)) {
g_printerr ("%s\n", error->message);
exit (1);
}
filename = argv[1];
} }
filename = argv[1];
title = g_filename_display_basename (filename); title = g_filename_display_basename (filename);
g_set_prgname (title); g_set_prgname (title);
g_free (title); g_free (title);
/* evaluate the script */
error = NULL; error = NULL;
if (!gjs_context_eval (js_context, script, len,
filename, &code, &error)) { /* evaluate the script */
g_free (script); bool success = gjs_context_eval_file(js_context, filename, &code, &error);
if (!success) {
g_printerr ("%s\n", error->message); g_printerr ("%s\n", error->message);
exit (1); exit (1);
} }
@ -113,6 +92,5 @@ main(int argc, char **argv)
gjs_context_gc (js_context); gjs_context_gc (js_context);
g_object_unref (js_context); g_object_unref (js_context);
g_free (script);
exit (code); exit (code);
} }