HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-options.c

71,835 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 867c76a8c4fbe3dea6292f0c77e847b13b25fe36

Lines 1001–1604 of 1,604

                }
        }
 
        ASSERT_EQ(i, n_entries);
 
        char **args = option_parser_get_args(&opts);
        ASSERT_TRUE(strv_equal(args, remaining));
        ASSERT_STREQ(argv[0], saved_argv0);
        ASSERT_NULL(opts.opt);
        ASSERT_NULL(opts.arg);
}
 
TEST(option_macros) {
        /* OPTION: long form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help"),
                              (Entry[]) {
                                      { "help" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION: short form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-h"),
                              (Entry[]) {
                                      { "help" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_LONG: only accessible via long form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--version"),
                              (Entry[]) {
                                      { "version" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_SHORT: only accessible via short form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-v"),
                              (Entry[]) {
                                      { .short_code = 'v' },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION with required arg: long --required=ARG */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--required=val1"),
                              (Entry[]) {
                                      { "required", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION with required arg: long --required ARG */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--required", "val1"),
                              (Entry[]) {
                                      { "required", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION with required arg: short -r ARG */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-r", "val1"),
                              (Entry[]) {
                                      { "required", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION with required arg: short -rARG */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-rval1"),
                              (Entry[]) {
                                      { "required", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_FULL with OPTION_OPTIONAL_ARG: long with = */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--optional=val1"),
                              (Entry[]) {
                                      { "optional", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_FULL with OPTION_OPTIONAL_ARG: long without = doesn't consume next */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--optional", "pos1"),
                              (Entry[]) {
                                      { "optional", NULL },
                                      {}
                              },
                              STRV_MAKE("pos1"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_FULL with OPTION_OPTIONAL_ARG: short inline */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-oval1"),
                              (Entry[]) {
                                      { "optional", "val1" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_FULL with OPTION_OPTIONAL_ARG: short without inline */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-o", "pos1"),
                              (Entry[]) {
                                      { "optional", NULL },
                                      {}
                              },
                              STRV_MAKE("pos1"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_FULL with OPTION_STOPS_PARSING: stops further option parsing */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--exec",
                                        "--help",
                                        "--version"),
                              (Entry[]) {
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("--help",
                                        "--version"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING: options before are still parsed */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "--exec",
                                        "-h",
                                        "--debug"),
                              (Entry[]) {
                                      { "help" },
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("-h",
                                        "--debug"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING with "--": "--" after exec is still consumed */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--exec",
                                        "--",
                                        "--help"),
                              (Entry[]) {
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("--help"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING with "--": "--" before exec takes precedence */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--",
                                        "--exec",
                                        "--help"),
                              (Entry[]) {
                                      {}
                              },
                              STRV_MAKE("--exec",
                                        "--help"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_GROUP: group marker is transparent to parsing, --debug in Advanced group works */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--debug"),
                              (Entry[]) {
                                      { "debug" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* Mixed: all macro types together */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "pos1",
                                        "-h",
                                        "--version",
                                        "-v",
                                        "--required=rval",
                                        "--optional=oval",
                                        "--optional2=oval",
                                        "--debug",
                                        "pos2",
                                        "-o",
                                        "--help"),
                              (Entry[]) {
                                      { "help" },
                                      { "version" },
                                      { .short_code = 'v' },
                                      { "required", "rval" },
                                      { "optional", "oval" },
                                      { "optional2", "oval" },
                                      { "debug" },
                                      { "optional", NULL },
                                      { "help" },
                                      {}
                              },
                              STRV_MAKE("pos1",
                                        "pos2"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* Short option combos with macros: -hv (help + verbose) */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-hv"),
                              (Entry[]) {
                                      { "help" },
                                      { .short_code = 'v' },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* Short option combo with required arg: -hrval (help + required with arg "val") */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-hrval"),
                              (Entry[]) {
                                      { "help" },
                                      { "required", "val" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* Short option combo with optional arg: -hoval (help + optional with arg "val") */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-hoval"),
                              (Entry[]) {
                                      { "help" },
                                      { "optional", "val" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING then "--": "--" is still consumed after exec */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "--exec",
                                        "--",
                                        "--version",
                                        "-h"),
                              (Entry[]) {
                                      { "help" },
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("--version",
                                        "-h"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING then later "--": "--" is not consumed */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "--exec",
                                        "--version",
                                        "--",
                                        "-h"),
                              (Entry[]) {
                                      { "help" },
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("--version",
                                        "--",
                                        "-h"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* OPTION_STOPS_PARSING then "--" twice: second "--" is not consumed */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "--exec",
                                        "--",
                                        "--",
                                        "--version",
                                        "-h"),
                              (Entry[]) {
                                      { "help" },
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("--",
                                        "--version",
                                        "-h"),
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* Basic OPTION_POSITIONAL use */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "arg1",
                                        "--debug",
                                        "arg2"),
                              (Entry[]) {
                                      { "help" },
                                      { "(positional)", "arg1" },
                                      { "debug" },
                                      { "(positional)", "arg2" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_RETURN_POSITIONAL_ARGS,
                              NULL);
 
        /* OPTION_POSITIONAL combined with OPTION_STOPS_PARSING */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--help",
                                        "arg1",
                                        "--exec",
                                        "arg2"),
                              (Entry[]) {
                                      { "help" },
                                      { "(positional)", "arg1" },
                                      { "exec" },
                                      {}
                              },
                              STRV_MAKE("arg2"),
                              OPTION_PARSER_RETURN_POSITIONAL_ARGS,
                              NULL);
 
        /* Second namespace, OPTION: long form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "--required2=arg"),
                              (Entry[]) {
                                      { "required2", "arg" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              "namespaced options");
 
        /* Second namespace, OPTION: short form */
        test_macros_parse_one(STRV_MAKE("arg0",
                                        "-rarg"),
                              (Entry[]) {
                                      { "required2", "arg" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              "namespaced options");
}
 
/* Test the pattern used by nspawn's --user: an optional-arg option that also
 * peeks at the next arg to handle legacy "space-separated" form. */
TEST(option_optional_arg_consume) {
        static const Option options[] = {
                { __COUNTER__, .short_code = 'h', .long_code = "help" },
                { __COUNTER__, .long_code = "user", .metavar = "NAME", .flags = OPTION_OPTIONAL_ARG },
                { __COUNTER__, .short_code = 'u', .long_code = "uid", .metavar = "USER" },
                {}
        };
 
        /* --user=NAME: optional arg provided via = */
        test_option_parse_one(STRV_MAKE("arg0",
                                        "--user=root"),
                              options,
                              (Entry[]) {
                                      { "user", "root" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* --user without arg: next arg is an option, so no consumption */
        test_option_parse_one(STRV_MAKE("arg0",
                                        "--user",
                                        "--help"),
                              options,
                              (Entry[]) {
                                      { "user", NULL },
                                      { "help" },
                                      {}
                              },
                              NULL,
                              OPTION_PARSER_NORMAL,
                              NULL);
 
        /* --user without arg: next arg is positional (doesn't start with -).
         * The option parser returns NULL for the arg. The caller would then
         * use option_parser_peek_next_arg/consume_next_arg to grab it. */
        {
                char **argv = STRV_MAKE("arg0", "--user", "someuser", "pos1");
                int argc = strv_length(argv);
 
                OptionParser opts = { argc, argv };
 
                ASSERT_OK_POSITIVE(option_parse(options, options + 3, &opts));
                ASSERT_STREQ(opts.opt->long_code, "user");
                ASSERT_NULL(opts.arg);
                ASSERT_STREQ(option_parser_peek_next_arg(&opts), "someuser");
                ASSERT_STREQ(option_parser_consume_next_arg(&opts), "someuser");
 
                ASSERT_EQ(option_parse(options, options + 3, &opts), 0);
                ASSERT_NULL(opts.opt);
                ASSERT_NULL(opts.arg);
 
                ASSERT_TRUE(strv_equal(option_parser_get_args(&opts), STRV_MAKE("pos1")));
        }
 
        /* --user at end of args: no next arg, so scope mode */
        {
                char **argv = STRV_MAKE("arg0", "--user");
                int argc = strv_length(argv);
 
                OptionParser opts = { argc, argv };
 
                ASSERT_OK_POSITIVE(option_parse(options, options + 3, &opts));
                ASSERT_STREQ(opts.opt->long_code, "user");
                ASSERT_NULL(opts.arg);
                ASSERT_NULL(option_parser_peek_next_arg(&opts));
                ASSERT_NULL(option_parser_consume_next_arg(&opts));
 
                ASSERT_EQ(option_parse(options, options + 3, &opts), 0);
                ASSERT_NULL(opts.opt);
                ASSERT_NULL(opts.arg);
 
                ASSERT_TRUE(strv_isempty(option_parser_get_args(&opts)));
        }
 
        /* --user followed by -u (option): scope mode, -u gets its own processing */
        {
                char **argv = STRV_MAKE("arg0", "--user", "-u", "nobody");
                int argc = strv_length(argv);
 
                OptionParser opts = { argc, argv };
 
                ASSERT_OK_POSITIVE(option_parse(options, options + 3, &opts));
                ASSERT_STREQ(opts.opt->long_code, "user");
                ASSERT_NULL(opts.arg);
                ASSERT_STREQ(option_parser_peek_next_arg(&opts), "-u");
 
                ASSERT_OK_POSITIVE(option_parse(options, options + 3, &opts));
                ASSERT_STREQ(opts.opt->long_code, "uid");
                ASSERT_STREQ(opts.arg, "nobody");
                ASSERT_NULL(option_parser_peek_next_arg(&opts));
                ASSERT_NULL(option_parser_consume_next_arg(&opts));
 
                ASSERT_EQ(option_parse(options, options + 3, &opts), 0);
                ASSERT_NULL(opts.opt);
                ASSERT_NULL(opts.arg);
 
                ASSERT_TRUE(strv_isempty(option_parser_get_args(&opts)));
        }
 
        /* "Functional test": --user followed by -u (option): scope mode, -u gets its own processing,
         * handled like in a real option parser. */
        {
                char **argv = STRV_MAKE("arg0", "--user", "-u", "nobody", "nogroup", "--user=nobody", "--user");
                int argc = strv_length(argv);
 
                OptionParser opts = { argc, argv };
                int scope_seen = 0;
                int nobody_seen = 0;
 
                for (int c; (c = option_parse(options, options + 3, &opts)) != 0; ) {
                        ASSERT_OK(c);
 
                        if (streq_ptr(opts.opt->long_code, "user")) {
                                const char *arg = opts.arg;
 
                                if (!arg) {
                                        const char *t = option_parser_peek_next_arg(&opts);
                                        if (t && t[0] != '-')
                                                arg = option_parser_consume_next_arg(&opts);
                                }
 
                                if (arg) {
                                        ASSERT_STREQ(arg, "nobody");
                                        nobody_seen ++;
                                } else
                                        scope_seen ++;
 
                        } else if (streq_ptr(opts.opt->long_code, "uid")) {
                                ASSERT_STREQ(opts.arg, "nobody");
                                nobody_seen ++;
                        }
                }
 
                ASSERT_EQ(nobody_seen, 2);
                ASSERT_EQ(scope_seen, 2);
                ASSERT_TRUE(strv_equal(option_parser_get_args(&opts), STRV_MAKE("nogroup")));
                ASSERT_NULL(opts.opt);
                ASSERT_NULL(opts.arg);
        }
}
 
static void test_option_get_synopsis_one(
                const Option *opt,
                const char *joiner,
                bool show_metavar,
                const char *expected) {
        log_debug("%s", expected);
        _cleanup_free_ char *s = option_get_synopsis(opt, joiner, show_metavar);
        ASSERT_STREQ(s, expected);
}
 
TEST(option_get_synopsis) {
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "X" }, "/",  true,  "-x/--xxx=X");
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "X" }, NULL, true,  "-x --xxx=X");
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "X" }, "/",  false, "-x/--xxx=" );
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "X" }, " ",  true,  "-x --xxx=X");
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "X" }, " ",  false, "-x --xxx=" );
        test_option_get_synopsis_one(&(const Option) { 0, 0,   0, "xxx", "X" }, "+",  true,  "--xxx=X"   );
        test_option_get_synopsis_one(&(const Option) { 0, 0,   0, "xxx", "X" }, "+",  false, "--xxx="    );
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', NULL,  "X" }, " ",  true,  "-x X"      );
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', NULL,  "X" }, "/",  false, "-x"        );
 
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "A B" }, "/", true,  "-x/--xxx='A B'");
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', "xxx", "A B" }, " ", true,  "-x --xxx='A B'");
        test_option_get_synopsis_one(&(const Option) { 0, 0,   0, "xxx", "A B" }, "+", true,  "--xxx='A B'"   );
        test_option_get_synopsis_one(&(const Option) { 0, 0, 'x', NULL,  "A B" }, " ", true,  "-x 'A B'"      );
 
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "X" }, "/",  true,  "-x/--xxx[=X]");
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "X" }, NULL, true,  "-x --xxx[=X]");
        /* Note: --xxx[=] would be silly, so we show --xxx=. It's a corner case. Maybe this should change. */
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "X" }, "/",  false, "-x/--xxx="   );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "X" }, " ",  true,  "-x --xxx[=X]");
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "X" }, " ",  false, "-x --xxx="   );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG,   0, "xxx", "X" }, "+",  true,  "--xxx[=X]"   );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG,   0, "xxx", "X" }, "+",  false, "--xxx="      );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', NULL,  "X" }, " ",  true,  "-x [X]"      );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', NULL,  "X" }, "/",  false, "-x"          );
 
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "A B" }, "/", true,  "-x/--xxx[='A B']");
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', "xxx", "A B" }, " ", true,  "-x --xxx[='A B']");
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG,   0, "xxx", "A B" }, "+", true,  "--xxx[='A B']"   );
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG, 'x', NULL,  "A B" }, " ", true,  "-x ['A B']"      );
 
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_OPTIONAL_ARG | OPTION_HELP_ENTRY | OPTION_STOPS_PARSING,
                                                       'x', "xxx", "A B" }, "/", true,  "-x/--xxx[='A B']");
 
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_HELP_ENTRY_VERBATIM, 'u', "special special",  "unused" }, "/",  true, "special special");
        test_option_get_synopsis_one(&(const Option) { 0, OPTION_POSITIONAL_ENTRY, 'u', "(fixed)", "unused" }, "/",  true, "(fixed)");
}
 
TEST(option_empty_long_name) {
        static const Option one_option[] = {
                { 1, .short_code = 'o', .long_code = "output", .metavar = "ARG" },
                {}
        };
 
        static const Option two_options[] = {
                { 1, .short_code = 'o', .long_code = "output", .metavar = "ARG" },
                { 2, .long_code = "help" },
                {}
        };
 
        /* "--=…" carries an empty long option name, which must not prefix-match every defined option.
         * With a single option defined that would otherwise look like a unique partial match. */
        test_option_invalid_one(STRV_MAKE("arg0", "--=foo"), one_option);
        test_option_invalid_one(STRV_MAKE("arg0", "--="), one_option);
        test_option_invalid_one(STRV_MAKE("arg0", "--=foo"), two_options);
        test_option_invalid_one(STRV_MAKE("arg0", "--="), two_options);
}
 
DEFINE_TEST_MAIN(LOG_DEBUG);