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
15 import multiprocessing
21 # Bring in the patman and dtoc libraries
22 our_path = os.path.dirname(os.path.realpath(__file__))
23 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
24 sys.path.insert(0, os.path.join(our_path, dirname))
26 # Bring in the libfdt module
27 sys.path.insert(0, 'scripts/dtc/pylibfdt')
28 sys.path.insert(0, os.path.join(our_path,
29 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
35 from concurrencytest import ConcurrentTestSuite, fork_for_tests
37 use_concurrent = False
41 def RunTests(debug, processes, args):
42 """Run the functional tests and any embedded doctests
45 debug: True to enable debugging, which shows a full stack trace on error
46 args: List of positional args provided to binman. This can hold a test
47 name to execute (as in 'binman -t testSections', for example)
48 processes: Number of processes to use to run tests (None=same as #CPUs)
58 result = unittest.TestResult()
60 suite = doctest.DocTestSuite(module)
63 sys.argv = [sys.argv[0]]
69 # Run the entry tests first ,since these need to be the first to import the
71 test_name = args and args[0] or None
72 suite = unittest.TestSuite()
73 loader = unittest.TestLoader()
74 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
75 elf_test.TestElf, image_test.TestImage):
78 suite.addTests(loader.loadTestsFromName(test_name, module))
79 except AttributeError:
82 suite.addTests(loader.loadTestsFromTestCase(module))
83 if use_concurrent and processes != 1:
84 concurrent_suite = ConcurrentTestSuite(suite,
85 fork_for_tests(processes or multiprocessing.cpu_count()))
86 concurrent_suite.run(result)
90 # Remove errors which just indicate a missing test. Since Python v3.5 If an
91 # ImportError or AttributeError occurs while traversing name then a
92 # synthetic test that raises that error when run will be returned. These
93 # errors are included in the errors accumulated by result.errors.
96 for test, err in result.errors:
97 if ("has no attribute '%s'" % test_name) not in err:
98 errors.append((test, err))
100 result.errors = errors
103 for test, err in result.errors:
104 print(test.id(), err)
105 for test, err in result.failures:
106 print(err, result.failures)
108 print('%d binman test%s SKIPPED:' %
109 (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
110 for skip_info in result.skipped:
111 print('%s: %s' % (skip_info[0], skip_info[1]))
112 if result.errors or result.failures:
113 print('binman tests FAILED')
117 def GetEntryModules(include_testing=True):
118 """Get a set of entry class implementations
121 Set of paths to entry class filenames
123 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
124 return set([os.path.splitext(os.path.basename(item))[0]
125 for item in glob_list
126 if include_testing or '_testing' not in item])
128 def RunTestCoverage():
129 """Run the tests and check that we get 100% coverage"""
130 glob_list = GetEntryModules(False)
131 all_set = set([os.path.splitext(os.path.basename(item))[0]
132 for item in glob_list if '_testing' not in item])
133 test_util.RunTestCoverage('tools/binman/binman.py', None,
134 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
135 options.build_dir, all_set)
137 def RunBinman(options, args):
138 """Main entry point to binman once arguments are parsed
141 options: Command-line options
142 args: Non-option arguments
146 # For testing: This enables full exception traces.
147 #options.debug = True
149 if not options.debug:
150 sys.tracebacklimit = 0
153 ret_code = RunTests(options.debug, options.processes, args[1:])
155 elif options.test_coverage:
158 elif options.entry_docs:
159 control.WriteEntryDocs(GetEntryModules())
163 ret_code = control.Binman(options, args)
164 except Exception as e:
165 print('binman: %s' % e)
168 traceback.print_exc()
173 if __name__ == "__main__":
174 (options, args) = cmdline.ParseArgs(sys.argv)
175 ret_code = RunBinman(options, args)