def test_cli_command_version_flag(capsys): """Tests that `quilt --version` is working""" TESTED_PARAMS.append(['--version']) from quilt.tools.main import main with pytest.raises(SystemExit): main(['--version']) outerr = capsys.readouterr() # there's not a lot to test here -- this literally just does the same thing # that 'quilt --version' does, but this at least ensures that it still # exists and still produces the expected result. dist = pkg_resources.get_distribution('quilt') expectation = "quilt {} ({})\n".format(dist.version, dist.egg_name()) # in python 2, apparently argparse's 'version' handler prints to stderr. result = outerr.err if PY2 else outerr.out assert expectation == result
def test_cli_command_in_help(capsys): """Tests for inclusion in 'help' Only tests the base subcommand, not sub-subcommands. """ TESTED_PARAMS.append(['--version']) from quilt.tools.main import main expected_params = set() hidden_params = set() expected_optionals = set() hidden_optionals = {'--dev'} for argpath in KNOWN_PARAMS: if len(argpath) == 1: if isinstance(argpath[0], string_types): assert argpath[0].startswith( '-'), "bug in test, not in tested code" expected_optionals.add(argpath[0]) continue if isinstance(argpath[1], string_types): expected_params.add(argpath[1]) with pytest.raises(SystemExit): main(['help']) outerr = capsys.readouterr() lines = outerr.out.split('\n') for pos, line in enumerate(lines): if line.strip() == '<subcommand>': start = pos + 1 found_params = [] for pos, line in enumerate(lines[start:]): print(line) if line.strip() == "optional arguments:": print("stopping (optionals)") start = pos + 1 break splitline = line.split(None, 1) if not splitline: print('skipped (splitline)') continue if line[4] == ' ': # skip multiline help text print('skipped (char 4)') continue arg = splitline[0] found_params.append(arg) assert found_params == sorted( found_params), 'Help params should be sorted properly' assert set( found_params ) | hidden_params == expected_params, 'Found params do not match expected params' found_optionals = [] for line in lines[start:]: splitline = line.split( None, 1) # ignore second optional form if present (--help, -h) if not splitline: continue optional = splitline[0].rstrip(',') if not optional.startswith('-'): continue print(optional) if optional in ['--help', '-h']: continue found_optionals.append(optional) assert found_optionals == sorted(found_optionals) assert set(found_optionals) | hidden_optionals == expected_optionals