Ejemplo n.º 1
0
    def test_shebang(self):
        """
        Find Python files that don't end with `.py`, but contain a Python
        shebang.
        """
        python = os.path.join(self.tempdir, 'a')
        with open(python, 'w') as fd:
            fd.write('#!/usr/bin/env python\n')

        self.makeEmptyFile('b')

        with open(os.path.join(self.tempdir, 'c'), 'w') as fd:
            fd.write('hello\nworld\n')

        python2 = os.path.join(self.tempdir, 'd')
        with open(python2, 'w') as fd:
            fd.write('#!/usr/bin/env python2\n')

        python3 = os.path.join(self.tempdir, 'e')
        with open(python3, 'w') as fd:
            fd.write('#!/usr/bin/env python3\n')

        pythonw = os.path.join(self.tempdir, 'f')
        with open(pythonw, 'w') as fd:
            fd.write('#!/usr/bin/env pythonw\n')

        self.assertEqual(
            sorted(iterSourceCode([self.tempdir])),
            sorted([python, python2, python3, pythonw]))
Ejemplo n.º 2
0
 def test_singleFile(self):
     """
     If the directory contains one Python file, C{iterSourceCode} will find
     it.
     """
     childpath = self.makeEmptyFile('foo.py')
     self.assertEqual(list(iterSourceCode([self.tempdir])), [childpath])
Ejemplo n.º 3
0
 def test_explicitFiles(self):
     """
     If one of the paths given to L{iterSourceCode} is not a directory but
     a file, it will include that in its output.
     """
     epath = self.makeEmptyFile("e.py")
     self.assertEqual(list(iterSourceCode([epath])), [epath])
Ejemplo n.º 4
0
def _check_recursive(paths, reporter):
    """
    The builtin recursive checker tries to check .pyc files.
    """
    num_warnings = 0
    for path in api.iterSourceCode(paths):
        if path.endswith('.py'):
            num_warnings += api.checkPath(path, reporter)
    return num_warnings
Ejemplo n.º 5
0
    def run(self):
        reporter = ProspectorReporter(ignore=self.ignore_codes)

        for filepath in iterSourceCode(self._paths):
            if any([ip.search(filepath) for ip in self._ignores]):
                continue

            checkPath(filepath, reporter)

        return reporter.get_messages()
Ejemplo n.º 6
0
 def test_recurses(self):
     """
     If the Python files are hidden deep down in child directories, we will
     find them.
     """
     os.mkdir(os.path.join(self.tempdir, "foo"))
     apath = self.makeEmptyFile("foo", "a.py")
     os.mkdir(os.path.join(self.tempdir, "bar"))
     bpath = self.makeEmptyFile("bar", "b.py")
     cpath = self.makeEmptyFile("c.py")
     self.assertEqual(sorted(iterSourceCode([self.tempdir])), sorted([apath, bpath, cpath]))
Ejemplo n.º 7
0
 def test_multipleDirectories(self):
     """
     L{iterSourceCode} can be given multiple directories.  It will recurse
     into each of them.
     """
     foopath = os.path.join(self.tempdir, "foo")
     barpath = os.path.join(self.tempdir, "bar")
     os.mkdir(foopath)
     apath = self.makeEmptyFile("foo", "a.py")
     os.mkdir(barpath)
     bpath = self.makeEmptyFile("bar", "b.py")
     self.assertEqual(sorted(iterSourceCode([foopath, barpath])), sorted([apath, bpath]))
Ejemplo n.º 8
0
 def test_multipleDirectories(self):
     """
     L{iterSourceCode} can be given multiple directories.  It will recurse
     into each of them.
     """
     foopath = os.path.join(self.tempdir, 'foo')
     barpath = os.path.join(self.tempdir, 'bar')
     os.mkdir(foopath)
     apath = self.makeEmptyFile('foo', 'a.py')
     os.mkdir(barpath)
     bpath = self.makeEmptyFile('bar', 'b.py')
     self.assertEqual(sorted(iterSourceCode([foopath, barpath])),
                      sorted([apath, bpath]))
Ejemplo n.º 9
0
 def test_recurses(self):
     """
     If the Python files are hidden deep down in child directories, we will
     find them.
     """
     os.mkdir(os.path.join(self.tempdir, 'foo'))
     apath = self.makeEmptyFile('foo', 'a.py')
     os.mkdir(os.path.join(self.tempdir, 'bar'))
     bpath = self.makeEmptyFile('bar', 'b.py')
     cpath = self.makeEmptyFile('c.py')
     self.assertEqual(
         sorted(iterSourceCode([self.tempdir])),
         sorted([apath, bpath, cpath]))
Ejemplo n.º 10
0
 def test_recurses(self):
     """
     If the Python files are hidden deep down in child directories, we will
     find them.
     """
     os.mkdir(os.path.join(self.tempdir, "foo"))
     apath = self.makeEmptyFile("foo", "a.py")
     self.makeEmptyFile("foo", "a.py~")
     os.mkdir(os.path.join(self.tempdir, "bar"))
     bpath = self.makeEmptyFile("bar", "b.py")
     cpath = self.makeEmptyFile("c.py")
     self.assertEqual(sorted(iterSourceCode([self.tempdir])),
                      sorted([apath, bpath, cpath]))
Ejemplo n.º 11
0
 def test_recurses(self):
     """
     If the Python files are hidden deep down in child directories, we will
     find them.
     """
     os.mkdir(os.path.join(self.tempdir, 'foo'))
     apath = self.makeEmptyFile('foo', 'a.py')
     os.mkdir(os.path.join(self.tempdir, 'bar'))
     bpath = self.makeEmptyFile('bar', 'b.py')
     cpath = self.makeEmptyFile('c.py')
     self.assertEqual(
         sorted(iterSourceCode([self.tempdir])),
         sorted([apath, bpath, cpath]))
Ejemplo n.º 12
0
 def test_emptyDirectory(self):
     """
     There are no Python files in an empty directory.
     """
     self.assertEqual(list(iterSourceCode([self.tempdir])), [])
Ejemplo n.º 13
0
 def test_onlyPythonSource(self):
     """
     Files that are not Python source files are not included.
     """
     self.makeEmptyFile('foo.pyc')
     self.assertEqual(list(iterSourceCode([self.tempdir])), [])
Ejemplo n.º 14
0
"""
Ejemplo n.º 15
0
    def test_shebang(self):
        """
        Find Python files that don't end with `.py`, but contain a Python
        shebang.
        """
        python = os.path.join(self.tempdir, "a")
        with open(python, "w") as fd:
            fd.write("#!/usr/bin/env python\n")

        self.makeEmptyFile("b")

        with open(os.path.join(self.tempdir, "c"), "w") as fd:
            fd.write("hello\nworld\n")

        python2 = os.path.join(self.tempdir, "d")
        with open(python2, "w") as fd:
            fd.write("#!/usr/bin/env python2\n")

        python3 = os.path.join(self.tempdir, "e")
        with open(python3, "w") as fd:
            fd.write("#!/usr/bin/env python3\n")

        pythonw = os.path.join(self.tempdir, "f")
        with open(pythonw, "w") as fd:
            fd.write("#!/usr/bin/env pythonw\n")

        python3args = os.path.join(self.tempdir, "g")
        with open(python3args, "w") as fd:
            fd.write("#!/usr/bin/python3 -u\n")

        python2u = os.path.join(self.tempdir, "h")
        with open(python2u, "w") as fd:
            fd.write("#!/usr/bin/python2u\n")

        python3d = os.path.join(self.tempdir, "i")
        with open(python3d, "w") as fd:
            fd.write("#!/usr/local/bin/python3d\n")

        python38m = os.path.join(self.tempdir, "j")
        with open(python38m, "w") as fd:
            fd.write("#! /usr/bin/env python3.8m\n")

        python27 = os.path.join(self.tempdir, "k")
        with open(python27, "w") as fd:
            fd.write("#!/usr/bin/python2.7   \n")

        # Should NOT be treated as Python source
        notfirst = os.path.join(self.tempdir, "l")
        with open(notfirst, "w") as fd:
            fd.write("#!/bin/sh\n#!/usr/bin/python\n")

        self.assertEqual(
            sorted(iterSourceCode([self.tempdir])),
            sorted([
                python,
                python2,
                python3,
                pythonw,
                python3args,
                python2u,
                python3d,
                python38m,
                python27,
            ]),
        )
Ejemplo n.º 16
0
 def test_onlyPythonSource(self):
     """
     Files that are not Python source files are not included.
     """
     self.makeEmptyFile('foo.pyc')
     self.assertEqual(list(iterSourceCode([self.tempdir])), [])
Ejemplo n.º 17
0
 def test_emptyDirectory(self):
     """
     There are no Python files in an empty directory.
     """
     self.assertEqual(list(iterSourceCode([self.tempdir])), [])