| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (C) 2017 Mike Pennisi. All rights reserved. |
| 3 |
# This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
import shutil, subprocess, sys, os, unittest, tempfile |
| 6 |
|
| 7 |
testDir = os.path.dirname(os.path.relpath(__file__)) |
| 8 |
OUT_DIR = os.path.join(testDir, 'out') |
| 9 |
ex = os.path.join(testDir, '..', 'lint.py') |
| 10 |
|
| 11 |
class TestLinter(unittest.TestCase): |
| 12 |
maxDiff = None |
| 13 |
|
| 14 |
def fixture(self, name, content): |
| 15 |
fspath = os.path.join(OUT_DIR, name) |
| 16 |
with open(fspath, 'w') as f: |
| 17 |
f.write(content) |
| 18 |
return fspath |
| 19 |
|
| 20 |
def lint(self, args): |
| 21 |
args[:0] = [ex] |
| 22 |
sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 23 |
stdout, stderr = sp.communicate() |
| 24 |
return dict(stdout=stdout, stderr=stderr, returncode=sp.returncode) |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
os.mkdir(OUT_DIR) |
| 28 |
|
| 29 |
def tearDown(self): |
| 30 |
shutil.rmtree(OUT_DIR, ignore_errors=True) |
| 31 |
|
| 32 |
def test_no_file(self): |
| 33 |
result = self.lint(['non-existent-file.js']) |
| 34 |
self.assertNotEqual(result["returncode"], 0) |
| 35 |
|
| 36 |
def test_whitelist_single(self): |
| 37 |
test_content = ('// Copyright (C) 2017 Mike Pennisi. All rights reserved.\n' + |
| 38 |
'// This code is governed by the BSD license found in the LICENSE file.') |
| 39 |
test_file = self.fixture('input.js', test_content) |
| 40 |
whitelist_content = test_file + ' FRONTMATTER' |
| 41 |
whitelist_file = self.fixture('lint.whitelist', whitelist_content) |
| 42 |
|
| 43 |
result = self.lint([test_file]) |
| 44 |
|
| 45 |
self.assertNotEqual(result['returncode'], 0) |
| 46 |
|
| 47 |
result = self.lint(['--whitelist', whitelist_file, test_file]) |
| 48 |
|
| 49 |
self.assertEqual(result['returncode'], 0) |
| 50 |
|
| 51 |
def test_whitelist_comment(self): |
| 52 |
test_content = ('// Copyright (C) 2017 Mike Pennisi. All rights reserved.\n' + |
| 53 |
'// This code is governed by the BSD license found in the LICENSE file.') |
| 54 |
test_file = self.fixture('input.js', test_content) |
| 55 |
whitelist_content = ('# One comment\n' + |
| 56 |
'# Another comment\n' + |
| 57 |
test_file + ' FRONTMATTER') |
| 58 |
whitelist_file = self.fixture('lint.whitelist', whitelist_content) |
| 59 |
|
| 60 |
result = self.lint([test_file]) |
| 61 |
|
| 62 |
self.assertNotEqual(result['returncode'], 0) |
| 63 |
|
| 64 |
result = self.lint(['--whitelist', whitelist_file, test_file]) |
| 65 |
|
| 66 |
self.assertEqual(result['returncode'], 0) |
| 67 |
|
| 68 |
def create_file_test(name, fspath): |
| 69 |
'''Dynamically generate a function that may be used as a test method with |
| 70 |
the Python `unittest` module.''' |
| 71 |
|
| 72 |
def test(self): |
| 73 |
with open(fspath, 'r') as f: |
| 74 |
contents = f.read() |
| 75 |
expected, input = contents.split('^ expected errors | v input\n') |
| 76 |
expected = expected.split() |
| 77 |
tmp_file = self.fixture(name, input) |
| 78 |
result = self.lint([tmp_file]) |
| 79 |
if len(expected) == 0: |
| 80 |
self.assertEqual(result['returncode'], 0) |
| 81 |
self.assertEqual(result['stderr'], '') |
| 82 |
else: |
| 83 |
self.assertNotEqual(result['returncode'], 0) |
| 84 |
for err in expected: |
| 85 |
self.assertIn(err, result['stderr']) |
| 86 |
|
| 87 |
return test |
| 88 |
|
| 89 |
dirname = os.path.join(os.path.abspath(testDir), 'fixtures') |
| 90 |
for file_name in os.listdir(dirname): |
| 91 |
full_path = os.path.join(dirname, file_name) |
| 92 |
if not os.path.isfile(full_path) or file_name.startswith('.'): |
| 93 |
continue |
| 94 |
|
| 95 |
t = create_file_test(file_name, full_path) |
| 96 |
t.__name__ = 'test_' + file_name |
| 97 |
setattr(TestLinter, t.__name__, t) |
| 98 |
|
| 99 |
if __name__ == '__main__': |
| 100 |
unittest.main() |