Skip to content

Commit

Permalink
Merge pull request #49 from Bachmann1234/issue-47
Browse files Browse the repository at this point in the history
Fix issue 47. and 48 Actually use all the files.... and dont run over deleted ones
  • Loading branch information
Bachmann1234 committed Aug 13, 2016
2 parents 0c593e8 + 552bfe4 commit da0c5e6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
14 changes: 14 additions & 0 deletions diff_cover/tests/fixtures/git_diff_violations_two_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/hello.py b/hello.py
index b732142..b2ba069 100644
--- a/hello.py
+++ b/hello.py
@@ -1 +1,2 @@
print "hello"
+print unknown_var
diff --git a/hi.py b/hi.py
index bae1109..151d05d 100644
--- a/hi.py
+++ b/hi.py
@@ -1 +1,2 @@
print "hi"
+print unknown_var
2 changes: 2 additions & 0 deletions diff_cover/tests/fixtures/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("hello")
print(unknown_var)
2 changes: 2 additions & 0 deletions diff_cover/tests/fixtures/hi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("hello")
print(unknown_var)
14 changes: 14 additions & 0 deletions diff_cover/tests/fixtures/pyflakes_two_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-------------
Diff Quality
Quality Report: pyflakes
Diff: origin/master...HEAD, staged, and unstaged changes
-------------
hello.py (0.0%):
2: undefined name 'unknown_var'
hi.py (0.0%):
2: undefined name 'unknown_var'
-------------
Total: 2 lines
Violations: 2 lines
% Quality: 0%
-------------
7 changes: 7 additions & 0 deletions diff_cover/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ def test_added_file_pyflakes_console(self):
['diff-quality', '--violations=pyflakes']
)

def test_added_file_pyflakes_console_two_files(self):
self._check_console_report(
'git_diff_violations_two_files.txt',
'pyflakes_two_files.txt',
['diff-quality', '--violations=pyflakes']
)

def test_added_file_pylint_console(self):
self._check_console_report(
'git_diff_violations.txt',
Expand Down
29 changes: 27 additions & 2 deletions diff_cover/tests/test_violations_reporter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
import subprocess
import xml.etree.cElementTree as etree
from subprocess import Popen
from textwrap import dedent
import mock

import six
from diff_cover.violationsreporters import base

from diff_cover.command_runner import CommandError, run_command_for_code
from diff_cover.tests.helpers import unittest
Expand All @@ -18,6 +20,10 @@
from six import BytesIO, StringIO


def _patch_so_all_files_exist():
_mock_exists = patch.object(base.os.path, 'exists').start()
_mock_exists.returnvalue = True

def _setup_patch(return_value, status_code=0):
mocked_process = mock.Mock()
mocked_process.returncode = status_code
Expand Down Expand Up @@ -314,6 +320,9 @@ def _coverage_xml(

class Pep8QualityReporterTest(unittest.TestCase):

def setUp(self):
_patch_so_all_files_exist()

def tearDown(self):
"""
Undo all patches
Expand Down Expand Up @@ -450,6 +459,7 @@ def tearDown(self):
patch.stopall()

def test_quality(self):
_patch_so_all_files_exist()

# Patch the output of `pyflakes`
return_string = '\n' + dedent("""
Expand Down Expand Up @@ -494,6 +504,7 @@ def test_no_quality_issues_emptystring(self):
self.assertEqual([], quality.violations('file1.py'))

def test_quality_error(self):
_patch_so_all_files_exist()

# Patch the output of `pyflakes`
_setup_patch((b"", b'whoops'), status_code=1)
Expand Down Expand Up @@ -571,6 +582,7 @@ def tearDown(self):
patch.stopall()

def test_quality(self):
_patch_so_all_files_exist()

# Patch the output of `flake8`
return_string = '\n' + dedent("""
Expand Down Expand Up @@ -636,6 +648,7 @@ def test_no_quality_issues_emptystring(self):
self.assertEqual([], quality.violations('file1.py'))

def test_quality_error(self):
_patch_so_all_files_exist()

# Patch the output of `flake8`
_setup_patch((b"", 'whoops Ƕئ'.encode('utf-8')), status_code=1)
Expand Down Expand Up @@ -666,6 +679,14 @@ def test_no_python_file(self):
result = quality.violations(path)
self.assertEqual(result, [])

def test_file_does_not_exist(self):
quality = QualityReporter(flake8_driver)
file_paths = ['ajshdjlasdhajksdh.py']
# Expect that we get no results because that file does not exist
for path in file_paths:
result = quality.violations(path)
self.assertEqual(result, [])

def test_quality_pregenerated_report(self):

# When the user provides us with a pre-generated flake8 report
Expand Down Expand Up @@ -708,6 +729,9 @@ def test_quality_pregenerated_report(self):

class PylintQualityReporterTest(unittest.TestCase):

def setUp(self):
_patch_so_all_files_exist()

def tearDown(self):
"""
Undo all patches.
Expand Down Expand Up @@ -949,7 +973,7 @@ def test_quality(self):
"""
Test basic scenarios, including special characters that would appear in JavaScript and mixed quotation marks
"""

_patch_so_all_files_exist()
# Patch the output of the linter cmd
return_string = '\n' + dedent("""
../test_file.js: line 3, col 9, Missing "use strict" statement.
Expand Down Expand Up @@ -998,7 +1022,7 @@ def test_no_quality_issues_emptystring(self):
self.assertEqual([], quality.violations('file1.js'))

def test_quality_error(self):

_patch_so_all_files_exist()
_setup_patch((b"", 'whoops Ƕئ'.encode('utf-8')), status_code=1)
with patch('diff_cover.violationsreporters.base.run_command_for_code') as code:
code.return_value = 0
Expand Down Expand Up @@ -1141,6 +1165,7 @@ def setUp(self):

@patch('sys.stderr', new_callable=StringIO)
def test_quality_reporter(self, mock_stderr):
_patch_so_all_files_exist()
with patch('diff_cover.violationsreporters.base.run_command_for_code') as code:
code.return_value = 0
reporter = QualityReporter(pep8_driver)
Expand Down
15 changes: 9 additions & 6 deletions diff_cover/violationsreporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(self, driver, reports=None, options=None):
"""
super(QualityReporter, self).__init__(driver.name)
self.reports = self._load_reports(reports) if reports else None
self.violations_dict = {}
self.violations_dict = defaultdict(list)
self.driver = driver
self.options = options
self.driver_tool_installed = None
Expand Down Expand Up @@ -142,18 +142,21 @@ def violations(self, src_path):
if not any(src_path.endswith(ext) for ext in self.driver.supported_extensions):
return []
if src_path not in self.violations_dict:
if not self.reports:
if self.reports:
self.violations_dict = self.driver.parse_reports(self.reports)
else:
if self.driver_tool_installed is None:
self.driver_tool_installed = self.driver.installed()
if not self.driver_tool_installed:
raise EnvironmentError("{0} is not installed".format(self.driver.name))
command = copy.deepcopy(self.driver.command)
if self.options:
command.append(self.options)
command.append(src_path.encode(sys.getfilesystemencoding()))
output, _ = execute(command)
self.reports = [output]
self.violations_dict = self.driver.parse_reports(self.reports)
if os.path.exists(src_path):
command.append(src_path.encode(sys.getfilesystemencoding()))
output, _ = execute(command)
self.violations_dict.update(self.driver.parse_reports([output]))

return self.violations_dict[src_path]

def measured_lines(self, src_path):
Expand Down

0 comments on commit da0c5e6

Please sign in to comment.