2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
7 # Creates binary images from input files controlled by a description
10 """See README for more information"""
12 from distutils.sysconfig import get_python_lib
14 import multiprocessing
21 # Bring in the patman and dtoc libraries (but don't override the first path
23 our_path = os.path.dirname(os.path.realpath(__file__))
24 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
25 sys.path.insert(2, os.path.join(our_path, dirname))
27 # Bring in the libfdt module
28 sys.path.insert(2, 'scripts/dtc/pylibfdt')
29 sys.path.insert(2, os.path.join(our_path,
30 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
32 # When running under python-coverage on Ubuntu 16.04, the dist-packages
33 # directories are dropped from the python path. Add them in so that we can find
34 # the elffile module. We could use site.getsitepackages() here but unfortunately
35 # that is not available in a virtualenv.
36 sys.path.append(get_python_lib())
42 from concurrencytest import ConcurrentTestSuite, fork_for_tests
44 use_concurrent = False
48 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
49 """Run the functional tests and any embedded doctests
52 debug: True to enable debugging, which shows a full stack trace on error
53 verbosity: Verbosity level to use
54 test_preserve_dirs: True to preserve the input directory used by tests
55 so that it can be examined afterwards (only useful for debugging
56 tests). If a single test is selected (in args[0]) it also preserves
57 the output directory for this test. Both directories are displayed
59 processes: Number of processes to use to run tests (None=same as #CPUs)
60 args: List of positional args provided to binman. This can hold a test
61 name to execute (as in 'binman test testSections', for example)
62 toolpath: List of paths to use for tools
73 result = unittest.TestResult()
75 suite = doctest.DocTestSuite(module)
78 sys.argv = [sys.argv[0]]
82 sys.argv.append('-v%d' % verbosity)
85 sys.argv += ['--toolpath', path]
87 # Run the entry tests first ,since these need to be the first to import the
89 test_name = args and args[0] or None
90 suite = unittest.TestSuite()
91 loader = unittest.TestLoader()
92 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
93 elf_test.TestElf, image_test.TestImage,
94 cbfs_util_test.TestCbfs):
95 # Test the test module about our arguments, if it is interested
96 if hasattr(module, 'setup_test_args'):
97 setup_test_args = getattr(module, 'setup_test_args')
98 setup_test_args(preserve_indir=test_preserve_dirs,
99 preserve_outdirs=test_preserve_dirs and test_name is not None,
100 toolpath=toolpath, verbosity=verbosity)
103 suite.addTests(loader.loadTestsFromName(test_name, module))
104 except AttributeError:
107 suite.addTests(loader.loadTestsFromTestCase(module))
108 if use_concurrent and processes != 1:
109 concurrent_suite = ConcurrentTestSuite(suite,
110 fork_for_tests(processes or multiprocessing.cpu_count()))
111 concurrent_suite.run(result)
115 # Remove errors which just indicate a missing test. Since Python v3.5 If an
116 # ImportError or AttributeError occurs while traversing name then a
117 # synthetic test that raises that error when run will be returned. These
118 # errors are included in the errors accumulated by result.errors.
121 for test, err in result.errors:
122 if ("has no attribute '%s'" % test_name) not in err:
123 errors.append((test, err))
125 result.errors = errors
128 for test, err in result.errors:
129 print(test.id(), err)
130 for test, err in result.failures:
131 print(err, result.failures)
133 print('%d binman test%s SKIPPED:' %
134 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
135 for skip_info in result.skipped:
136 print('%s: %s' % (skip_info[0], skip_info[1]))
137 if result.errors or result.failures:
138 print('binman tests FAILED')
142 def GetEntryModules(include_testing=True):
143 """Get a set of entry class implementations
146 Set of paths to entry class filenames
148 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
149 return set([os.path.splitext(os.path.basename(item))[0]
150 for item in glob_list
151 if include_testing or '_testing' not in item])
153 def RunTestCoverage():
154 """Run the tests and check that we get 100% coverage"""
155 glob_list = GetEntryModules(False)
156 all_set = set([os.path.splitext(os.path.basename(item))[0]
157 for item in glob_list if '_testing' not in item])
158 test_util.RunTestCoverage('tools/binman/binman.py', None,
159 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
160 args.build_dir, all_set)
163 """Main entry point to binman once arguments are parsed
166 args: Command line arguments Namespace object
171 sys.tracebacklimit = 0
173 if args.cmd == 'test':
174 if args.test_coverage:
177 ret_code = RunTests(args.debug, args.verbosity, args.processes,
178 args.test_preserve_dirs, args.tests,
181 elif args.cmd == 'entry-docs':
182 control.WriteEntryDocs(GetEntryModules())
186 ret_code = control.Binman(args)
187 except Exception as e:
188 print('binman: %s' % e)
191 traceback.print_exc()
196 if __name__ == "__main__":
197 args = cmdline.ParseArgs(sys.argv[1:])
199 ret_code = RunBinman(args)