Esempio n. 1
0
    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 = Pep8QualityReporter('pep8', [])

        # 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'))
Esempio n. 2
0
 def test_no_python_file(self):
     quality = Pep8QualityReporter('pep8', [])
     file_paths = ['file1.coffee', 'subdir/file2.js']
     # Expect that we get no results because no Python files
     for path in file_paths:
         result = quality.violations(path)
         self.assertEqual(result, [])
Esempio n. 3
0
    def test_no_quality_issues_emptystring(self):

        # Patch the output of `pep8`
        _mock_communicate = patch.object(Popen, 'communicate').start()
        _mock_communicate.return_value = (b'', b'')

        # Parse the report
        quality = Pep8QualityReporter('pep8', [])
        self.assertEqual([], quality.violations('file1.py'))
Esempio n. 4
0
    def test_quality_error(self):

        # Patch the output of `pep8`
        _mock_communicate = patch.object(Popen, 'communicate').start()
        _mock_communicate.return_value = (b"", b'whoops')

        # Parse the report
        quality = Pep8QualityReporter('pep8', [])

        # Expect that the name is set
        self.assertEqual(quality.name(), 'pep8')

        self.assertRaises(QualityReporterError, quality.violations, 'file1.py')
Esempio n. 5
0
    def test_quality_error(self):

        # Patch the output of `pep8`
        _mock_communicate = patch.object(Popen, 'communicate').start()
        _mock_communicate.return_value = (b"", 'whoops Ƕئ'.encode('utf-8'))

        # Parse the report
        quality = Pep8QualityReporter('pep8', [])

        # Expect that the name is set
        self.assertEqual(quality.name(), 'pep8')
        with self.assertRaises(QualityReporterError) as ex:
            quality.violations('file1.py')
        self.assertEqual(six.text_type(ex.exception), 'whoops Ƕئ')
    def test_quality_pregenerated_report(self):

        # Patch the output of `pep8`
        _mock_communicate = patch.object(Popen, 'communicate').start()
        _mock_communicate.return_value = ('', '')

        # When the user provides us with a pre-generated pep8 report
        # then use that instead of calling pep8 directly.
        pep8_reports = [
            StringIO('\n' + dedent("""
                path/to/file.py:1:17: E231 whitespace
                path/to/file.py:3:13: E225 whitespace
                another/file.py:7:1: E302 blank lines
            """).strip() + '\n'),
            StringIO('\n' + dedent(u"""
                path/to/file.py:24:2: W123 \u9134\u1912
                another/file.py:50:1: E302 blank lines
            """).strip() + '\n'),
        ]

        # Parse the report
        quality = Pep8QualityReporter('pep8', pep8_reports)

        # Measured_lines is undefined for
        # a quality reporter since all lines are measured
        self.assertEqual(quality.measured_lines('path/to/file.py'), None)

        # Expect that we get the right violations
        expected_violations = [
            Violation(1, u'E231 whitespace'),
            Violation(3, u'E225 whitespace'),
            Violation(24, u'W123 \u9134\u1912')
        ]

        # We're not guaranteed that the violations are returned
        # in any particular order.
        actual_violations = quality.violations('path/to/file.py')

        self.assertEqual(len(actual_violations), len(expected_violations))
        for expected in expected_violations:
            self.assertIn(expected, actual_violations)
Esempio n. 7
0
    def test_quality_reporter(self, mock_stderr):
        reporter = Pep8QualityReporter('pep8', [])
        with self.assertRaises(OSError):
            reporter.violations("path/to/file.py")

        self.assertEqual(mock_stderr.getvalue(), "pep8 path/to/file.py")
Esempio n. 8
0
    def test_no_such_file(self):
        quality = Pep8QualityReporter('pep8', [])

        # Expect that we get no results
        result = quality.violations('')
        self.assertEqual(result, [])