diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2016-06-09 22:56:52 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-06-09 22:56:52 -0400 |
commit | 8493f3b8e440bab6f8a24df8257093d977e38482 (patch) | |
tree | 3f5aa00551ec59be4f84be8f7a238be75410ff25 | |
parent | 83aa28fa8236b9dc2f1f0154e9962b02fe49a50e (diff) | |
download | python-coveragepy-8493f3b8e440bab6f8a24df8257093d977e38482.tar.gz |
Properly handle invalid sorting option
-rw-r--r-- | coverage/summary.py | 2 | ||||
-rw-r--r-- | tests/test_summary.py | 15 |
2 files changed, 14 insertions, 3 deletions
diff --git a/coverage/summary.py b/coverage/summary.py index 95e78c3..b0fa71a 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -117,6 +117,8 @@ class SummaryReporter(Reporter): # Sort the lines and write them out. if getattr(self.config, 'sort', None): position = column_order.get(self.config.sort.lower()) + if position is None: + raise CoverageException("Invalid sorting option: {0!r}".format(self.config.sort)) lines.sort(key=lambda l: (l[1][position], l[0])) for line in lines: diff --git a/tests/test_summary.py b/tests/test_summary.py index 35fbfb7..97171b5 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -17,7 +17,7 @@ from coverage.backward import StringIO from coverage.config import CoverageConfig from coverage.control import Coverage from coverage.data import CoverageData -from coverage.misc import output_encoding +from coverage.misc import CoverageException, output_encoding from coverage.summary import SummaryReporter from tests.coveragetest import CoverageTest @@ -696,8 +696,8 @@ class TestSummaryReporterConfiguration(CoverageTest): self.assertEqual(len(lines), 3) nums = [list(map(int, l.replace('%', '').split()[1:])) for l in lines] # [ - # [339, 155, 54], - # [ 13, 3, 77], + # [339, 155, 54], + # [ 13, 3, 77], # [234, 228, 3] # ] self.assertTrue(nums[1][0] < nums[2][0] < nums[0][0]) @@ -744,3 +744,12 @@ class TestSummaryReporterConfiguration(CoverageTest): opts.from_args(sort='Cover') report = self.get_summary_text(data, opts) self.assert_ordering(report, "test_coverage.py", "test_api.py", "test_backward.py") + + def test_sort_report_by_invalid_option(self): + # Sort the text report by a nonsense column. + data = self.get_coverage_data(self.LINES_1) + opts = CoverageConfig() + opts.from_args(sort='Xyzzy') + msg = "Invalid sorting option: 'Xyzzy'" + with self.assertRaisesRegex(CoverageException, msg): + self.get_summary_text(data, opts) |