def main(cliargs=None): """Entry point for the application script :param list cliargs: Arguments to parse or None (=use sys.argv) """ atexit.register(shutdown, int(round(time.time() * 1000))) try: a = Actions(parsecli(cliargs)) res = a.parse() renderer = None if hasattr(a.args, 'format') is False: renderer = getrenderer('default') else: renderer = getrenderer(a.args.format) renderer(res, args=a.args) except PermissionError as err: # noqa log.error("%s on file %r.", err.args[1], err.filename) sys.exit(ReturnCodes.E_PERMISSION_DENIED) except ValueError as err: log.error(err) sys.exit(ReturnCodes.E_INVALID_XML_DOCUMENT) except FileNotFoundError as err: # noqa log.error("Could not find file %r.", err.filename) sys.exit(ReturnCodes.E_FILE_NOT_FOUND) except DMConfigFileNotFound as err: #noqa log.error("Couldn't find config file '%s'", err) sys.exit(ReturnCodes.E_FILE_NOT_FOUND) except KeyboardInterrupt: sys.exit()
def test_analyze_sort_0(testdir, tmpdir, capsys): """ Test the analyze sort feature """ xmlset = list() i = 1 while i <= 3: xmlset.append("analyze_sort-{}.xml".format(i)) i += 1 xmlfiles = [testdir / base for base in xmlset] for xmlfile in xmlfiles: xmlfile.copy(tmpdir) xmlfiles = [str(tmpdir / base) for base in xmlset] clicmd = 'analyze -qf "{{maintainer}}" -q -s "maintainer" {}'.format( " ".join(xmlfiles)) a = Actions(parsecli(shlex.split(clicmd))) a.parse() out, err = capsys.readouterr() expected_output = "a\nb\nc\n" assert not err assert expected_output == out
def test_analyze_sort_0(testdir, tmpdir, capsys): """ Test the analyze output """ xmlset = list() i = 1 while i <= 3: xmlset.append("analyze_output-{}.xml".format(i)) i += 1 xmlfiles = [ testdir / base for base in xmlset ] for xmlfile in xmlfiles: xmlfile.copy(tmpdir) xmlfiles = [ str(tmpdir / base) for base in xmlset ] clicmd = 'analyze -qf "{{maintainer}} {{status}} {{priority}}" {}'.format(" ".join(xmlfiles)) a = Actions(parsecli(shlex.split(clicmd))) a.parse() out, err = capsys.readouterr() out = out.split("\n") expected_output = [ "mschnitzer wip 1", "toms done 10", "fs editing 4" ] assert not err for i in expected_output: assert i in out
def test_docmanager_cli_del_0(tmp_valid_xml): """Checks if the delete command works """ ustr = "Ehufuveva271" file = tmp_valid_xml.strpath prepare_file(file, ustr) content = "" with open(file, 'r') as f: content = f.read() assert ustr in content clicmd = "del -p {} {}".format(ustr, file) a = Actions(parsecli(shlex.split(clicmd))) a.parse() content = "" with open(file, 'r') as f: content = f.read() assert ustr not in content
def test_docmanager_inputformatcheck(option, correct, wrong, tmp_valid_xml, capsys): """ Test the input format """ tmp_file = tmp_valid_xml.strpath # check with a wrong input format code = -1 try: clicmd = 'set --{} {} {}'.format(option, wrong, tmp_file) a = Actions(parsecli(clicmd.split())) except SystemExit as e: code = e.code assert code == ReturnCodes.E_WRONG_INPUT_FORMAT, "Wrong exit code. Expected {} but got {}.".format( ReturnCodes.E_WRONG_INPUT_FORMAT, code) # check with the correct input format code = 0 try: clicmd = 'set --{} {} {}'.format(option, correct, tmp_file) a = Actions(parsecli(clicmd.split())) except SystemExit as e: code = e.code assert 0 == code, "Wrong exit code. Expected 0 but got {}.".format(code)
def test_docmanager_aliascmd_outformat(format_type, expected, capsys): """Tests the 'alias' command """ # set default values testdir = os.path.dirname(__file__) configfile = os.path.join(testdir, "testfiles/dm-test.conf") cmd = "alias --own {} set alias_format_test1 alias1".format(configfile) Actions(parsecli(shlex.split(cmd))).parse() cmd = "alias --own {} set alias_format_test2 alias2".format(configfile) Actions(parsecli(shlex.split(cmd))).parse() # list all available aliases cmd = "alias --own {} --format {} list".format(configfile, format_type) args = parsecli(shlex.split(cmd)) a = Actions(parsecli(shlex.split(cmd))) res = a.parse() renderer = getrenderer(format_type) renderer(res, args=a.args) # read output out, err = capsys.readouterr() # compare assert out == expected
def test_exitcodes_3(tmp_invalid_db5_file): """ check for an invalid DocBook 5 file """ try: clicmd = "get invalid_file_name.xml" a = Actions(parsecli(shlex.split(clicmd))) a.parse() except SystemExit as e: assert e.code == ReturnCodes.E_FILE_NOT_FOUND, \ "Expected exit code {} but got {}.".format(ReturnCodes.E_FILE_NOT_FOUND, e.code)
def test_docmanager_jsonout(tmp_valid_xml, capsys): """ Test the json output format """ tmp_file = tmp_valid_xml.strpath # set some test values clicmd = 'set -p hello=world -p suse=green -p json=test {}'.format( tmp_file) a = Actions(parsecli(shlex.split(clicmd))) a.parse() out, err = capsys.readouterr() # read only 2 properties clicmd = 'get -p hello -p suse {} --format json'.format(tmp_file) a = Actions(parsecli(shlex.split(clicmd))) res = a.parse() renderer = getrenderer('json') renderer(res, args=a.args) out, err = capsys.readouterr() not_json = False try: out_json = json.loads(out) except ValueError as e: not_json = True assert not_json == False, "Output is not JSON." json_inv_data = "Returned JSON contains invalid data." assert out_json[tmp_file]['hello'] == 'world', json_inv_data assert out_json[tmp_file]['suse'] == 'green', json_inv_data # read all properties clicmd = 'get {} --format json'.format(tmp_file) a = Actions(parsecli(shlex.split(clicmd))) res = a.parse() renderer = getrenderer('json') renderer(res, args=a.args) out, err = capsys.readouterr() not_json = False try: out_json = json.loads(out) except ValueError as e: not_json = True assert not_json == False, "Output is not JSON." json_inv_data = "Returned JSON contains invalid data." assert out_json[tmp_file]['hello'] == 'world', json_inv_data assert out_json[tmp_file]['suse'] == 'green', json_inv_data assert out_json[tmp_file]['json'] == 'test', json_inv_data
def test_docmanager_init_0(tmp_valid_xml): """ Test the init sub command without force """ tmpfile = tmp_valid_xml.strpath clicmd = 'init {}'.format(tmpfile) a = Actions(parsecli(shlex.split(clicmd))) a.parse() handler = XmlHandler(tmpfile) for i in DEFAULT_DM_PROPERTIES: assert handler.is_prop_set( i) == True, "property {} is not set.".format(i)
def test_docmanager_configcmd_0(tmpdir, capsys): """Call the DocManager 'config' sub command and modify the config file """ configdir = tmpdir.mkdir(".config") configfile = configdir / "config" configfile.write_text("", encoding="utf-8") cmd = "config -o {} test1.test2 \"test3\"".format(configfile.strpath) a = Actions(parsecli(shlex.split(cmd))) a.parse() content = None with open(configfile.strpath, 'r') as f: content = f.read() result = "[test1]\ntest2 = test3\n\n" assert content == result try: cmd = "config -o {} test1.test2".format(configfile.strpath) a = Actions(parsecli(shlex.split(cmd))) a.parse() except SystemExit: pass out, err = capsys.readouterr() assert out == "test3\n"
def test_docmanager_aliascmd_0(capsys): """Tests the 'alias' command """ testdir = os.path.dirname(__file__) configfile = os.path.join(testdir, "testfiles/dm-test.conf") cmd = "alias --own {} set my alias".format(configfile) Actions(parsecli(shlex.split(cmd))).parse() cmd = "alias --own {} get my".format(configfile) args = parsecli(shlex.split(cmd)) Actions(parsecli(shlex.split(cmd))).parse() out, err = capsys.readouterr() assert out == "alias\n"
def test_docmanager_configcmd_1(tmpdir): """Call the DocManager 'config' sub command with an invalid syntax and check the exit code """ configdir = tmpdir.mkdir(".config") configfile = configdir / "config" configfile.write_text("", encoding="utf-8") code = -1 try: cmd = "config -o {} test1test2 \"test3\"".format(configfile.strpath) a = Actions(parsecli(shlex.split(cmd))) a.parse() except SystemExit as e: code = e.code assert code == ReturnCodes.E_INVALID_CONFIG_PROPERTY_SYNTAX
def test_multiple_files(props, xmlset, expected, testdir, tmpdir, capsys): """Checks multiple files to get the correct result :param list props: properties to check :param list xmlset: relative XML filenames :param list expected: expected result :param py.path.local testdir: Path to test directory (fixture) :param py.path.local tmpdir: temporary directory (fixture) :param capsys: capture system stderr """ # files = [ "test-dm-status-1.xml", "test-dm-status-2.xml" ] xmlfiles = [testdir / base for base in xmlset] for xmlfile in xmlfiles: xmlfile.copy(tmpdir) xmlfiles = [str(tmpdir / base) for base in xmlset] cli = "get -p %s %s" % (",".join(props), " ".join(xmlfiles)) a = Actions(parsecli(cli.split())) out, err = capsys.readouterr() result = [tuple(line.split(" -> ")) for line in out.strip().split("\n")].sort() # TODO: Make it more general # Expected the following output: # file1 -> a # file2 -> b if expected: expected = list(zip(repeat(xmlfiles, len(props)), expected)).sort() else: expected = None assert not err assert expected == result
def test_docmanager_setattr1(tmp_valid_xml): ustr = "Uf4C56WL" handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"myprop": None}) handler.write() clicmd = "set-attr -p myprop -a {}=blub {}".format(ustr, tmp_valid_xml.strpath) a = Actions(parsecli(shlex.split(clicmd))) a.parse() with open(tmp_valid_xml.strpath, 'r') as f: content = f.read() assert ustr + "=\"blub\"" in content, 'Attribute {}=blub could not be created.'.format( ustr)
def test_docmanager_xmlhandlerwrite(command,expected,tmp_valid_xml): """Check if the write() function of the XmlHandler will be called properly by the Actions class """ testfile = tmp_valid_xml.strpath clicmd = command.format(testfile) a = Actions(parsecli(shlex.split(clicmd))) a.parse() content = "" with open(testfile, 'r') as f: content = f.read() for i in expected: assert i in content
def test_docmanager_init_1(tmp_valid_xml, option, value): """ Test the init sub command with pre defined values """ tmpfile = tmp_valid_xml.strpath ropt = option.replace("/", "-") clicmd = "init --{} \"{}\" {}".format(ropt, value, tmpfile) a = Actions(parsecli(shlex.split(clicmd))) a.parse() handler = XmlHandler(tmpfile) ret = handler.get(option) assert ret[ option] == value, "The file was initialized without the pre defined value for the option '{}'.".format( option)
def test_exitcodes_1(tmp_broken_xml): """ parse broken xml file in get """ try: clicmd = "get {}".format(tmp_broken_xml) a = Actions(parsecli(shlex.split(clicmd))) except SystemExit as e: assert e.code == ReturnCodes.E_XML_PARSE_ERROR, \ "Expected exit code {} but got {}.".format(ReturnCodes.E_XML_PARSE_ERROR, e.code)
def test_exitcodes_2(tmp_invalid_db5_file): """ check for an invalid DocBook 5 file """ try: clicmd = "get {}".format(tmp_invalid_db5_file) a = Actions(parsecli(shlex.split(clicmd))) except SystemExit as e: assert e.code == ReturnCodes.E_INVALID_XML_DOCUMENT, \ "Expected exit code {} but got {}.".format(ReturnCodes.E_INVALID_XML_DOCUMENT, e.code)
def test_analyze_defaultoutput_0(testdir, tmpdir, capsys): """ Test the analyze default output """ xmlset = [ "analyze_output-1.xml" ] xmlfiles = [ testdir / base for base in xmlset ] for xmlfile in xmlfiles: xmlfile.copy(tmpdir) xmlfiles = [ str(tmpdir / base) for base in xmlset ] clicmd = 'analyze -qf "{{emptytest}} {{emptytest2}}" -q -do "-" {}'.format(" ".join(xmlfiles)) a = Actions(parsecli(shlex.split(clicmd))) a.parse() out, err = capsys.readouterr() expected_output = "- -\n" assert not err assert expected_output == out
def test_docmanager_getattr0(format_type, expected, tmp_valid_xml, capsys): ustr = 'Uf4C56WL' handler = XmlHandler(tmp_valid_xml.strpath) handler.set({"myprop": None}) handler.set_attr("myprop", {ustr: 'blub'}) handler.write() if format_type: clicmd = "get-attr -p myprop -a {} --format {} {}".format( ustr, format_type, tmp_valid_xml.strpath) else: clicmd = "get-attr -p myprop -a {} {}".format(ustr, tmp_valid_xml.strpath) a = Actions(parsecli(shlex.split(clicmd))) res = a.parse() renderer = getrenderer(format_type) renderer(res, args=a.args) out, err = capsys.readouterr() expected = expected.replace("[REPLACE_FILENAME]", tmp_valid_xml.strpath) assert out == expected
def test_docmanager_xmlout(tmp_valid_xml, capsys): """ Test the XML output format """ tmp_file = tmp_valid_xml.strpath # set some test values clicmd = 'set -p hello=world -p suse=green {}'.format(tmp_file) a = Actions(parsecli(shlex.split(clicmd))) a.parse() out, err = capsys.readouterr() # read only 2 properties clicmd = 'get -p hello -p suse {} --format xml'.format(tmp_file) a = Actions(parsecli(shlex.split(clicmd))) res = a.parse() renderer = getrenderer('xml') renderer(res, args=a.args) out, err = capsys.readouterr() root = None not_xml = False try: root = etree.fromstring(out) except XMLSyntaxError as e: not_xml = True assert not_xml == False, "Output is not XML." tree = root.getroottree() elem = root.find("./files") assert elem is not None, "<files> tag could not be found." assert root.find("./files/file") is not None, "No <file> tag in <files>." elem = root.find( "./files/file[@name='{}']/property[@name='hello']".format(tmp_file)) assert elem is not None, "<files> does not contain a \"property\" tag with \"hello\" as value in attribute \"name\"." assert elem.text is not "world", "tag <hello> has an invalid content." elem = root.find( "./files/file[@name='{}']/property[@name='suse']".format(tmp_file)) assert elem is not None, "<files> does not contain a \"property\" tag with \"suse\" as value in attribute \"name\"." assert elem.text is not "green", "tag <suse> has an invalid content."
def test_docmanager_predefprops(option, value, tmp_valid_xml, capsys): """Check predefined property actions""" # write test clicmd = shlex.split('set --{} "{}" {}'.format(option, value, tmp_valid_xml)) a = Actions(parsecli(clicmd)) a.parse() out, err = capsys.readouterr() # read test clicmd = shlex.split('get -p "{}" {}'.format(option, tmp_valid_xml)) a = Actions(parsecli(clicmd)) res = a.parse() renderer = getrenderer('default') renderer(res, args=a.args) out, err = capsys.readouterr() expected = value assert out[:-1] == expected, "get test: Expected output '{}' " \ "but got '{}'.".format(expected, out[:-1])
def prepare_file(file, ustr): clicmd = "set -p {}=bla {}".format(ustr, file) a = Actions(parsecli(shlex.split(clicmd))) a.parse()