def test_checkRecursive(self): """ L{checkRecursive} descends into each directory, finding Python files and reporting problems. """ tempdir = tempfile.mkdtemp() try: os.mkdir(os.path.join(tempdir, "foo")) file1 = os.path.join(tempdir, "foo", "bar.py") with open(file1, "wb") as fd: fd.write("import baz\n".encode("ascii")) file2 = os.path.join(tempdir, "baz.py") with open(file2, "wb") as fd: fd.write("import contraband".encode("ascii")) log = [] reporter = LoggingReporter(log) warnings = checkRecursive([tempdir], reporter) self.assertEqual(warnings, 2) self.assertEqual( sorted(log), sorted([ ("flake", str(UnusedImport(file1, Node(1), "baz"))), ("flake", str(UnusedImport(file2, Node(1), "contraband"))), ]), ) finally: shutil.rmtree(tempdir)
def test_readFromStdin(self): """ If no arguments are passed to C{pyflakes} then it reads from stdin. """ d = self.runPyflakes([], stdin='import contraband'.encode('ascii')) self.assertEqual( d, ("%s\n" % UnusedImport('<stdin>', 1, 'contraband'), '', 1))
def test_readFromStdin(self): """ If no arguments are passed to C{pyflakes} then it reads from stdin. """ d = self.runPyflakes([], stdin="import contraband") expected = UnusedImport("<stdin>", Node(1), "contraband") self.assertEqual(d, ("%s%s" % (expected, os.linesep), "", 1))
def test_readFromStdin(self): """ If no arguments are passed to C{pyflakes} then it reads from stdin. """ d = self.runPyflakes([], stdin='import contraband') expected = UnusedImport('<stdin>', Node(1), 'contraband') self.assertEqual(d, ("%s%s" % (expected, self.LINESEP), '', 1))
def test_pyflakesWarning(self): """ If the source file has a pyflakes warning, this is reported as a 'flake'. """ sourcePath = self.makeTempFile("import foo") count, errors = self.getErrors(sourcePath) self.assertEquals(count, 1) self.assertEquals(errors, [('flake', str(UnusedImport(sourcePath, 1, 'foo')))])
def test_flake(self): """ C{flake} reports a code warning from Pyflakes. It is exactly the str() of a L{pyflakes.messages.Message}. """ out = StringIO() reporter = Reporter(out, None) message = UnusedImport('foo.py', 42, 'bar') reporter.flake(message) self.assertEquals(out.getvalue(), "%s\n" % (message, ))
def test_fileWithFlakes(self): """ When a Python source file has warnings, the return code is non-zero and the warnings are printed to stdout. """ with open(self.tempfilepath, "wb") as fd: fd.write("import contraband\n".encode("ascii")) d = self.runPyflakes([self.tempfilepath]) expected = UnusedImport(self.tempfilepath, Node(1), "contraband") self.assertEqual(d, ("%s%s" % (expected, os.linesep), "", 1))
def test_fileWithFlakes(self): """ When a Python source file has warnings, the return code is non-zero and the warnings are printed to stdout. """ with open(self.tempfilepath, 'wb') as fd: fd.write("import contraband\n".encode('ascii')) d = self.runPyflakes([self.tempfilepath]) expected = UnusedImport(self.tempfilepath, Node(1), 'contraband') self.assertEqual(d, ("%s%s" % (expected, self.LINESEP), '', 1))
def test_pyflakesWarning(self): """ If the source file has a pyflakes warning, this is reported as a 'flake'. """ with self.makeTempFile("import foo") as sourcePath: count, errors = self.getErrors(sourcePath) self.assertEqual(count, 1) self.assertEqual( errors, [("flake", str(UnusedImport(sourcePath, Node(1), "foo")))])
def test_fileWithFlakes(self): """ When a Python source file has warnings, the return code is non-zero and the warnings are printed to stdout. """ fd = open(self.tempfilepath, 'wb') fd.write("import contraband\n".encode('ascii')) fd.close() d = self.runPyflakes([self.tempfilepath]) self.assertEqual( d, ("%s\n" % UnusedImport(self.tempfilepath, 1, 'contraband'), '', 1))
def test_checkRecursive(self): """ L{checkRecursive} descends into each directory, finding Python files and reporting problems. """ tempdir = tempfile.mkdtemp() os.mkdir(os.path.join(tempdir, 'foo')) file1 = os.path.join(tempdir, 'foo', 'bar.py') fd = open(file1, 'wb') fd.write("import baz\n".encode('ascii')) fd.close() file2 = os.path.join(tempdir, 'baz.py') fd = open(file2, 'wb') fd.write("import contraband".encode('ascii')) fd.close() log = [] reporter = LoggingReporter(log) warnings = checkRecursive([tempdir], reporter) self.assertEqual(warnings, 2) self.assertEqual( sorted(log), sorted([('flake', str(UnusedImport(file1, 1, 'baz'))), ('flake', str(UnusedImport(file2, 1, 'contraband')))]))