Re: [PATCH 04/20] t0040-parse-options: test parse_options() with various 'parse_opt_flags'

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jul 25, 2022 at 04:38:48PM +0200, Ævar Arnfjörð Bjarmason wrote:
> 
> On Mon, Jul 25 2022, SZEDER Gábor wrote:
> 
> > In 't0040-parse-options.sh' we thoroughly test the parsing of all
> > types and forms of options, but in all those tests parse_options() is
> > always invoked with a 0 flags parameter.
> >
> > Add a few tests to demonstrate how various 'enum parse_opt_flags'
> > values are supposed to influence option parsing.
> >
> > Signed-off-by: SZEDER Gábor <szeder.dev@xxxxxxxxx>
> > ---
> >  t/helper/test-parse-options.c | 61 ++++++++++++++++++++++++++++++
> >  t/helper/test-tool.c          |  1 +
> >  t/helper/test-tool.h          |  1 +
> >  t/t0040-parse-options.sh      | 70 +++++++++++++++++++++++++++++++++++
> >  4 files changed, 133 insertions(+)
> >
> > diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
> > index 48d3cf6692..32b906bd6a 100644
> > --- a/t/helper/test-parse-options.c
> > +++ b/t/helper/test-parse-options.c
> > @@ -192,3 +192,64 @@ int cmd__parse_options(int argc, const char **argv)
> >  
> >  	return ret;
> >  }
> > +
> > +static int parse_options_flags__cmd(int argc, const char **argv,
> > +				    enum parse_opt_flags test_flags)
> > +{
> > +	const char *usage[] = {
> > +		"<...> cmd [options]",
> > +		NULL
> > +	};
> > +	int opt = 0;
> > +	const struct option options[] = {
> > +		OPT_INTEGER('o', "opt", &opt, "an integer option"),
> > +		OPT_END()
> > +	};
> > +
> > +	argc = parse_options(argc, argv, NULL, options, usage, test_flags);
> > +
> > +	printf("opt: %d\n", opt);
> > +	for (int i = 0; i < argc; i++)
> > +		printf("arg %02d: %s\n", i, argv[i]);
> > +
> > +	return 0;
> > +}
> > +
> > +static enum parse_opt_flags test_flags = 0;
> > +static const struct option test_flag_options[] = {
> > +	OPT_GROUP("flag-options:"),
> > +	OPT_BIT(0, "keep-dashdash", &test_flags,
> > +		"pass PARSE_OPT_KEEP_DASHDASH to parse_options()",
> > +		PARSE_OPT_KEEP_DASHDASH),
> > +	OPT_BIT(0, "stop-at-non-option", &test_flags,
> > +		"pass PARSE_OPT_STOP_AT_NON_OPTION to parse_options()",
> > +		PARSE_OPT_STOP_AT_NON_OPTION),
> > +	OPT_BIT(0, "keep-argv0", &test_flags,
> > +		"pass PARSE_OPT_KEEP_ARGV0 to parse_options()",
> > +		PARSE_OPT_KEEP_ARGV0),
> > +	OPT_BIT(0, "keep-unknown", &test_flags,
> > +		"pass PARSE_OPT_KEEP_UNKNOWN to parse_options()",
> > +		PARSE_OPT_KEEP_UNKNOWN),
> > +	OPT_BIT(0, "no-internal-help", &test_flags,
> > +		"pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
> > +		PARSE_OPT_NO_INTERNAL_HELP),
> > +	OPT_END()
> > +};
> > +
> > +int cmd__parse_options_flags(int argc, const char **argv)
> > +{
> > +	const char *usage[] = {
> > +		"test-tool parse-options-flags [flag-options] cmd [options]",
> > +		NULL
> > +	};
> > +
> > +	argc = parse_options(argc, argv, NULL, test_flag_options, usage,
> > +			     PARSE_OPT_STOP_AT_NON_OPTION);
> > +
> > +	if (argc == 0 || strcmp(argv[0], "cmd")) {
> > +		error("'cmd' is mandatory");
> > +		usage_with_options(usage, test_flag_options);
> > +	}
> > +
> > +	return parse_options_flags__cmd(argc, argv, test_flags);
> > +}
> > diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
> > index 318fdbab0c..6e62282b60 100644
> > --- a/t/helper/test-tool.c
> > +++ b/t/helper/test-tool.c
> > @@ -51,6 +51,7 @@ static struct test_cmd cmds[] = {
> >  	{ "online-cpus", cmd__online_cpus },
> >  	{ "pack-mtimes", cmd__pack_mtimes },
> >  	{ "parse-options", cmd__parse_options },
> > +	{ "parse-options-flags", cmd__parse_options_flags },
> >  	{ "parse-pathspec-file", cmd__parse_pathspec_file },
> >  	{ "partial-clone", cmd__partial_clone },
> >  	{ "path-utils", cmd__path_utils },
> 
> I wanted to add some new parse_options() code to
> t/helper/test-parse-options.c in the past, but was stymied by its
> cmd_*() going through a singular parse_options().
> 
> So just creating a new callback is a neat solution.
> 
> But wouldn't it be better to just create a new
> t/helper/test-parse-options-something-.c & test file? It seems this
> doesn't really share anything with the current helper & tests...

Well, at least they share the concept, as they all test parse-options.
And 'parse-options-flags' and 'parse-subcommands' do share the options
array to specify the various parse_opt_flags.

A new test script for these flags and/or for subcommands would
definitely be worse, IMO.  I've found it very convenient that whenever
I updated 'parse-options.{c,h}', I only needed to run a single
'./t0040-parse-options.sh' script to check my changes.




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux