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 __future__ import print_function
14 from distutils.sysconfig import get_python_lib
16 import multiprocessing
23 # Bring in the patman and dtoc libraries
24 our_path = os.path.dirname(os.path.realpath(__file__))
25 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
26 sys.path.insert(0, os.path.join(our_path, dirname))
28 # Bring in the libfdt module
29 sys.path.insert(0, 'scripts/dtc/pylibfdt')
30 sys.path.insert(0, os.path.join(our_path,
31 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
33 # When running under python-coverage on Ubuntu 16.04, the dist-packages
34 # directories are dropped from the python path. Add them in so that we can find
35 # the elffile module. We could use site.getsitepackages() here but unfortunately
36 # that is not available in a virtualenv.
37 sys.path.append(get_python_lib())
43 from concurrencytest import ConcurrentTestSuite, fork_for_tests
45 use_concurrent = False
49 def RunTests(debug, processes, args):
50 """Run the functional tests and any embedded doctests
53 debug: True to enable debugging, which shows a full stack trace on error
54 args: List of positional args provided to binman. This can hold a test
55 name to execute (as in 'binman -t testSections', for example)
56 processes: Number of processes to use to run tests (None=same as #CPUs)
66 result = unittest.TestResult()
68 suite = doctest.DocTestSuite(module)
71 sys.argv = [sys.argv[0]]
75 # Run the entry tests first ,since these need to be the first to import the
77 test_name = args and args[0] or None
78 suite = unittest.TestSuite()
79 loader = unittest.TestLoader()
80 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
81 elf_test.TestElf, image_test.TestImage):
84 suite.addTests(loader.loadTestsFromName(test_name, module))
85 except AttributeError:
88 suite.addTests(loader.loadTestsFromTestCase(module))
89 if use_concurrent and processes != 1:
90 concurrent_suite = ConcurrentTestSuite(suite,
91 fork_for_tests(processes or multiprocessing.cpu_count()))
92 concurrent_suite.run(result)
96 # Remove errors which just indicate a missing test. Since Python v3.5 If an
97 # ImportError or AttributeError occurs while traversing name then a
98 # synthetic test that raises that error when run will be returned. These
99 # errors are included in the errors accumulated by result.errors.
102 for test, err in result.errors:
103 if ("has no attribute '%s'" % test_name) not in err:
104 errors.append((test, err))
106 result.errors = errors
109 for test, err in result.errors:
110 print(test.id(), err)
111 for test, err in result.failures:
112 print(err, result.failures)
114 print('%d binman test%s SKIPPED:' %
115 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
116 for skip_info in result.skipped:
117 print('%s: %s' % (skip_info[0], skip_info[1]))
118 if result.errors or result.failures:
119 print('binman tests FAILED')
123 def GetEntryModules(include_testing=True):
124 """Get a set of entry class implementations
127 Set of paths to entry class filenames
129 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
130 return set([os.path.splitext(os.path.basename(item))[0]
131 for item in glob_list
132 if include_testing or '_testing' not in item])
134 def RunTestCoverage():
135 """Run the tests and check that we get 100% coverage"""
136 glob_list = GetEntryModules(False)
137 all_set = set([os.path.splitext(os.path.basename(item))[0]
138 for item in glob_list if '_testing' not in item])
139 test_util.RunTestCoverage('tools/binman/binman.py', None,
140 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
141 options.build_dir, all_set)
143 def RunBinman(options, args):
144 """Main entry point to binman once arguments are parsed
147 options: Command-line options
148 args: Non-option arguments
152 if not options.debug:
153 sys.tracebacklimit = 0
156 ret_code = RunTests(options.debug, options.processes, args[1:])
158 elif options.test_coverage:
161 elif options.entry_docs:
162 control.WriteEntryDocs(GetEntryModules())
166 ret_code = control.Binman(options, args)
167 except Exception as e:
168 print('binman: %s' % e)
171 traceback.print_exc()
176 if __name__ == "__main__":
177 (options, args) = cmdline.ParseArgs(sys.argv)
178 ret_code = RunBinman(options, args)