Skip to content

Commit

Permalink
Merge pull request #2 from initios/feature/improvements
Browse files Browse the repository at this point in the history
Feature/improvements
  • Loading branch information
carlosgoce committed Mar 18, 2015
2 parents 136f6fa + 19cb283 commit e52dd0c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.coverage
tests/output/*.xml
4 changes: 2 additions & 2 deletions bin/junit_conversor
Expand Up @@ -3,8 +3,8 @@ import sys
from junit_conversor import _convert


def main(flake8_file, destination_file, delete_source_file=False):
_convert(flake8_file, destination_file, delete_source_file)
def main(flake8_file, destination_file):
_convert(flake8_file, destination_file)
sys.exit("File %s was created successfully" % destination_file)


Expand Down
30 changes: 16 additions & 14 deletions junit_conversor/__init__.py
@@ -1,24 +1,28 @@
import os
import xml.etree.cElementTree as ET
from collections import defaultdict


def _parse(file_name):
lines = tuple(open(file_name, 'r'))
parsed = []
parsed = defaultdict(list)

for line in lines:
splitted = line.split(":")
parsed.append({
error = {
'file': splitted[0].strip(),
'line': splitted[1].strip(),
'col': splitted[2].strip(),
'detail': splitted[3].strip(),
})
'code': splitted[3].strip()[:4]
}

return parsed
parsed[error['file']].append(error)

return dict(parsed)

def _convert(origin, destination, delete_origin=False):

def _convert(origin, destination):
parsed = _parse(origin)

if len(parsed) < 1:
Expand All @@ -31,15 +35,13 @@ def _convert(origin, destination, delete_origin=False):
testsuite.attrib["tests"] = str(len(parsed))
testsuite.attrib["time"] = "1"

for line in parsed:
ET.SubElement(testsuite, "testcase", file=line['file'],
line=line['line'], col=line['col']).text = line['detail']
for file_name, errors in parsed.items():
testcase = ET.SubElement(testsuite, "testcase", name=file_name)

for error in errors:
ET.SubElement(testcase, "error", file=error['file'], line=error['line'], col=error['col'],
message=error['detail'], type="flake8 %s" % error['code']) \
.text = "{}:{} {}".format(error['line'], error['col'], error['detail'])

tree = ET.ElementTree(testsuite)
tree.write(destination, encoding='utf-8', xml_declaration=True)

if delete_origin:
try:
os.remove(destination)
except OSError:
pass
17 changes: 10 additions & 7 deletions tests/tests.py
Expand Up @@ -15,21 +15,24 @@ class ParseTest(unittest.TestCase):
def test_can_parse_a_flake8_file(self):
parsed = _parse(failed_flake8)

self.assertEqual(parsed, [
{'file': 'tests/subject/__init__.py', 'line': '1', 'col': '1', 'detail': "F401 'os' imported but unused"},
{'file': 'tests/subject/__init__.py', 'line': '3', 'col': '1', 'detail': "E302 expected 2 blank lines, found 1"},
{'file': 'tests/subject/example.py', 'line': '4', 'col': '1', 'detail': "E302 expected 2 blank lines, found 1"},
])
self.assertEqual(parsed, {
"tests/subject/__init__.py": [
{"file": "tests/subject/__init__.py", "line": "1", "col": "1", "detail": "F401 'os' imported but unused", "code": "F401"},
{"file": "tests/subject/__init__.py", "line": "3", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"},
],
"tests/subject/example.py": [
{"file": "tests/subject/example.py", "line": "4", "col": "1", "detail": "E302 expected 2 blank lines, found 1", "code": "E302"},
]
})

def test_parsing_an_flake8_success_file_returns_an_empty_list(self):
self.assertEqual([], _parse(valid_flake8))
self.assertEqual({}, _parse(valid_flake8))


class ConvertTest(unittest.TestCase):
def setUp(self):
self.destination = os.path.join(output_dir, 'junit.xml')

def tearDown(self):
try:
os.remove(self.destination)
except OSError:
Expand Down

0 comments on commit e52dd0c

Please sign in to comment.