Skip to content

Commit

Permalink
Get testrun id from config file as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Nov 15, 2018
1 parent ec236a1 commit 1de4c11
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
28 changes: 20 additions & 8 deletions dump2polarion/dumper_cli.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
15 changes: 15 additions & 0 deletions dump2polarion/properties.py
Expand Up @@ -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":
Expand Down
6 changes: 6 additions & 0 deletions dump2polarion/utils.py
Expand Up @@ -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")
18 changes: 13 additions & 5 deletions tests/test_polarion_dumper.py
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_properties.py
Expand Up @@ -110,6 +110,24 @@ def test_remove_testcases_response_property(self):
filled = utils.etree_to_string(xml_root)
assert "<response-properties" not in filled

def test_remove_testsuites_property(self):
fname = "complete_transform.xml"
xml_root = utils.get_xml_root(os.path.join(conf.DATA_PATH, fname))
filled = utils.etree_to_string(xml_root)
assert '<property name="polarion-testrun-id"' in filled
properties.remove_property(xml_root, "testrun-id")
filled = utils.etree_to_string(xml_root)
assert '<property name="polarion-testrun-id"' not in filled

def test_remove_testcases_property(self):
fname = "testcases.xml"
xml_root = utils.get_xml_root(os.path.join(conf.DATA_PATH, fname))
filled = utils.etree_to_string(xml_root)
assert '<property name="lookup-method"' in filled
properties.remove_property(xml_root, "lookup-method")
filled = utils.etree_to_string(xml_root)
assert '<property name="lookup-method"' not in filled

def test_add_testsuites_lookup_property(self):
fname = "complete_noprop.xml"
xml_root = utils.get_xml_root(os.path.join(conf.DATA_PATH, fname))
Expand Down

0 comments on commit 1de4c11

Please sign in to comment.