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

Commit ee17b7d

Browse files
committed
Add --directory override to specify top level git directory.
Currently, the coverage reporter invokes git to determine the base directory of the git repository. If CI is being run inside a docker container, best practices include not copying the VCS information into the container to reduce the size. However, if that advice is followed, the coverage reporter is then unable to invoke git successfully for this use case. To support running without git, this adds a command line flag to manually specify the base directory without invoking git. This has the side benefit of allowing the coverage reporter to be useful for other VCS systems as well.
1 parent c4610f9 commit ee17b7d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/codacy/reporter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ def file_exists(rootdir, filename):
5959
return False
6060

6161

62-
def generate_filename(sources, filename):
62+
def generate_filename(sources, filename, git_directory):
6363
def strip_prefix(line, prefix):
6464
if line.startswith(prefix):
6565
return line[len(prefix):]
6666
else:
6767
return line
6868

69-
git_directory = get_git_directory()
69+
if not git_directory:
70+
git_directory = get_git_directory()
7071

7172
for source in sources:
7273
if file_exists(source, filename):
@@ -75,7 +76,7 @@ def strip_prefix(line, prefix):
7576
return filename
7677

7778

78-
def parse_report_file(report_file):
79+
def parse_report_file(report_file, git_directory):
7980
"""Parse XML file and POST it to the Codacy API
8081
:param report_file:
8182
"""
@@ -97,7 +98,7 @@ def percent(s):
9798
classes = report_xml.getElementsByTagName('class')
9899
for cls in classes:
99100
file_report = {
100-
'filename': generate_filename(sources, cls.attributes['filename'].value),
101+
'filename': generate_filename(sources, cls.attributes['filename'].value, git_directory),
101102
'total': percent(cls.attributes['line-rate'].value),
102103
'coverage': {},
103104
}
@@ -141,6 +142,7 @@ def run():
141142
parser = argparse.ArgumentParser(description='Codacy coverage reporter for Python.')
142143
parser.add_argument("-r", "--report", type=str, help="coverage report file", default=DEFAULT_REPORT_FILE)
143144
parser.add_argument("-c", "--commit", type=str, help="git commit hash")
145+
parser.add_argument("-d", "--directory", type=str, help="git top level directory")
144146
parser.add_argument("-v", "--verbose", help="show debug information", action="store_true")
145147

146148
args = parser.parse_args()
@@ -160,7 +162,7 @@ def run():
160162
exit(1)
161163

162164
logging.info("Parsing report file...")
163-
report = parse_report_file(args.report)
165+
report = parse_report_file(args.report, args.directory)
164166

165167
logging.info("Uploading report...")
166168
upload_report(report, CODACY_PROJECT_TOKEN, args.commit)

tests/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def file_get_contents(filename):
2020
with open(filename) as f:
2121
return f.read()
2222

23-
generated = codacy.reporter.parse_report_file(generated_filename)
23+
generated = codacy.reporter.parse_report_file(generated_filename, '')
2424

2525
json_content = file_get_contents(expected_filename)
2626
expected = json.loads(json_content)

0 commit comments

Comments
 (0)