2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (c) 2016 Google, Inc
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')
26 # Also allow entry-type modules to be brought in from the etype directory.
27 sys.path.insert(0, os.path.join(our_path, 'etype'))
34 """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 image_test.TestImage):
58 suite = unittest.TestLoader().loadTestsFromTestCase(module)
62 for test, err in result.errors:
64 for test, err in result.failures:
65 print err, result.failures
66 if result.errors or result.failures:
67 print 'binman tests FAILED'
71 def RunTestCoverage():
72 """Run the tests and check that we get 100% coverage"""
73 # This uses the build output from sandbox_spl to get _libfdt.so
74 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
75 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
76 'tools/binman/binman.py -t' % options.build_dir)
78 stdout = command.Output('coverage', 'report')
79 lines = stdout.splitlines()
81 test_set= set([os.path.basename(line.split()[0])
82 for line in lines if '/etype/' in line])
83 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
84 all_set = set([os.path.basename(item) for item in glob_list])
85 missing_list = all_set
86 missing_list.difference_update(test_set)
87 missing_list.remove('_testing.py')
88 coverage = lines[-1].split(' ')[-1]
91 print 'Missing tests for %s' % (', '.join(missing_list))
93 if coverage != '100%':
95 print "Type 'coverage html' to get a report in htmlcov/index.html"
96 print 'Coverage error: %s, but should be 100%%' % coverage
99 raise ValueError('Test coverage failure')
101 def RunBinman(options, args):
102 """Main entry point to binman once arguments are parsed
105 options: Command-line options
106 args: Non-option arguments
110 # For testing: This enables full exception traces.
111 #options.debug = True
113 if not options.debug:
114 sys.tracebacklimit = 0
117 ret_code = RunTests(options.debug)
119 elif options.test_coverage:
122 elif options.full_help:
123 pager = os.getenv('PAGER')
126 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
128 command.Run(pager, fname)
132 ret_code = control.Binman(options, args)
133 except Exception as e:
134 print 'binman: %s' % e
137 traceback.print_exc()
142 if __name__ == "__main__":
143 (options, args) = cmdline.ParseArgs(sys.argv)
144 ret_code = RunBinman(options, args)