1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright 2017, Data61
6# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
7# ABN 41 687 119 230.
8#
9# This software may be distributed and modified according to the terms of
10# the BSD 2-Clause license. Note that NO WARRANTY is provided.
11# See "LICENSE_BSD2.txt" for details.
12#
13# @TAG(DATA61_BSD)
14#
15
16from __future__ import absolute_import, division, print_function, \
17    unicode_literals
18
19import os, pylint, re, sys, unittest
20from pylint import epylint
21
22ME = os.path.abspath(__file__)
23
24# Make CAmkES importable
25sys.path.append(os.path.join(os.path.dirname(ME), '../../..'))
26
27from camkes.internal.tests.utils import CAmkESTest
28
29class TestLint(CAmkESTest):
30    pass
31
32def lint(self, path):
33    stdout, stderr = epylint.py_run('%s --errors-only' % path, return_std=True)
34    err = []
35    for line in [x.strip() for x in stdout] + [x.strip() for x in stderr]:
36        if line == 'No config file found, using default configuration':
37            continue
38        if line:
39            err.append(line)
40    if len(err) > 0:
41        self.fail('\n'.join(err))
42
43srcdir = os.path.join(os.path.dirname(ME), '..')
44regex = re.compile(r'^.*\.py$')
45sub = re.compile(r'[^\w]')
46for src in os.listdir(srcdir):
47    if regex.match(src) is None:
48        continue
49    path = os.path.abspath(os.path.join(srcdir, src))
50    name = 'test_%s' % sub.sub('_', src)
51    setattr(TestLint, name, lambda self, path=path: lint(self, path))
52
53if __name__ == '__main__':
54    unittest.main()
55