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"""
13 import multiprocessing
19 # Bring in the patman and dtoc libraries
20 our_path = os.path.dirname(os.path.realpath(__file__))
21 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
22 sys.path.insert(0, os.path.join(our_path, dirname))
24 # Bring in the libfdt module
25 sys.path.insert(0, 'scripts/dtc/pylibfdt')
26 sys.path.insert(0, os.path.join(our_path,
27 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
33 from concurrencytest import ConcurrentTestSuite, fork_for_tests
35 use_concurrent = False
39 def RunTests(debug, processes, args):
40 """Run the functional tests and any embedded doctests
43 debug: True to enable debugging, which shows a full stack trace on error
44 args: List of positional args provided to binman. This can hold a test
45 name to execute (as in 'binman -t testSections', for example)
46 processes: Number of processes to use to run tests (None=same as #CPUs)
56 result = unittest.TestResult()
58 suite = doctest.DocTestSuite(module)
61 sys.argv = [sys.argv[0]]
67 # Run the entry tests first ,since these need to be the first to import the
69 test_name = args and args[0] or None
70 suite = unittest.TestSuite()
71 loader = unittest.TestLoader()
72 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
73 elf_test.TestElf, image_test.TestImage):
76 suite.addTests(loader.loadTestsFromName(test_name, module))
77 except AttributeError:
80 suite.addTests(loader.loadTestsFromTestCase(module))
81 if use_concurrent and processes != 1:
82 concurrent_suite = ConcurrentTestSuite(suite,
83 fork_for_tests(processes or multiprocessing.cpu_count()))
84 concurrent_suite.run(result)
89 for test, err in result.errors:
91 for test, err in result.failures:
92 print err, result.failures
93 if result.errors or result.failures:
94 print 'binman tests FAILED'
98 def GetEntryModules(include_testing=True):
99 """Get a set of entry class implementations
102 Set of paths to entry class filenames
104 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
105 return set([os.path.splitext(os.path.basename(item))[0]
106 for item in glob_list
107 if include_testing or '_testing' not in item])
109 def RunTestCoverage():
110 """Run the tests and check that we get 100% coverage"""
111 glob_list = GetEntryModules(False)
112 all_set = set([os.path.splitext(os.path.basename(item))[0]
113 for item in glob_list if '_testing' not in item])
114 test_util.RunTestCoverage('tools/binman/binman.py', None,
115 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
116 options.build_dir, all_set)
118 def RunBinman(options, args):
119 """Main entry point to binman once arguments are parsed
122 options: Command-line options
123 args: Non-option arguments
127 # For testing: This enables full exception traces.
128 #options.debug = True
130 if not options.debug:
131 sys.tracebacklimit = 0
134 ret_code = RunTests(options.debug, options.processes, args[1:])
136 elif options.test_coverage:
139 elif options.entry_docs:
140 control.WriteEntryDocs(GetEntryModules())
144 ret_code = control.Binman(options, args)
145 except Exception as e:
146 print 'binman: %s' % e
149 traceback.print_exc()
154 if __name__ == "__main__":
155 (options, args) = cmdline.ParseArgs(sys.argv)
156 ret_code = RunBinman(options, args)