diff options
| author | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-28 17:56:57 +0000 |
|---|---|---|
| committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2016-07-28 17:56:57 +0000 |
| commit | ebc7ffd4e78e8f010c7ab12f2989fd614ef522d3 (patch) | |
| tree | b4cf3736ae8edba597dd7ca6e63a32b418b112a5 | |
| parent | 53455fdff7e89f83d811feffcdd5238b84113d63 (diff) | |
| parent | ad3b4869095dc589c4c72982a101e6bdd6b5c87e (diff) | |
| download | flake8-ebc7ffd4e78e8f010c7ab12f2989fd614ef522d3.tar.gz | |
Merge branch 'fix-report-ordering' into 'master'
Sort reports by line and column
*Description of changes*
The reports have been incorrectly sorted. This fixes the sorting order to use the line number and then column. It also adds a test to verify that the sorting algorithm works.
*Related to:* #196
See merge request !100
| -rw-r--r-- | src/flake8/checker.py | 2 | ||||
| -rw-r--r-- | tests/integration/test_checker.py | 68 |
2 files changed, 69 insertions, 1 deletions
diff --git a/src/flake8/checker.py b/src/flake8/checker.py index e7a94d8..d331b0c 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -306,7 +306,7 @@ class Manager(object): """ results_reported = results_found = 0 for checker in self.checkers: - results = sorted(checker.results, key=lambda tup: (tup[2], tup[3])) + results = sorted(checker.results, key=lambda tup: (tup[1], tup[2])) results_reported += self._handle_results(checker.display_name, results) results_found += len(results) diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index 2a145e7..5c013b6 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -76,3 +76,71 @@ def test_handle_file_plugins(plugin_target): line_number=EXPECTED_REPORT[0], column=EXPECTED_REPORT[1], text=EXPECTED_REPORT[2]) + + +PLACEHOLDER_CODE = 'some_line = "of" * code' + + +@pytest.mark.parametrize('results, expected_order', [ + # No entries should be added + ([], []), + # Results are correctly ordered + ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1]), + # Reversed order of lines + ([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0]), + # Columns are not ordered correctly (when reports are ordered correctly) + ([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0, 2]), + ([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE)], [1, 2, 0]), + ([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 2, 1]), + ([('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 3, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]), + ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]), + # Previously sort column and message (so reversed) (see bug 196) + ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE), + ('A101', 2, 1, 'charlie error', PLACEHOLDER_CODE)], [0, 1]), +]) +def test_report_order(results, expected_order): + """ + Test in which order the results will be reported. + + It gets a list of reports from the file checkers and verifies that the + result will be ordered independent from the original report. + """ + def count_side_effect(name, sorted_results): + """Side effect for the result handler to tell all are reported.""" + return len(sorted_results) + + # To simplify the parameters (and prevent copy & pasting) reuse report + # tuples to create the expected result lists from the indexes + expected_results = [results[index] for index in expected_order] + + file_checker = mock.Mock(spec=['results', 'display_name']) + file_checker.results = results + file_checker.display_name = 'placeholder' + + style_guide = mock.Mock(spec=['options']) + + # Create a placeholder manager without arguments or plugins + # Just add one custom file checker which just provides the results + manager = checker.Manager(style_guide, [], []) + manager.checkers = [file_checker] + + # _handle_results is the first place which gets the sorted result + # Should something non-private be mocked instead? + handler = mock.Mock() + handler.side_effect = count_side_effect + manager._handle_results = handler + + assert manager.report() == (len(results), len(results)) + handler.assert_called_once_with('placeholder', expected_results) |
