From aacc3211e5e05ded2b948a8398916cf937d132e7 2024-02-24 01:43:39 From: Branko Majic Date: 2024-02-24 01:43:39 Subject: [PATCH] GC-43: Fix Python 3.11 failing CLI test due to subparser masking: - Renamed the subparser commands to be fully distinct. - The subparser registration is never reset since the Python test file is run as part of a single process, thus the two tests would register two subparsers under the same subcommand. - In older versions of Python this was not a problem, but with newer verions (3.11+) this inconsistency has been fixed. For more details, see: https://bugs.python.org/issue39716 --- diff --git a/tests/test_cli.py b/tests/test_cli.py index 1a905d596a87ad6a42116fae3e423b5b9cf49afa..099a8927365c00ef975d749baf4467dfe59bdd18 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -566,42 +566,42 @@ def test_usage_command_invoked_with_correct_parameters(mock_usage, tmpdir): assert mock_usage.call_count == 1 -@mock.patch('sys.argv', ['gimmecert', 'testcommand']) +@mock.patch('sys.argv', ['gimmecert', 'successcommand']) def test_main_does_not_exit_if_it_calls_function_that_returns_success(tmpdir): # This should ensure we don't accidentally create artifacts # outside of test directory. tmpdir.chdir() @gimmecert.decorators.subcommand_parser - def setup_testcommand_parser(parser, subparsers): - subparser = subparsers.add_parser('testcommand', description='test command') + def setup_successcommand_parser(parser, subparsers): + subparser = subparsers.add_parser('successcommand', description='success command') - def testcommand_wrapper(args): + def successcommand_wrapper(args): return gimmecert.commands.ExitCode.SUCCESS - subparser.set_defaults(func=testcommand_wrapper) + subparser.set_defaults(func=successcommand_wrapper) return subparser gimmecert.cli.main() # Should not raise -@mock.patch('sys.argv', ['gimmecert', 'testcommand']) +@mock.patch('sys.argv', ['gimmecert', 'failurecommand']) def test_main_exits_if_it_calls_function_that_returns_failure(tmpdir): # This should ensure we don't accidentally create artifacts # outside of test directory. tmpdir.chdir() @gimmecert.decorators.subcommand_parser - def setup_testcommand_parser(parser, subparsers): - subparser = subparsers.add_parser('testcommand', description='test command') + def setup_failurecommand_parser(parser, subparsers): + subparser = subparsers.add_parser('failurecommand', description='failure command') - def testcommand_wrapper(args): + def failurecommand_wrapper(args): return gimmecert.commands.ExitCode.ERROR_ALREADY_INITIALISED - subparser.set_defaults(func=testcommand_wrapper) + subparser.set_defaults(func=failurecommand_wrapper) return subparser