3 # Copyright (c) 2016 Google, Inc
4 # Written by Simon Glass <sjg@chromium.org>
6 # SPDX-License-Identifier: GPL-2.0+
8 # Creates binary images from input files controlled by a description
11 """See README for more information"""
19 # Bring in the patman and dtoc libraries
20 our_path = os.path.dirname(os.path.realpath(__file__))
21 for dirname in ['../patman', '../dtoc', '..']:
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')
27 # Also allow entry-type modules to be brought in from the etype directory.
28 sys.path.insert(0, os.path.join(our_path, 'etype'))
35 """Run the functional tests and any embedded doctests"""
43 result = unittest.TestResult()
45 suite = doctest.DocTestSuite(module)
48 sys.argv = [sys.argv[0]]
52 # Run the entry tests first ,since these need to be the first to import the
54 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
56 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf):
57 suite = unittest.TestLoader().loadTestsFromTestCase(module)
61 for test, err in result.errors:
63 for test, err in result.failures:
64 print err, result.failures
65 if result.errors or result.failures:
66 print 'binman tests FAILED'
70 def RunTestCoverage():
71 """Run the tests and check that we get 100% coverage"""
72 # This uses the build output from sandbox_spl to get _libfdt.so
73 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
74 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
75 'tools/binman/binman.py -t' % options.build_dir)
77 stdout = command.Output('coverage', 'report')
78 lines = stdout.splitlines()
80 test_set= set([os.path.basename(line.split()[0])
81 for line in lines if '/etype/' in line])
82 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
83 all_set = set([os.path.basename(item) for item in glob_list])
84 missing_list = all_set
85 missing_list.difference_update(test_set)
86 missing_list.remove('_testing.py')
87 coverage = lines[-1].split(' ')[-1]
90 print 'Missing tests for %s' % (', '.join(missing_list))
92 if coverage != '100%':
94 print "Type 'coverage html' to get a report in htmlcov/index.html"
95 print 'Coverage error: %s, but should be 100%%' % coverage
98 raise ValueError('Test coverage failure')
100 def RunBinman(options, args):
101 """Main entry point to binman once arguments are parsed
104 options: Command-line options
105 args: Non-option arguments
109 # For testing: This enables full exception traces.
110 #options.debug = True
112 if not options.debug:
113 sys.tracebacklimit = 0
116 ret_code = RunTests(options.debug)
118 elif options.test_coverage:
121 elif options.full_help:
122 pager = os.getenv('PAGER')
125 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
127 command.Run(pager, fname)
131 ret_code = control.Binman(options, args)
132 except Exception as e:
133 print 'binman: %s' % e
136 traceback.print_exc()
141 if __name__ == "__main__":
142 (options, args) = cmdline.ParseArgs(sys.argv)
143 ret_code = RunBinman(options, args)