File: run.py

package info (click to toggle)
qtdeclarative-opensource-src 5.15.8%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 259,256 kB
  • sloc: javascript: 512,396; cpp: 495,775; xml: 8,892; python: 3,304; ansic: 2,764; sh: 206; makefile: 62; php: 27
file content (100 lines) | stat: -rwxr-xr-x 3,540 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# Copyright (C) 2017 Mike Pennisi. All rights reserved.
# This code is governed by the BSD license found in the LICENSE file.

import shutil, subprocess, sys, os, unittest, tempfile

testDir = os.path.dirname(os.path.relpath(__file__))
OUT_DIR = os.path.join(testDir, 'out')
ex = os.path.join(testDir, '..', 'lint.py')

class TestLinter(unittest.TestCase):
    maxDiff = None

    def fixture(self, name, content):
        fspath = os.path.join(OUT_DIR, name)
        with open(fspath, 'w') as f:
            f.write(content)
        return fspath

    def lint(self, args):
        args[:0] = [ex]
        sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = sp.communicate()
        return dict(stdout=stdout, stderr=stderr, returncode=sp.returncode)

    def setUp(self):
        os.mkdir(OUT_DIR)

    def tearDown(self):
        shutil.rmtree(OUT_DIR, ignore_errors=True)

    def test_no_file(self):
        result = self.lint(['non-existent-file.js'])
        self.assertNotEqual(result["returncode"], 0)

    def test_whitelist_single(self):
        test_content = ('// Copyright (C) 2017 Mike Pennisi. All rights reserved.\n' +
            '// This code is governed by the BSD license found in the LICENSE file.')
        test_file = self.fixture('input.js', test_content)
        whitelist_content = test_file + ' FRONTMATTER'
        whitelist_file = self.fixture('lint.whitelist', whitelist_content)

        result = self.lint([test_file])

        self.assertNotEqual(result['returncode'], 0)

        result = self.lint(['--whitelist', whitelist_file, test_file])

        self.assertEqual(result['returncode'], 0)

    def test_whitelist_comment(self):
        test_content = ('// Copyright (C) 2017 Mike Pennisi. All rights reserved.\n' +
            '// This code is governed by the BSD license found in the LICENSE file.')
        test_file = self.fixture('input.js', test_content)
        whitelist_content = ('# One comment\n' +
            '# Another comment\n' +
            test_file + ' FRONTMATTER')
        whitelist_file = self.fixture('lint.whitelist', whitelist_content)

        result = self.lint([test_file])

        self.assertNotEqual(result['returncode'], 0)

        result = self.lint(['--whitelist', whitelist_file, test_file])

        self.assertEqual(result['returncode'], 0)

def create_file_test(name, fspath):
    '''Dynamically generate a function that may be used as a test method with
    the Python `unittest` module.'''

    def test(self):
        with open(fspath, 'r') as f:
            contents = f.read()
        expected, input = contents.split('^ expected errors | v input\n')
        expected = expected.split()
        tmp_file = self.fixture(name, input)
        result = self.lint([tmp_file])
        if len(expected) == 0:
            self.assertEqual(result['returncode'], 0)
            self.assertEqual(result['stderr'], '')
        else:
            self.assertNotEqual(result['returncode'], 0)
            for err in expected:
                self.assertIn(err, result['stderr'])

    return test

dirname = os.path.join(os.path.abspath(testDir), 'fixtures')
for file_name in os.listdir(dirname):
    full_path = os.path.join(dirname, file_name)
    if not os.path.isfile(full_path) or file_name.startswith('.'):
        continue

    t = create_file_test(file_name, full_path)
    t.__name__ = 'test_' + file_name
    setattr(TestLinter, t.__name__, t)

if __name__ == '__main__':
    unittest.main()