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"""
44 result = unittest.TestResult()
46 suite = doctest.DocTestSuite(module)
49 sys.argv = [sys.argv[0]]
53 # Run the entry tests first ,since these need to be the first to import the
55 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
57 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf,
58 image_test.TestImage):
59 suite = unittest.TestLoader().loadTestsFromTestCase(module)
63 for test, err in result.errors:
65 for test, err in result.failures:
66 print err, result.failures
67 if result.errors or result.failures:
68 print 'binman tests FAILED'
72 def RunTestCoverage():
73 """Run the tests and check that we get 100% coverage"""
74 # This uses the build output from sandbox_spl to get _libfdt.so
75 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
76 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
77 'tools/binman/binman.py -t' % options.build_dir)
79 stdout = command.Output('coverage', 'report')
80 lines = stdout.splitlines()
82 test_set= set([os.path.basename(line.split()[0])
83 for line in lines if '/etype/' in line])
84 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
85 all_set = set([os.path.basename(item) for item in glob_list])
86 missing_list = all_set
87 missing_list.difference_update(test_set)
88 missing_list.remove('_testing.py')
89 coverage = lines[-1].split(' ')[-1]
92 print 'Missing tests for %s' % (', '.join(missing_list))
94 if coverage != '100%':
96 print "Type 'coverage html' to get a report in htmlcov/index.html"
97 print 'Coverage error: %s, but should be 100%%' % coverage
100 raise ValueError('Test coverage failure')
102 def RunBinman(options, args):
103 """Main entry point to binman once arguments are parsed
106 options: Command-line options
107 args: Non-option arguments
111 # For testing: This enables full exception traces.
112 #options.debug = True
114 if not options.debug:
115 sys.tracebacklimit = 0
118 ret_code = RunTests(options.debug)
120 elif options.test_coverage:
123 elif options.full_help:
124 pager = os.getenv('PAGER')
127 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
129 command.Run(pager, fname)
133 ret_code = control.Binman(options, args)
134 except Exception as e:
135 print 'binman: %s' % e
138 traceback.print_exc()
143 if __name__ == "__main__":
144 (options, args) = cmdline.ParseArgs(sys.argv)
145 ret_code = RunBinman(options, args)