Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit e3a6f06

Browse files
author
Rodrigo Fernandes
committed
Merge pull request #6 from codacy/add-coverage
Add coverage
2 parents db580b9 + 27148fc commit e3a6f06

File tree

15 files changed

+410
-47
lines changed

15 files changed

+410
-47
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ install: build
77
python setup.py develop
88

99
test: pep8 pyflakes
10+
sed 's?\$$1?'`pwd`'?' tests/filepath/cobertura.xml.tpl > tests/filepath/cobertura.xml
1011
python setup.py test
12+
rm tests/filepath/cobertura.xml || true
13+
14+
test-all:
15+
tox
16+
17+
coverage:
18+
rm coverage.xml || true
19+
sed 's?\$$1?'`pwd`'?' tests/filepath/cobertura.xml.tpl > tests/filepath/cobertura.xml
20+
coverage run --source src/codacy/ setup.py test
21+
rm tests/filepath/cobertura.xml || true
22+
coverage xml
23+
python-codacy-coverage -r coverage.xml
1124

1225
# requires "pip install pep8"
1326
pep8:

README.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ python-codacy-coverage
33

44
Credits to Ryan for creating this! Python coverage reporter for Codacy https://www.codacy.com
55

6+
.. image:: https://api.codacy.com/project/badge/grade/3a8cf06a9db94d0ab3d55e0357bc8f9d
7+
:target: https://www.codacy.com/app/Codacy/python-codacy-coverage
8+
:alt: Codacy Badge
9+
.. image:: https://api.codacy.com/project/badge/coverage/3a8cf06a9db94d0ab3d55e0357bc8f9d
10+
:target: https://www.codacy.com/app/Codacy/python-codacy-coverage
11+
:alt: Codacy Badge
612
.. image:: https://circleci.com/gh/codacy/python-codacy-coverage.png?style=shield&circle-token=:circle-token
713
:target: https://circleci.com/gh/codacy/python-codacy-coverage
814
:alt: Build Status
9-
.. image:: https://www.codacy.com/project/badge/3a8cf06a9db94d0ab3d55e0357bc8f9d
10-
:target: https://www.codacy.com/app/Codacy/python-codacy-coverage
11-
:alt: Codacy Badge
12-
15+
.. image:: https://badge.fury.io/py/codacy-coverage.svg
16+
:target: https://badge.fury.io/py/codacy-coverage
17+
:alt: PyPI version
18+
1319
Setup
1420
-----
1521

circle.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
machine:
2+
python:
3+
version: 2.7.10
4+
5+
dependencies:
6+
post:
7+
- sudo pip install pep8 --upgrade
8+
- sudo pip install pyflakes
9+
- sudo pip install coverage
10+
- sudo pip install virtualenv==12.0.2
11+
12+
test:
13+
override:
14+
- make test-all
15+
- make install
16+
- make coverage

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
'License :: OSI Approved :: MIT License',
3636

37-
'Programming Language :: Python :: 2',
37+
'Programming Language :: Python :: 2.7',
3838
'Programming Language :: Python :: 3',
3939
],
4040

src/codacy/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import reporter
1+
from __future__ import absolute_import
2+
from . import reporter
23

34

45
def main():

src/codacy/reporter.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
import os
77
from xml.dom import minidom
8-
98
import requests
109

1110
logging.basicConfig(level=logging.INFO,
@@ -20,11 +19,45 @@
2019
def get_git_revision_hash():
2120
import subprocess
2221

23-
return subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
22+
return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode("utf-8").strip()
23+
24+
25+
def get_git_directory():
26+
import subprocess
27+
28+
return subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).decode("utf-8").strip()
29+
30+
31+
def file_exists(rootdir, filename):
32+
for root, subFolders, files in os.walk(rootdir):
33+
if filename in files:
34+
return True
35+
else:
36+
for subFolder in subFolders:
37+
return file_exists(subFolder, filename)
38+
return False
39+
40+
41+
def generate_filename(sources, filename):
42+
def strip_prefix(line, prefix):
43+
if line.startswith(prefix):
44+
return line[len(prefix):]
45+
else:
46+
return line
47+
48+
git_directory = get_git_directory()
49+
50+
for source in sources:
51+
if file_exists(source, filename):
52+
return strip_prefix(source, git_directory).strip("/") + "/" + filename.strip("/")
53+
54+
return filename
2455

2556

2657
def parse_report_file(report_file):
27-
"""Parse XML file and POST it to the Codacy API"""
58+
"""Parse XML file and POST it to the Codacy API
59+
:param report_file:
60+
"""
2861

2962
# Convert decimal string to floored int percent value
3063
def percent(s):
@@ -39,10 +72,11 @@ def percent(s):
3972
'fileReports': [],
4073
}
4174

75+
sources = [x.firstChild.nodeValue for x in report_xml.getElementsByTagName('source')]
4276
classes = report_xml.getElementsByTagName('class')
4377
for cls in classes:
4478
file_report = {
45-
'filename': cls.attributes['filename'].value,
79+
'filename': generate_filename(sources, cls.attributes['filename'].value),
4680
'total': percent(cls.attributes['line-rate'].value),
4781
'coverage': {},
4882
}

tests.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@
44

55

66
class ReporterTests(unittest.TestCase):
7-
8-
def test_parser(self):
7+
def compare_parse_result(self, generated_filename, expected_filename):
98
def file_get_contents(filename):
109
with open(filename) as f:
1110
return f.read()
1211

13-
jsonContent = file_get_contents('tests/coverage.json')
14-
expected = json.loads(jsonContent)
12+
generated = codacy.reporter.parse_report_file(generated_filename)
13+
14+
json_content = file_get_contents(expected_filename)
15+
expected = json.loads(json_content)
1516

16-
generated = codacy.reporter.parse_report_file('tests/cobertura.xml')
1717
self.assertEqual(generated, expected)
1818

19+
def test_parser_coverage3(self):
20+
self.maxDiff = None
21+
22+
self.compare_parse_result('tests/coverage3/cobertura.xml', 'tests/coverage3/coverage.json')
23+
24+
def test_parser_coverage4(self):
25+
self.maxDiff = None
26+
27+
self.compare_parse_result('tests/coverage4/cobertura.xml', 'tests/coverage4/coverage.json')
28+
29+
def test_parser_git_filepath(self):
30+
self.maxDiff = None
31+
32+
self.compare_parse_result('tests/filepath/cobertura.xml', 'tests/filepath/coverage.json')
33+
34+
1935
if __name__ == '__main__':
2036
unittest.main()

tests/coverage.json

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/coverage3/coverage.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"total": 87,
3+
"fileReports": [
4+
{
5+
"total": 87,
6+
"coverage": {
7+
"5": 1,
8+
"4": 1,
9+
"6": 2
10+
},
11+
"filename": "src/test/resources/TestSourceFile.scala"
12+
},
13+
{
14+
"total": 87,
15+
"coverage": {
16+
"9": 1,
17+
"10": 1
18+
},
19+
"filename": "src/test/resources/TestSourceFile.scala"
20+
},
21+
{
22+
"total": 87,
23+
"coverage": {
24+
"1": 1,
25+
"3": 1,
26+
"2": 1
27+
},
28+
"filename": "src/test/resources/TestSourceFile2.scala"
29+
}
30+
],
31+
"language": "python"
32+
}

0 commit comments

Comments
 (0)