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