Skip to content

Commit

Permalink
Refactor. Add test that was previously not-covered.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Patterson committed Jun 1, 2015
1 parent faa0820 commit ec1dfb8
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions diff_cover/tests/test_violations_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,10 @@ def setUp(self):
# Mock patch the installation of jshint
_mock_command_simple = patch.object(JsHintQualityReporter, '_run_command_simple').start()
_mock_command_simple.return_value = 0
self._mock_communicate = patch.object(subprocess, 'Popen').start()
self.subproc_mock = MagicMock()
self.subproc_mock.returncode = 0


def tearDown(self):
"""
Expand All @@ -996,15 +1000,13 @@ def test_quality(self):
"""

# Patch the output of `jshint`
_mock_popen_init = patch.object(Popen, '__init__').start()
_mock_popen_init.return_value = None
_mock_communicate = patch.object(Popen, 'communicate').start()
return_string = '\n' + dedent("""
../test_file.js: line 3, col 9, Missing "use strict" statement.
../test_file.js: line 10, col 17, '$hi' is defined but never used.
""").strip() + '\n'
_mock_communicate.return_value = (
self.subproc_mock.communicate.return_value = (
(return_string.encode('utf-8'), b''))
self._mock_communicate.return_value = self.subproc_mock

# Parse the report
quality = JsHintQualityReporter('jshint', [])
Expand All @@ -1027,10 +1029,8 @@ def test_quality(self):
def test_no_quality_issues_newline(self):

# Patch the output of `jshint`
_mock_popen_init = patch.object(Popen, '__init__').start()
_mock_popen_init.return_value = None
_mock_communicate = patch.object(Popen, 'communicate').start()
_mock_communicate.return_value = (b'\n', b'')
self.subproc_mock.communicate.return_value = (b'\n', b'')
self._mock_communicate.return_value = self.subproc_mock

# Parse the report
quality = JsHintQualityReporter('jshint', [])
Expand All @@ -1039,23 +1039,21 @@ def test_no_quality_issues_newline(self):
def test_no_quality_issues_emptystring(self):

# Patch the output of `jshint`
_mock_popen_init = patch.object(Popen, '__init__').start()
_mock_popen_init.return_value = None
_mock_communicate = patch.object(Popen, 'communicate').start()
_mock_communicate.return_value = (b'', b'')
self.subproc_mock.communicate.return_value = (b'', b'')
self._mock_communicate.return_value = self.subproc_mock

# Parse the report
quality = JsHintQualityReporter('jshint', [])
self.assertEqual([], quality.violations('file1.js'))

def test_quality_error(self):

# Override the subprocess return code to a failure
self.subproc_mock.returncode = 1

# Patch the output of `jshint`
_mock_communicate = patch.object(subprocess, 'Popen').start()
subproc_mock = MagicMock()
subproc_mock.returncode = 1
subproc_mock.communicate.return_value = (b"", 'whoops Ƕئ'.encode('utf-8'))
_mock_communicate.return_value = subproc_mock
self.subproc_mock.communicate.return_value = (b"", 'whoops Ƕئ'.encode('utf-8'))
self._mock_communicate.return_value = self.subproc_mock

# Parse the report
quality = JsHintQualityReporter('jshint', [])
Expand Down Expand Up @@ -1121,8 +1119,19 @@ def test_quality_pregenerated_report(self):
for expected in expected_violations:
self.assertIn(expected, actual_violations)

def test_not_installed(self):
"""
If jshint is not available via commandline, it should raise an EnvironmentError
"""
_mock_command_simple = patch.object(JsHintQualityReporter, '_run_command_simple').start()
_mock_command_simple.return_value = 1
with self.assertRaises(EnvironmentError):
JsHintQualityReporter('jshint', [])

class SubprocessErrorTestCase(unittest.TestCase):
"""
Error in subprocess call(s)
"""
def setUp(self):
# when you create a new subprocess.Popen() object and call .communicate()
# on it, raise an OSError
Expand Down

0 comments on commit ec1dfb8

Please sign in to comment.