diff --git a/junit_conversor/__init__.py b/junit_conversor/__init__.py index 4b66c08..c5000e9 100644 --- a/junit_conversor/__init__.py +++ b/junit_conversor/__init__.py @@ -1,11 +1,10 @@ -import xml.etree.cElementTree as ET import sys -from collections import defaultdict +import xml.etree.cElementTree as ET def _parse(file_name): lines = tuple(open(file_name, 'r')) - parsed = defaultdict(list) + parsed = {} for line in lines: splitted = line.split(":", 3) @@ -20,7 +19,8 @@ def _parse(file_name): 'code': splitted[3].strip()[:4] } - parsed[error['file']].append(error) + case_name = '{file}:{line}:{col}:'.format(file=error['file'], line=error['line'], col=error['col']) + parsed[case_name] = error return dict(parsed) @@ -35,21 +35,19 @@ def _convert(origin, destination): testsuite.attrib["tests"] = str(len(parsed)) or "1" testsuite.attrib["time"] = "1" - for file_name, errors in parsed.items(): - testcase = ET.SubElement(testsuite, "testcase", name=file_name) - - for error in errors: - kargs = { - "file": error['file'], - "line": error['line'], - "col": error['col'], - "message": error['detail'], - "type": "flake8 %s" % error['code'] - } + for case_name, error in parsed.items(): + testcase = ET.SubElement(testsuite, "testcase", name=case_name) + kargs = { + "file": error['file'], + "line": error['line'], + "col": error['col'], + "message": error['detail'], + "type": "flake8 %s" % error['code'] + } - text = "{0}:{1} {2}".format(error['line'], error['col'], error['detail']) + text = "{0}:{1} {2}".format(error['line'], error['col'], error['detail']) - ET.SubElement(testcase, "failure", **kargs).text = text + ET.SubElement(testcase, "failure", **kargs).text = text tree = ET.ElementTree(testsuite) diff --git a/tests/tests.py b/tests/tests.py index d7af427..099f90a 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -39,14 +39,14 @@ def test_should_parse_a_flake8_file_with_errors(self): parsed = _parse(failed_flake8) self.assertEqual(parsed, { - "tests/subject/__init__.py": [ + "tests/subject/__init__.py:1:1:": {"file": "tests/subject/__init__.py", "line": "1", "col": "1", "detail": "F401 'os' imported but unused", "code": "F401"}, + "tests/subject/__init__.py:3:1:": {"file": "tests/subject/__init__.py", "line": "3", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"}, - ], - "tests/subject/example.py": [ + "tests/subject/example.py:4:1:": {"file": "tests/subject/example.py", "line": "4", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"}, + "tests/subject/example.py:16:22:": {"file": "tests/subject/example.py", "line": "16", "col": "22", "detail": "E203 whitespace before ':'", "code": "E203"}, - ] }) def test_should_return_an_empty_dict_when_parsing_a_flake8_success_file(self): @@ -56,13 +56,12 @@ def test_should_skip_invalid_lines(self): parsed = _parse(failed_flake8_with_invalid_lines) self.assertEqual(parsed, { - "tests/subject/__init__.py": [ + "tests/subject/__init__.py:1:1:": {"file": "tests/subject/__init__.py", "line": "1", "col": "1", "detail": "F401 'os' imported but unused", "code": "F401"}, + "tests/subject/__init__.py:3:1:": {"file": "tests/subject/__init__.py", "line": "3", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"}, - ], - "tests/subject/example.py": [ + "tests/subject/example.py:4:1:": {"file": "tests/subject/example.py", "line": "4", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"}, - ] })