|
15 | 15 | from pyflakes.reporter import Reporter
|
16 | 16 | from pyflakes.api import (
|
17 | 17 | main,
|
| 18 | + check, |
18 | 19 | checkPath,
|
19 | 20 | checkRecursive,
|
20 | 21 | iterSourceCode,
|
@@ -255,6 +256,17 @@ def test_syntaxErrorNoOffset(self):
|
255 | 256 | "bad line of source\n"),
|
256 | 257 | err.getvalue())
|
257 | 258 |
|
| 259 | + def test_syntaxErrorNoText(self): |
| 260 | + """ |
| 261 | + C{syntaxError} doesn't include text or nonsensical offsets if C{text} is C{None}. |
| 262 | +
|
| 263 | + This typically happens when reporting syntax errors from stdin. |
| 264 | + """ |
| 265 | + err = io.StringIO() |
| 266 | + reporter = Reporter(None, err) |
| 267 | + reporter.syntaxError('<stdin>', 'a problem', 0, 0, None) |
| 268 | + self.assertEqual(("<stdin>:1:1: a problem\n"), err.getvalue()) |
| 269 | + |
258 | 270 | def test_multiLineSyntaxError(self):
|
259 | 271 | """
|
260 | 272 | If there's a multi-line syntax error, then we only report the last
|
@@ -606,7 +618,8 @@ def test_misencodedFileUTF8(self):
|
606 | 618 | """ % SNOWMAN).encode('utf-8')
|
607 | 619 | with self.makeTempFile(source) as sourcePath:
|
608 | 620 | self.assertHasErrors(
|
609 |
| - sourcePath, [f"{sourcePath}: problem decoding source\n"]) |
| 621 | + sourcePath, |
| 622 | + [f"{sourcePath}:1:1: 'ascii' codec can't decode byte 0xe2 in position 21: ordinal not in range(128)\n"]) # noqa: E501 |
610 | 623 |
|
611 | 624 | def test_misencodedFileUTF16(self):
|
612 | 625 | """
|
@@ -648,6 +661,43 @@ def test_checkRecursive(self):
|
648 | 661 | finally:
|
649 | 662 | shutil.rmtree(tempdir)
|
650 | 663 |
|
| 664 | + def test_stdinReportsErrors(self): |
| 665 | + """ |
| 666 | + L{check} reports syntax errors from stdin |
| 667 | + """ |
| 668 | + source = "max(1 for i in range(10), key=lambda x: x+1)\n" |
| 669 | + err = io.StringIO() |
| 670 | + count = withStderrTo(err, check, source, "<stdin>") |
| 671 | + self.assertEqual(count, 1) |
| 672 | + errlines = err.getvalue().split("\n")[:-1] |
| 673 | + |
| 674 | + if PYPY: |
| 675 | + expected_error = [ |
| 676 | + "<stdin>:1:3: Generator expression must be parenthesized if not sole argument", # noqa: E501 |
| 677 | + "max(1 for i in range(10), key=lambda x: x+1)", |
| 678 | + " ^", |
| 679 | + ] |
| 680 | + elif sys.version_info >= (3, 9): |
| 681 | + expected_error = [ |
| 682 | + "<stdin>:1:5: Generator expression must be parenthesized", |
| 683 | + "max(1 for i in range(10), key=lambda x: x+1)", |
| 684 | + " ^", |
| 685 | + ] |
| 686 | + elif sys.version_info >= (3, 8): |
| 687 | + expected_error = [ |
| 688 | + "<stdin>:1:5: Generator expression must be parenthesized", |
| 689 | + ] |
| 690 | + elif sys.version_info >= (3, 7): |
| 691 | + expected_error = [ |
| 692 | + "<stdin>:1:4: Generator expression must be parenthesized", |
| 693 | + ] |
| 694 | + elif sys.version_info >= (3, 6): |
| 695 | + expected_error = [ |
| 696 | + "<stdin>:1:4: Generator expression must be parenthesized if not sole argument", # noqa: E501 |
| 697 | + ] |
| 698 | + |
| 699 | + self.assertEqual(errlines, expected_error) |
| 700 | + |
651 | 701 |
|
652 | 702 | class IntegrationTests(TestCase):
|
653 | 703 | """
|
|
0 commit comments