~ubuntu-core-dev/ubuntu-release-upgrader/trunk

« back to all changes in this revision

Viewing changes to tests/test_pyflakes.py

  • Committer: Balint Reczey
  • Date: 2019-12-17 20:29:55 UTC
  • Revision ID: balint.reczey@canonical.com-20191217202955-nqe4xz2c54s60y59
Moved to git at https://git.launchpad.net/ubuntu-release-upgrader

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python3
2
 
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
3
 
 
4
 
# Partly based on a script from Review Board, MIT license; but modified to
5
 
# act as a unit test.
6
 
 
7
 
from __future__ import print_function
8
 
 
9
 
import os
10
 
import re
11
 
import subprocess
12
 
import unittest
13
 
 
14
 
CURDIR = os.path.dirname(os.path.abspath(__file__))
15
 
 
16
 
 
17
 
class TestPyflakesClean(unittest.TestCase):
18
 
    """ ensure that the tree is pyflakes clean """
19
 
 
20
 
    def read_exclusions(self):
21
 
        exclusions = {}
22
 
        try:
23
 
            excpath = os.path.join(CURDIR, "pyflakes.exclude")
24
 
            with open(excpath, "r") as fp:
25
 
                for line in fp:
26
 
                    if not line.startswith("#"):
27
 
                        exclusions[line.rstrip()] = 1
28
 
        except IOError:
29
 
            pass
30
 
        return exclusions
31
 
 
32
 
    def filter_exclusions(self, contents):
33
 
        exclusions = self.read_exclusions()
34
 
        for line in contents:
35
 
            if line.startswith("#"):
36
 
                continue
37
 
 
38
 
            line = line.rstrip().split(CURDIR + '/', 1)[1]
39
 
            test_line = re.sub(r":[0-9]+:", r":*:", line, 1)
40
 
            test_line = re.sub(r"line [0-9]+", r"line *", test_line)
41
 
 
42
 
            if test_line not in exclusions:
43
 
                yield line
44
 
 
45
 
    def test_pyflakes_clean(self):
46
 
        # mvo: type -f here to avoid running pyflakes on imported files
47
 
        #      that are symlinks to other packages
48
 
        cmd = 'find %s/.. -type f -name "*.py" | xargs pyflakes3' % CURDIR
49
 
        p = subprocess.Popen(
50
 
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
51
 
            close_fds=True, shell=True, universal_newlines=True)
52
 
        contents = p.communicate()[0].splitlines()
53
 
        filtered_contents = list(self.filter_exclusions(contents))
54
 
        for line in filtered_contents:
55
 
            print(line)
56
 
        self.assertEqual(0, len(filtered_contents))
57
 
 
58
 
 
59
 
if __name__ == "__main__":
60
 
    import logging
61
 
    logging.basicConfig(level=logging.DEBUG)
62
 
    unittest.main()