Skip to content

Commit cc1af1d

Browse files
vladimirolteankuba-moo
authored andcommitted
ingest_mdir: fix --list-tests handling
There are 2 problems with the --list-tests command: - The arguments --tree and the --patch/--mdir group are marked as required=True, forcing argparse to demand them even when they aren't needed (like for --list-tests). - The main() function tries to access args.tree before checking if --list-tests was specified, which would cause a crash even if the parser requirement was removed. This problem is masked by the first one, so it makes sense to handle them together. $ ./ingest_mdir.py --list-tests # should have succeeded ingest_mdir.py: error: the following arguments are required: --tree $ ./ingest_mdir.py --tree dummy --list-tests ingest_mdir.py: error: one of the arguments --patch --mdir is required $ ./ingest_mdir.py --tree dummy --patch dummy2 --list-tests # only this variant succeeds Remove the required=True from the patch_arg group and the --tree argument, and add manual handling for them if --list-tests wasn't specified. Also, move the args.tree dereference after the list_tests check and the manual verification that the argument was provided. Signed-off-by: Vladimir Oltean <[email protected]>
1 parent ee7cf4a commit cc1af1d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

ingest_mdir.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242

4343
parser = argparse.ArgumentParser()
4444

45-
patch_arg = parser.add_mutually_exclusive_group(required=True)
45+
patch_arg = parser.add_mutually_exclusive_group()
4646
patch_arg.add_argument('--patch', help='path to the patch file')
4747
patch_arg.add_argument('--mdir', help='path to the directory with the patches')
4848

49-
parser.add_argument('--tree', required=True, help='path to the tree to test on')
49+
parser.add_argument('--tree', help='path to the tree to test on')
5050
parser.add_argument('--tree-name', help='the tree name to expect')
5151
parser.add_argument('--result-dir',
5252
help='the directory where results will be generated')
@@ -286,15 +286,23 @@ def main():
286286
YELLOW = ''
287287
RESET = ''
288288

289-
args.tree = os.path.abspath(args.tree)
290-
291289
if args.test:
292290
config.set('tests', 'include', ','.join(args.test))
293291

292+
# Handle --list-tests first, as it needs no other arguments
294293
if args.list_tests:
295294
list_tests(args, config)
296295
return
297296

297+
# If not listing tests, manually validate the required arguments
298+
if not args.tree:
299+
parser.error("the following arguments are required: --tree")
300+
301+
if not args.patch and not args.mdir:
302+
parser.error("one of the arguments --patch --mdir is required")
303+
304+
args.tree = os.path.abspath(args.tree)
305+
298306
if args.result_dir is None:
299307
args.result_dir = tempfile.mkdtemp()
300308
print("Saving output and logs to:", args.result_dir)

0 commit comments

Comments
 (0)