From 1de4c110cf157d0907efb98bc7ba3282e82bbb50 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Thu, 15 Nov 2018 16:23:56 +0100 Subject: [PATCH] Get testrun id from config file as well --- dump2polarion/dumper_cli.py | 28 ++++++++++++++++++++-------- dump2polarion/properties.py | 15 +++++++++++++++ dump2polarion/utils.py | 6 ++++++ tests/test_polarion_dumper.py | 18 +++++++++++++----- tests/test_properties.py | 18 ++++++++++++++++++ 5 files changed, 72 insertions(+), 13 deletions(-) diff --git a/dump2polarion/dumper_cli.py b/dump2polarion/dumper_cli.py index 1504fdc..1c775b1 100644 --- a/dump2polarion/dumper_cli.py +++ b/dump2polarion/dumper_cli.py @@ -72,18 +72,30 @@ def get_submit_args(args): return {k: v for k, v in submit_args.items() if v is not None} -def get_testrun_id(args, testrun_id): +def get_testrun_id(args, config, records_testrun_id): """Returns testrun id.""" - if args.testrun_id and testrun_id and not args.force and testrun_id != args.testrun_id: + config_testrun_id = utils.get_testrun_id_config(config) + + found_testrun_id = args.testrun_id or records_testrun_id or config_testrun_id + if not found_testrun_id: raise Dump2PolarionException( - "The test run id '{}' found in exported data doesn't match '{}'. " - "If you really want to proceed, add '-f'.".format(testrun_id, args.testrun_id) + "The testrun id was not specified on command line and not found in the input data " + "or config file." ) - found_testrun_id = args.testrun_id or testrun_id - if not found_testrun_id: + match = True + for tr_id in (args.testrun_id, records_testrun_id, config_testrun_id): + if tr_id and tr_id != found_testrun_id: + match = False + break + + if not match and args.force: + logger.warning("Using '%s' as testrun id.", found_testrun_id) + elif not match: raise Dump2PolarionException( - "The testrun id was not specified on command line and not found in the input data." + "The test run ids found in exported data, config file and/or specified on command line " + "differ. If you really want to proceed, add '-f' and test run id '{}' " + "will be used.".format(found_testrun_id) ) return found_testrun_id @@ -133,7 +145,7 @@ def dumper(args, config, transform_func=None): try: records = dump2polarion.do_import(args.input_file, older_than=import_time) - testrun_id = get_testrun_id(args, records.testrun) + testrun_id = get_testrun_id(args, config, records.testrun) exporter = dump2polarion.XunitExport( testrun_id, records, config, transform_func=transform_func ) diff --git a/dump2polarion/properties.py b/dump2polarion/properties.py index 6bed03e..41120e8 100644 --- a/dump2polarion/properties.py +++ b/dump2polarion/properties.py @@ -151,6 +151,21 @@ def remove_response_property(xml_root): raise Dump2PolarionException(_NOT_EXPECTED_FORMAT_MSG) +def remove_property(xml_root, partial_name): + """Removes properties if exist.""" + if xml_root.tag in ("testsuites", "testcases", "requirements"): + properties = xml_root.find("properties") + remove_properties = [] + for prop in properties: + prop_name = prop.get("name", "") + if partial_name in prop_name: + remove_properties.append(prop) + for rem_prop in remove_properties: + properties.remove(rem_prop) + else: + raise Dump2PolarionException(_NOT_EXPECTED_FORMAT_MSG) + + def set_lookup_method(xml_root, value): """Changes lookup method.""" if xml_root.tag == "testsuites": diff --git a/dump2polarion/utils.py b/dump2polarion/utils.py index e58b92d..eccdfe5 100644 --- a/dump2polarion/utils.py +++ b/dump2polarion/utils.py @@ -165,3 +165,9 @@ def find_vcs_root(path, dirs=(".git",)): return path prev, path = path, os.path.abspath(os.path.join(path, os.pardir)) return None + + +def get_testrun_id_config(config): + """Gets testrun ID defined in config file.""" + config_testrun_id = config.get("xunit_import_properties") or {} + return config_testrun_id.get("polarion-testrun-id") diff --git a/tests/test_polarion_dumper.py b/tests/test_polarion_dumper.py index 3ddba32..8eede83 100644 --- a/tests/test_polarion_dumper.py +++ b/tests/test_polarion_dumper.py @@ -29,24 +29,32 @@ def test_get_args(self): def test_testrun_id_match(self): args = dumper_cli.get_args(["-i", "dummy", "-t", "5_8_0_17"]) - found = dumper_cli.get_testrun_id(args, "5_8_0_17") + found = dumper_cli.get_testrun_id(args, {}, "5_8_0_17") assert found == "5_8_0_17" def test_testrun_id_nomatch(self): args = dumper_cli.get_args(["-i", "dummy", "-t", "5_8_0_17"]) with pytest.raises(Dump2PolarionException) as excinfo: - dumper_cli.get_testrun_id(args, "5_8_0_18") - assert "found in exported data doesn't match" in str(excinfo.value) + dumper_cli.get_testrun_id(args, {}, "5_8_0_18") + assert "differ" in str(excinfo.value) + + def test_testrun_id_config_nomatch(self): + args = dumper_cli.get_args(["-i", "dummy", "-t", "5_8_0_18"]) + with pytest.raises(Dump2PolarionException) as excinfo: + dumper_cli.get_testrun_id( + args, {"xunit_import_properties": {"polarion-testrun-id": "5_8_0_17"}}, "5_8_0_18" + ) + assert "differ" in str(excinfo.value) def test_testrun_id_force(self): args = dumper_cli.get_args(["-i", "dummy", "-t", "5_8_0_18", "--force"]) - found = dumper_cli.get_testrun_id(args, "5_8_0_17") + found = dumper_cli.get_testrun_id(args, {}, "5_8_0_17") assert found == "5_8_0_18" def test_testrun_id_missing(self): args = dumper_cli.get_args(["-i", "dummy"]) with pytest.raises(Dump2PolarionException) as excinfo: - dumper_cli.get_testrun_id(args, None) + dumper_cli.get_testrun_id(args, {}, None) assert "The testrun id was not specified" in str(excinfo.value) def test_submit_if_ready_noxml(self, config_prop): diff --git a/tests/test_properties.py b/tests/test_properties.py index 01df5a2..6c2dc98 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -110,6 +110,24 @@ def test_remove_testcases_response_property(self): filled = utils.etree_to_string(xml_root) assert "