Maintainers.txt: Update email address
[mirror_edk2.git] / BaseTools / Tests / CheckPythonSyntax.py
1 ## @file
2 # Unit tests for checking syntax of Python source code
3 #
4 # Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 #
8
9 ##
10 # Import Modules
11 #
12 import os
13 import unittest
14 import py_compile
15
16 import TestTools
17
18 class Tests(TestTools.BaseToolsTest):
19
20 def setUp(self):
21 TestTools.BaseToolsTest.setUp(self)
22
23 def SingleFileTest(self, filename):
24 try:
25 py_compile.compile(filename, doraise=True)
26 except Exception as e:
27 self.fail('syntax error: %s, Error is %s' % (filename, str(e)))
28
29 def MakePythonSyntaxCheckTests():
30 def GetAllPythonSourceFiles():
31 pythonSourceFiles = []
32 for (root, dirs, files) in os.walk(TestTools.PythonSourceDir):
33 for filename in files:
34 if filename.lower().endswith('.py'):
35 pythonSourceFiles.append(
36 os.path.join(root, filename)
37 )
38 return pythonSourceFiles
39
40 def MakeTestName(filename):
41 assert filename.lower().endswith('.py')
42 name = filename[:-3]
43 name = name.replace(TestTools.PythonSourceDir, '')
44 name = name.replace(os.path.sep, '_')
45 return 'test' + name
46
47 def MakeNewTest(filename):
48 test = MakeTestName(filename)
49 newmethod = lambda self: self.SingleFileTest(filename)
50 setattr(
51 Tests,
52 test,
53 newmethod
54 )
55
56 for filename in GetAllPythonSourceFiles():
57 MakeNewTest(filename)
58
59 MakePythonSyntaxCheckTests()
60 del MakePythonSyntaxCheckTests
61
62 TheTestSuite = TestTools.MakeTheTestSuite(locals())
63
64 if __name__ == '__main__':
65 allTests = TheTestSuite()
66 unittest.TextTestRunner().run(allTests)
67
68