Skip to content

Commit

Permalink
tools: Add --test to compile-{keymap,compose}
Browse files Browse the repository at this point in the history
The new flag `--test` enables to only test if compilation succeeds,
without printing the corresponding keymap or Compose file.

This is useful for quick feedback and to speedup some tests suites, e.g.
for `xkeyboard-config`.
  • Loading branch information
wismill committed Nov 14, 2024
1 parent a7b84be commit 54f068e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changes/tools/+test-flag.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added `--test` option to `compile-keymap` and `compile-compose`, to enable testing
compilation without printing the resulting file.
17 changes: 15 additions & 2 deletions tools/compile-compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void
usage(FILE *fp, char *progname)
{
fprintf(fp,
"Usage: %s [--help] [--file FILE] [--locale LOCALE]\n",
"Usage: %s [--help] [--file FILE] [--locale LOCALE] [--test]\n",
progname);
fprintf(fp,
"\n"
Expand All @@ -50,7 +50,9 @@ usage(FILE *fp, char *progname)
" Specify a Compose file to load\n"
" --locale LOCALE\n"
" Specify the locale directly, instead of relying on the environment variables\n"
" LC_ALL, LC_TYPE and LANG.\n");
" LC_ALL, LC_TYPE and LANG.\n"
" --test\n"
" Test compilation but do not print the Compose file.\n");
}

int
Expand All @@ -62,14 +64,17 @@ main(int argc, char *argv[])
const char *locale = NULL;
const char *path = NULL;
enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1;
bool test = false;
enum options {
OPT_FILE,
OPT_LOCALE,
OPT_TEST,
};
static struct option opts[] = {
{"help", no_argument, 0, 'h'},
{"file", required_argument, 0, OPT_FILE},
{"locale", required_argument, 0, OPT_LOCALE},
{"test", no_argument, 0, OPT_TEST},
{0, 0, 0, 0},
};

Expand All @@ -95,6 +100,9 @@ main(int argc, char *argv[])
case OPT_LOCALE:
locale = optarg;
break;
case OPT_TEST:
test = true;
break;
case 'h':
usage(stdout, argv[0]);
return EXIT_SUCCESS;
Expand Down Expand Up @@ -141,6 +149,11 @@ main(int argc, char *argv[])
}
}

if (test) {
ret = EXIT_SUCCESS;
goto out;
}

ret = xkb_compose_table_dump(stdout, compose_table)
? EXIT_SUCCESS
: EXIT_FAILURE;
Expand Down
18 changes: 18 additions & 0 deletions tools/compile-keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static enum output_format {
} output_format = FORMAT_KEYMAP;
static const char *includes[64];
static size_t num_includes = 0;
static bool test = false;

static void
usage(char **argv)
Expand All @@ -62,6 +63,8 @@ usage(char **argv)
" Print this help and exit\n"
" --verbose\n"
" Enable verbose debugging output\n"
" --test\n"
" Test compilation but do not print the keymap.\n"
#if ENABLE_PRIVATE_APIS
" --kccgst\n"
" Print a keymap which only includes the KcCGST component names instead of the full keymap\n"
Expand Down Expand Up @@ -107,6 +110,7 @@ parse_options(int argc, char **argv, struct xkb_rule_names *names)
{
enum options {
OPT_VERBOSE,
OPT_TEST,
OPT_KCCGST,
OPT_RMLVO,
OPT_FROM_XKB,
Expand All @@ -121,6 +125,7 @@ parse_options(int argc, char **argv, struct xkb_rule_names *names)
static struct option opts[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, OPT_VERBOSE},
{"test", no_argument, 0, OPT_TEST},
#if ENABLE_PRIVATE_APIS
{"kccgst", no_argument, 0, OPT_KCCGST},
#endif
Expand Down Expand Up @@ -150,6 +155,9 @@ parse_options(int argc, char **argv, struct xkb_rule_names *names)
case OPT_VERBOSE:
verbose = true;
break;
case OPT_TEST:
test = true;
break;
case OPT_KCCGST:
output_format = FORMAT_KCCGST;
break;
Expand Down Expand Up @@ -216,6 +224,8 @@ print_kccgst(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)

if (!xkb_components_from_rules(ctx, rmlvo, &kccgst, NULL))
return false;
if (test)
goto out;

printf("xkb_keymap {\n"
" xkb_keycodes { include \"%s\" };\n"
Expand All @@ -224,6 +234,7 @@ print_kccgst(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
" xkb_symbols { include \"%s\" };\n"
"};\n",
kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
out:
free(kccgst.keycodes);
free(kccgst.types);
free(kccgst.compat);
Expand All @@ -244,10 +255,14 @@ print_keymap(struct xkb_context *ctx, const struct xkb_rule_names *rmlvo)
if (keymap == NULL)
return false;

if (test)
goto out;

char *buf = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
printf("%s\n", buf);
free(buf);

out:
xkb_keymap_unref(keymap);
return true;
}
Expand Down Expand Up @@ -291,6 +306,9 @@ print_keymap_from_file(struct xkb_context *ctx)
if (!keymap) {
fprintf(stderr, "Couldn't create xkb keymap\n");
goto out;
} else if (test) {
success = true;
goto out;
}

keymap_string = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1);
Expand Down
3 changes: 3 additions & 0 deletions tools/xkbcli-compile-compose.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Specify a Compose file to load
.It Fl \-locale Ar LOCALE
Specify the locale directly, instead of relying on the environment variables
LC_ALL, LC_TYPE and LANG.
.
.It Fl \-test
Test compilation but do not print the Compose file
.El
.
.Sh SEE ALSO
Expand Down
3 changes: 3 additions & 0 deletions tools/xkbcli-compile-keymap.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Print help and exit
.It Fl \-verbose
Enable verbose debugging output
.
.It Fl \-test
Test compilation but do not print the keymap
.
.It Fl \-rmlvo
Print the full RMLVO with the defaults filled in for missing elements
.
Expand Down

0 comments on commit 54f068e

Please sign in to comment.