def test_tag_show_good_tag(capsys): tag.tag("nixconfig", ["show", "tag1"]) _out, _err = capsys.readouterr() assert "script1" in _out, "'script1' should be in output" assert "script2" in _out, "'script2' should be in output" assert "script3" not in _out, "'script2' should be in output" assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_tag_list_no_tag(capsys): tag.tag("nixconfig", ["list"]) _out, _err = capsys.readouterr() assert "tag1" in _out, "'tag1' should be in output" assert "tag2" in _out, "'tag2' should be in output" assert "tag3" in _out, "'tag3' should be in output" assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_tag_show_invalid_tag(capsys): with pytest.raises(NixError) as _excinfo: tag.tag("nixconfig", ["show", "badtag"]) _out, _err = capsys.readouterr() assert "Unknown tag: badtag" in str(_excinfo.value) assert len(_out) == 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_tag_rename_to_exist_tag(capsys): with pytest.raises(NixError) as _excinfo: tag.tag("nixconfig", ["rename", "tag1", "tag2"]) _out, _err = capsys.readouterr() assert "New tag already exists: tag2" 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_tag_no_command(capsys): with pytest.raises(NixError) as _excinfo: tag.tag("nixconfig", []) _out, _err = capsys.readouterr() assert "Missing tag command" in str( _excinfo.value), "exception doesn't contain expected string" assert "usage: nixconfig tag" in _out, "StdOut doesn't contain expected string" assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_tag_show_no_tag(capsys): with pytest.raises(SystemExit) as _excinfo: tag.tag("nixconfig", ["show"]) _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: tag" in _err, "StdErr doesn't contain expected string"
def test_tag_rename_same(capsys): with pytest.raises(NixError) as _excinfo: tag.tag("nixconfig", ["rename", "tag1", "tag1"]) _out, _err = capsys.readouterr() assert "Original and new tag names are the same: tag1" 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_bad_command(capsys): with pytest.raises(SystemExit) as _excinfo: tag.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 test_tag_delete_in_use_tag(capsys): with pytest.raises(NixError) as _excinfo: tag.tag("nixconfig", ["delete", "tag1"]) _out, _err = capsys.readouterr() assert "Unable to delete tag: tag1 while attached to scripts" in str( _excinfo.value) assert len(_out) == 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err)
def test_tag_rename_good_tag(capsys): tag.tag("nixconfig", ["rename", "tag1", "newtag"]) _out, _err = capsys.readouterr() assert len(_out) is 0, "StdOut should be empty, contains: {}".format(_out) assert len(_err) is 0, "StdErr should be empty, contains: {}".format(_err) tag.tag("nixconfig", ["rename", "newtag", "tag1"]) _out, _err = capsys.readouterr() 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 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)