1## @file
2#  Unit tests for checking syntax of Python source code
3#
4#  Copyright (c) 2009, Intel Corporation. All rights reserved.<BR>
5#
6#  This program and the accompanying materials
7#  are licensed and made available under the terms and conditions of the BSD License
8#  which accompanies this distribution.  The full text of the license may be found at
9#  http://opensource.org/licenses/bsd-license.php
10#
11#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13#
14
15##
16# Import Modules
17#
18import os
19import unittest
20import py_compile
21
22import TestTools
23
24class Tests(TestTools.BaseToolsTest):
25
26    def setUp(self):
27        TestTools.BaseToolsTest.setUp(self)
28
29    def SingleFileTest(self, filename):
30        try:
31            py_compile.compile(filename, doraise=True)
32        except Exception, e:
33            self.fail('syntax error: %s, Error is %s' % (filename, str(e)))
34
35def MakePythonSyntaxCheckTests():
36    def GetAllPythonSourceFiles():
37        pythonSourceFiles = []
38        for (root, dirs, files) in os.walk(TestTools.PythonSourceDir):
39            for filename in files:
40                if filename.lower().endswith('.py'):
41                    pythonSourceFiles.append(
42                            os.path.join(root, filename)
43                        )
44        return pythonSourceFiles
45
46    def MakeTestName(filename):
47        assert filename.lower().endswith('.py')
48        name = filename[:-3]
49        name = name.replace(TestTools.PythonSourceDir, '')
50        name = name.replace(os.path.sep, '_')
51        return 'test' + name
52
53    def MakeNewTest(filename):
54        test = MakeTestName(filename)
55        newmethod = lambda self: self.SingleFileTest(filename)
56        setattr(
57            Tests,
58            test,
59            newmethod
60            )
61
62    for filename in GetAllPythonSourceFiles():
63        MakeNewTest(filename)
64
65MakePythonSyntaxCheckTests()
66del MakePythonSyntaxCheckTests
67
68TheTestSuite = TestTools.MakeTheTestSuite(locals())
69
70if __name__ == '__main__':
71    allTests = TheTestSuite()
72    unittest.TextTestRunner().run(allTests)
73
74
75