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"""
18 # Bring in the patman and dtoc libraries
19 our_path = os.path.dirname(os.path.realpath(__file__))
20 for dirname in ['../patman', '../dtoc', '..']:
21 sys.path.insert(0, os.path.join(our_path, dirname))
23 # Bring in the libfdt module
24 sys.path.insert(0, 'scripts/dtc/pylibfdt')
30 def RunTests(debug, args):
31 """Run the functional tests and any embedded doctests
34 debug: True to enable debugging, which shows a full stack trace on error
35 args: List of positional args provided to binman. This can hold a test
36 name to execute (as in 'binman -t testSections', for example)
46 result = unittest.TestResult()
48 suite = doctest.DocTestSuite(module)
51 sys.argv = [sys.argv[0]]
55 # Run the entry tests first ,since these need to be the first to import the
57 test_name = args and args[0] or None
58 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
59 elf_test.TestElf, image_test.TestImage):
62 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
63 except AttributeError:
66 suite = unittest.TestLoader().loadTestsFromTestCase(module)
70 for test, err in result.errors:
72 for test, err in result.failures:
73 print err, result.failures
74 if result.errors or result.failures:
75 print 'binman tests FAILED'
79 def RunTestCoverage():
80 """Run the tests and check that we get 100% coverage"""
81 # This uses the build output from sandbox_spl to get _libfdt.so
82 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools python-coverage run '
83 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
84 'tools/binman/binman.py -t' % options.build_dir)
86 stdout = command.Output('python-coverage', 'report')
87 lines = stdout.splitlines()
89 test_set= set([os.path.basename(line.split()[0])
90 for line in lines if '/etype/' in line])
91 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
92 all_set = set([os.path.splitext(os.path.basename(item))[0]
93 for item in glob_list if '_testing' not in item])
94 missing_list = all_set
95 missing_list.difference_update(test_set)
96 coverage = lines[-1].split(' ')[-1]
99 print 'Missing tests for %s' % (', '.join(missing_list))
102 if coverage != '100%':
104 print "Type 'coverage html' to get a report in htmlcov/index.html"
105 print 'Coverage error: %s, but should be 100%%' % coverage
108 raise ValueError('Test coverage failure')
110 def RunBinman(options, args):
111 """Main entry point to binman once arguments are parsed
114 options: Command-line options
115 args: Non-option arguments
119 # For testing: This enables full exception traces.
120 #options.debug = True
122 if not options.debug:
123 sys.tracebacklimit = 0
126 ret_code = RunTests(options.debug, args[1:])
128 elif options.test_coverage:
131 elif options.full_help:
132 pager = os.getenv('PAGER')
135 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
137 command.Run(pager, fname)
141 ret_code = control.Binman(options, args)
142 except Exception as e:
143 print 'binman: %s' % e
146 traceback.print_exc()
151 if __name__ == "__main__":
152 (options, args) = cmdline.ParseArgs(sys.argv)
153 ret_code = RunBinman(options, args)