def test_quality(self): # Patch the output of `pyflakes` _mock_communicate = patch.object(Popen, 'communicate').start() return_string = '\n' + dedent(""" ../new_file.py:328: undefined name '_thing' ../new_file.py:418: 'random' imported but unused """).strip() + '\n' _mock_communicate.return_value = ( (return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(pyflakes_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pyflakes') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(328, "undefined name '_thing'"), Violation(418, "'random' imported but unused") ] self.assertEqual( expected_violations, quality.violations('../new_file.py'))
def test_quality(self): """ Test basic scenarios, including special characters that would appear in JavaScript and mixed quotation marks """ # Patch the output of the linter cmd 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' self.subproc_mock.communicate.return_value = (( return_string.encode('utf-8'), b'')) self._mock_communicate.return_value = self.subproc_mock # Parse the report quality = QualityReporter(self._get_out()) # Expect that the name is set self.assertEqual(quality.name(), self.quality_name) # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../blah.js'), None) # Expect that we get the right violations expected_violations = [ Violation(3, 'Missing "use strict" statement.'), Violation(10, "'$hi' is defined but never used."), ] self.assertEqual(expected_violations, quality.violations('../test_file.js'))
def test_quality(self): """Integration test.""" # Patch the output of `pydocstye` _setup_patch((dedent(""" ../new_file.py:1 at module level: D100: Missing docstring in public module ../new_file.py:13 in public function `gather`: D103: Missing docstring in public function """).strip().encode('ascii'), '')) expected_violations = [ Violation(1, 'D100: Missing docstring in public module'), Violation(13, "D103: Missing docstring in public function"), ] # Parse the report quality = QualityReporter(pydocstyle_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pydocstyle') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get violations for file1.py only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('../new_file.py') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality(self): # Patch the output of `pep8` _mock_communicate = patch.object(Popen, 'communicate').start() return_string = '\n' + dedent(""" ../new_file.py:1:17: E231 whitespace ../new_file.py:3:13: E225 whitespace ../new_file.py:7:1: E302 blank lines """).strip() + '\n' _mock_communicate.return_value = ( (return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(pep8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pep8') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(1, 'E231 whitespace'), Violation(3, 'E225 whitespace'), Violation(7, 'E302 blank lines') ] self.assertEqual(expected_violations, quality.violations('../new_file.py'))
def test_quality(self): """Integration test.""" # Patch the output of `pydocstye` _setup_patch(( dedent(""" ../new_file.py:1 at module level: D100: Missing docstring in public module ../new_file.py:13 in public function `gather`: D103: Missing docstring in public function """).strip().encode('ascii'), '' )) expected_violations = [ Violation(1, 'D100: Missing docstring in public module'), Violation(13, "D103: Missing docstring in public function"), ] # Parse the report quality = QualityReporter(pydocstyle_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pydocstyle') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get violations for file1.py only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('../new_file.py') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality(self): # Patch the output of `pyflakes` return_string = '\n' + dedent(""" ../new_file.py:328: undefined name '_thing' ../new_file.py:418: 'random' imported but unused """).strip() + '\n' _setup_patch((return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(pyflakes_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pyflakes') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(328, "undefined name '_thing'"), Violation(418, "'random' imported but unused") ] self.assertEqual(expected_violations, quality.violations('../new_file.py'))
def test_quality(self): # Patch the output of `pep8` _mock_communicate = patch.object(Popen, 'communicate').start() return_string = '\n' + dedent(""" ../new_file.py:1:17: E231 whitespace ../new_file.py:3:13: E225 whitespace ../new_file.py:7:1: E302 blank lines """).strip() + '\n' _setup_patch((return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(pep8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pep8') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(1, 'E231 whitespace'), Violation(3, 'E225 whitespace'), Violation(7, 'E302 blank lines') ] self.assertEqual(expected_violations, quality.violations('../new_file.py'))
def test_quality(self, process_patcher): """Integration test.""" # Patch the output of `checkstyle` process_patcher(( dedent(""" [WARN] ../new_file.java:1:1: Line contains a tab character. [WARN] ../new_file.java:13: 'if' construct must use '{}'s. """).strip().encode("ascii"), "", )) expected_violations = [ Violation(1, "Line contains a tab character."), Violation(13, "'if' construct must use '{}'s."), ] # Parse the report quality = QualityReporter(checkstyle_driver) # Expect that the name is set assert quality.name() == "checkstyle" # Measured_lines is undefined for a # quality reporter since all lines are measured assert not quality.measured_lines("../new_file.java") # Expect that we get violations for file1.java only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations("../new_file.java") assert len(actual_violations) == len(expected_violations) for expected in expected_violations: assert expected in actual_violations
def test_quality(self): """Integration test.""" # Patch the output of `checkstyle` _setup_patch(( dedent(""" [WARN] ../new_file.java:1:1: Line contains a tab character. [WARN] ../new_file.java:13: 'if' construct must use '{}'s. """).strip().encode('ascii'), '' )) expected_violations = [ Violation(1, 'Line contains a tab character.'), Violation(13, "'if' construct must use '{}'s."), ] # Parse the report quality = QualityReporter(checkstyle_driver) # Expect that the name is set self.assertEqual(quality.name(), 'checkstyle') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.java'), None) # Expect that we get violations for file1.java only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('../new_file.java') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality(self): """ Test basic scenarios, including special characters that would appear in JavaScript and mixed quotation marks """ # Patch the output of the linter cmd 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' self.subproc_mock.communicate.return_value = ( (return_string.encode('utf-8'), b'')) self._mock_communicate.return_value = self.subproc_mock # Parse the report quality = QualityReporter(self._get_out()) # Expect that the name is set self.assertEqual(quality.name(), self.quality_name) # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../blah.js'), None) # Expect that we get the right violations expected_violations = [ Violation(3, 'Missing "use strict" statement.'), Violation(10, "'$hi' is defined but never used."), ] self.assertEqual(expected_violations, quality.violations('../test_file.js'))
def test_quality(self): # Patch the output of `checkstyle` _setup_patch( ( dedent( """ <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="file1.java"> <error line="1" severity="error" message="Missing docstring"/> <error line="2" severity="error" message="Unused variable 'd'"/> <error line="2" severity="warning" message="TODO: Not the real way we'll store usages!"/> <error line="579" severity="error" message="Unable to import 'rooted_paths'"/> <error line="113" severity="error" message="Unused argument 'cls'"/> <error line="150" severity="error" message="error while code parsing ([Errno 2] No such file or directory)"/> <error line="149" severity="error" message="Comma not followed by a space"/> </file> <file name="path/to/file2.java"> <error line="100" severity="error" message="Access to a protected member"/> </file> </checkstyle> """ ) .strip() .encode("ascii"), "", ) ) expected_violations = [ Violation(1, "error: Missing docstring"), Violation(2, "error: Unused variable 'd'"), Violation(2, "warning: TODO: Not the real way we'll store usages!"), Violation(579, "error: Unable to import 'rooted_paths'"), Violation( 150, "error: error while code parsing ([Errno 2] No such file or directory)", ), Violation(149, "error: Comma not followed by a space"), Violation(113, "error: Unused argument 'cls'"), ] # Parse the report quality = QualityReporter(CheckstyleXmlDriver()) # Expect that the name is set self.assertEqual(quality.name(), "checkstyle") # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertIsNone(quality.measured_lines("file1.java")) # Expect that we get violations for file1.java only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations("file1.java") self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality(self): # Patch the output of `pylint` _setup_patch((dedent(""" file1.py:1: [C0111] Missing docstring file1.py:1: [C0111, func_1] Missing docstring file1.py:2: [W0612, cls_name.func] Unused variable 'd' file1.py:2: [W0511] TODO: Not the real way we'll store usages! file1.py:579: [F0401] Unable to import 'rooted_paths' file1.py:113: [W0613, cache_relation.clear_pk] Unused argument 'cls' file1.py:150: [F0010] error while code parsing ([Errno 2] No such file or directory) file1.py:149: [C0324, Foo.__dict__] Comma not followed by a space self.peer_grading._find_corresponding_module_for_location(Location('i4x','a','b','c','d')) file1.py:162: [R0801] Similar lines in 2 files ==file1:162 ==student.views:4 import json import logging import random path/to/file2.py:100: [W0212, openid_login_complete] Access to a protected member """).strip().encode('ascii'), '')) expected_violations = [ Violation(1, 'C0111: Missing docstring'), Violation(1, 'C0111: func_1: Missing docstring'), Violation(2, "W0612: cls_name.func: Unused variable 'd'"), Violation(2, "W0511: TODO: Not the real way we'll store usages!"), Violation(579, "F0401: Unable to import 'rooted_paths'"), Violation( 150, "F0010: error while code parsing ([Errno 2] No such file or directory)" ), Violation(149, "C0324: Foo.__dict__: Comma not followed by a space"), Violation(162, "R0801: Similar lines in 2 files"), Violation(113, "W0613: cache_relation.clear_pk: Unused argument 'cls'") ] # Parse the report quality = QualityReporter(PylintDriver()) # Expect that the name is set self.assertEqual(quality.name(), 'pylint') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('file1.py'), None) # Expect that we get violations for file1.py only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('file1.py') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality_error(self): # Patch the output of `pyflakes` _setup_patch((b"", b'whoops'), status_code=1) # Parse the report quality = QualityReporter(pyflakes_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pyflakes') self.assertRaises(CommandError, quality.violations, 'file1.py')
def test_quality_error(self): _setup_patch((b"", 'whoops Ƕئ'.encode('utf-8')), status_code=1) # Parse the report quality = QualityReporter(self._get_out()) # Expect that the name is set self.assertEqual(quality.name(), self.quality_name) with self.assertRaises(CommandError) as ex: quality.violations('file1.js') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
def test_quality(self): # Patch the output of `pylint` _setup_patch(( dedent(""" file1.py:1: [C0111] Missing docstring file1.py:1: [C0111, func_1] Missing docstring file1.py:2: [W0612, cls_name.func] Unused variable 'd' file1.py:2: [W0511] TODO: Not the real way we'll store usages! file1.py:579: [F0401] Unable to import 'rooted_paths' file1.py:113: [W0613, cache_relation.clear_pk] Unused argument 'cls' file1.py:150: [F0010] error while code parsing ([Errno 2] No such file or directory) file1.py:149: [C0324, Foo.__dict__] Comma not followed by a space self.peer_grading._find_corresponding_module_for_location(Location('i4x','a','b','c','d')) file1.py:162: [R0801] Similar lines in 2 files ==file1:162 ==student.views:4 import json import logging import random path/to/file2.py:100: [W0212, openid_login_complete] Access to a protected member """).strip().encode('ascii'), '' )) expected_violations = [ Violation(1, 'C0111: Missing docstring'), Violation(1, 'C0111: func_1: Missing docstring'), Violation(2, "W0612: cls_name.func: Unused variable 'd'"), Violation(2, "W0511: TODO: Not the real way we'll store usages!"), Violation(579, "F0401: Unable to import 'rooted_paths'"), Violation(150, "F0010: error while code parsing ([Errno 2] No such file or directory)"), Violation(149, "C0324: Foo.__dict__: Comma not followed by a space"), Violation(162, "R0801: Similar lines in 2 files"), Violation(113, "W0613: cache_relation.clear_pk: Unused argument 'cls'") ] # Parse the report quality = QualityReporter(PylintDriver()) # Expect that the name is set self.assertEqual(quality.name(), 'pylint') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('file1.py'), None) # Expect that we get violations for file1.py only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('file1.py') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality_error(self): # Patch the output of `flake8` _setup_patch((b"", 'whoops Ƕئ'.encode('utf-8')), status_code=1) # Parse the report quality = QualityReporter(flake8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'flake8') with self.assertRaises(CommandError) as ex: quality.violations('file1.py') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
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 # Parse the report quality = QualityReporter(self._get_out()) # Expect that the name is set self.assertEqual(quality.name(), self.quality_name) with self.assertRaises(CommandError) as ex: quality.violations('file1.js') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
def test_quality_error(self): # Patch the output of `pyflakes` _setup_patch((b"", b'whoops'), status_code=1) with patch('diff_cover.violationsreporters.base.run_command_for_code') as code: code.return_value = 0 quality = QualityReporter(pyflakes_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pyflakes') self.assertRaises(CommandError, quality.violations, 'file1.py')
def test_quality(self): _patch_so_all_files_exist() # Patch the output of `flake8` return_string = '\n' + dedent(""" ../new_file.py:1:17: E231 whitespace ../new_file.py:3:13: E225 whitespace ../new_file.py:7:1: E302 blank lines ../new_file.py:8:1: W191 indentation contains tabs ../new_file.py:10:1: F841 local variable name is assigned to but never used ../new_file.py:20:1: C901 'MyModel.mymethod' is too complex (14) ../new_file.py:50:1: N801 class names should use CapWords convention ../new_file.py:60:10: T000 Todo note found. ../new_file.py:70:0: I100 statements are in the wrong order. ../new_file.py:80:0: B901 blind except: statement ../new_file.py:90:0: D207 Docstring is under-indented ../new_file.py:100:0: S100 Snippet found ../new_file.py:110:0: Q000 Remove Single quotes """).strip() + '\n' _setup_patch((return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(flake8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'flake8') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(1, 'E231 whitespace'), Violation(3, 'E225 whitespace'), Violation(7, 'E302 blank lines'), Violation(8, 'W191 indentation contains tabs'), Violation( 10, 'F841 local variable name is assigned to but never used'), Violation(20, "C901 'MyModel.mymethod' is too complex (14)"), Violation(50, 'N801 class names should use CapWords convention'), Violation(60, 'T000 Todo note found.'), Violation(70, 'I100 statements are in the wrong order.'), Violation(80, 'B901 blind except: statement'), Violation(90, 'D207 Docstring is under-indented'), Violation(100, 'S100 Snippet found'), Violation(110, 'Q000 Remove Single quotes'), ] self.assertEqual(expected_violations, quality.violations('../new_file.py'))
def test_quality_error(self): # Patch the output of `pyflakes` _setup_patch((b"", b'whoops'), status_code=1) with patch('diff_cover.violationsreporters.base.run_command_for_code' ) as code: code.return_value = 0 quality = QualityReporter(pyflakes_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pyflakes') self.assertRaises(CommandError, quality.violations, 'file1.py')
def test_quality_error(self): # Patch the output of `pycodestyle` _setup_patch((b"", 'whoops Ƕئ'.encode('utf-8')), status_code=255) with patch('diff_cover.violationsreporters.base.run_command_for_code') as code: code.return_value = 0 # Parse the report quality = QualityReporter(pycodestyle_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pycodestyle') with self.assertRaises(CommandError) as ex: quality.violations('file1.py') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
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 # Parse the report quality = QualityReporter(self._get_out()) # Expect that the name is set self.assertEqual(quality.name(), self.quality_name) with self.assertRaises(CommandError) as ex: quality.violations('file1.js') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
def test_quality(self): # Patch the output of `flake8` _mock_communicate = patch.object(Popen, 'communicate').start() return_string = '\n' + dedent(""" ../new_file.py:1:17: E231 whitespace ../new_file.py:3:13: E225 whitespace ../new_file.py:7:1: E302 blank lines ../new_file.py:8:1: W191 indentation contains tabs ../new_file.py:10:1: F841 local variable name is assigned to but never used ../new_file.py:20:1: C901 'MyModel.mymethod' is too complex (14) ../new_file.py:50:1: N801 class names should use CapWords convention ../new_file.py:60:10: T000 Todo note found. ../new_file.py:70:0: I100 statements are in the wrong order. ../new_file.py:80:0: B901 blind except: statement ../new_file.py:90:0: D207 Docstring is under-indented ../new_file.py:100:0: S100 Snippet found ../new_file.py:110:0: Q000 Remove Single quotes """).strip() + '\n' _mock_communicate.return_value = ( (return_string.encode('utf-8'), b'')) # Parse the report quality = QualityReporter(flake8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'flake8') # Measured_lines is undefined for # a quality reporter since all lines are measured self.assertEqual(quality.measured_lines('../new_file.py'), None) # Expect that we get the right violations expected_violations = [ Violation(1, 'E231 whitespace'), Violation(3, 'E225 whitespace'), Violation(7, 'E302 blank lines'), Violation(8, 'W191 indentation contains tabs'), Violation(10, 'F841 local variable name is assigned to but never used'), Violation(20, "C901 'MyModel.mymethod' is too complex (14)"), Violation(50, 'N801 class names should use CapWords convention'), Violation(60, 'T000 Todo note found.'), Violation(70, 'I100 statements are in the wrong order.'), Violation(80, 'B901 blind except: statement'), Violation(90, 'D207 Docstring is under-indented'), Violation(100, 'S100 Snippet found'), Violation(110, 'Q000 Remove Single quotes'), ] self.assertEqual(expected_violations, quality.violations('../new_file.py'))
def test_quality(self): # Patch the output of `checkstyle` _setup_patch(( dedent(""" <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="file1.java"> <error line="1" severity="error" message="Missing docstring"/> <error line="2" severity="error" message="Unused variable 'd'"/> <error line="2" severity="warning" message="TODO: Not the real way we'll store usages!"/> <error line="579" severity="error" message="Unable to import 'rooted_paths'"/> <error line="113" severity="error" message="Unused argument 'cls'"/> <error line="150" severity="error" message="error while code parsing ([Errno 2] No such file or directory)"/> <error line="149" severity="error" message="Comma not followed by a space"/> </file> <file name="path/to/file2.java"> <error line="100" severity="error" message="Access to a protected member"/> </file> </checkstyle> """).strip().encode('ascii'), '' )) expected_violations = [ Violation(1, 'error: Missing docstring'), Violation(2, "error: Unused variable 'd'"), Violation(2, "warning: TODO: Not the real way we'll store usages!"), Violation(579, "error: Unable to import 'rooted_paths'"), Violation(150, "error: error while code parsing ([Errno 2] No such file or directory)"), Violation(149, "error: Comma not followed by a space"), Violation(113, "error: Unused argument 'cls'") ] # Parse the report quality = QualityReporter(CheckstyleXmlDriver()) # Expect that the name is set self.assertEqual(quality.name(), 'checkstyle') # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertEqual(quality.measured_lines('file1.java'), None) # Expect that we get violations for file1.java only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations('file1.java') self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality_error(self): # Patch the output of `pep8` _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 # Parse the report quality = QualityReporter(pep8_driver) # Expect that the name is set self.assertEqual(quality.name(), 'pep8') with self.assertRaises(CommandError) as ex: quality.violations('file1.py') self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')