Changeset - aacc3211e5e0
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 months ago 2024-02-24 01:43:39
branko@majic.rs
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
1 file changed with 10 insertions and 10 deletions:
0 comments (0 inline, 0 general)
tests/test_cli.py
Show inline comments
 
@@ -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
 

	
0 comments (0 inline, 0 general)