def test_script_tag_list_good_script(capsys): script_tag.script_tag("nixconfig", ["list", "script1"]) _out, _err = capsys.readouterr() assert "tag1" in _out, "'tag1' should be in output" assert "tag2" in _out, "'tag2' should be in output" assert "tag3" not in _out, "'tag3' should not be in output" assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_script_tag_no_command(capsys): with pytest.raises(NixError) as _excinfo: script_tag.script_tag("nixconfig", []) _out, _err = capsys.readouterr() assert "Missing script-tag command" in str( _excinfo.value), "exception doesn't contain expected string" assert "usage: nixconfig script-tag" in _out, "StdOut doesn't contain expected string" assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_script_tag_remove_no_script(capsys): with pytest.raises(SystemExit) as _excinfo: script_tag.script_tag("nixconfig", ["remove"]) _out, _err = capsys.readouterr() assert "2" in str(_excinfo.value), "Exception doesn't contain expected string" assert len(_out) == 0, "StdOut should be empty, contains: {}".format(_out) assert "the following arguments are required: script, tags" in _err, "StdErr doesn't contain expected string"
def test_script_tag_list_good_script_unattached_tag(capsys): with pytest.raises(NixError) as _excinfo: script_tag.script_tag("nixconfig", ["remove", "script1", "tag3"]) _out, _err = capsys.readouterr() assert "Tags not attached to script: tag3" in str(_excinfo.value) assert len(_out) is 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_script_tag_list_good_script_bad_tag(capsys): with pytest.raises(NixError) as _excinfo: script_tag.script_tag("nixconfig", ["remove", "script1", "badtag"]) _out, _err = capsys.readouterr() assert "Unknown tags: badtag" in str(_excinfo.value) assert len(_out) is 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_script_tag_remove_bad_script(capsys): with pytest.raises(NixError) as _excinfo: script_tag.script_tag("nixconfig", ["remove", "badscript", "tag1"]) _out, _err = capsys.readouterr() assert "Unable to find script: badscript" in str(_excinfo.value) assert len(_out) is 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_script_tag_bad_command(capsys): with pytest.raises(SystemExit) as _excinfo: script_tag.script_tag("nixconfig", ["badcommand"]) _out, _err = capsys.readouterr() assert "2" in str( _excinfo.value), "Exception doesn't contain expected string" assert len(_out) == 0, "StdOut should be empty, contains: {}".format(_out) assert "invalid choice" in _err, "StdErr doesn't contain expected string"
def make_parser(sys_argv): """ Command line argument parser for the Nixconfig program. """ LOG.debug("Call parser") parser = argparse.ArgumentParser( description='Configure Nix', usage='''nixconfig {script,script-tag,tag} [<args>] Commands: script create, delete, update... scripts script-tag add, remove, list... tags from scripts tag create, delete, list... tags ''') parser.add_argument('command', nargs="?", default="", help='Subcommand to run') args = parser.parse_args(sys_argv[1:2]) _prog = sys.argv[0].rsplit("/", 1)[-1] if args.command == "script": script.script(_prog, sys_argv[2:]) elif args.command == "script-tag": script_tag.script_tag(_prog, sys_argv[2:]) elif args.command == "tag": tag.tag(_prog, sys_argv[2:]) else: print('Unrecognized command') parser.print_help() exit(1)