Jörg Krause | cd6d46e | 2017-03-06 21:07:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 3 | |
| 4 | # Copyright (c) 2016 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 7 | # Creates binary images from input files controlled by a description |
| 8 | # |
| 9 | |
| 10 | """See README for more information""" |
| 11 | |
Simon Glass | 2c3cf45 | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 12 | import glob |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 13 | import os |
| 14 | import sys |
| 15 | import traceback |
| 16 | import unittest |
| 17 | |
| 18 | # Bring in the patman and dtoc libraries |
| 19 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | dde3e71 | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 20 | for dirname in ['../patman', '../dtoc', '..']: |
| 21 | sys.path.insert(0, os.path.join(our_path, dirname)) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 22 | |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 23 | # Bring in the libfdt module |
Masahiro Yamada | 47ae539 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 24 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 25 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 26 | # Also allow entry-type modules to be brought in from the etype directory. |
Simon Glass | dde3e71 | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 27 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 28 | |
| 29 | import cmdline |
| 30 | import command |
| 31 | import control |
| 32 | |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 33 | def RunTests(debug): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 34 | """Run the functional tests and any embedded doctests""" |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 35 | import elf_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 36 | import entry_test |
| 37 | import fdt_test |
Simon Glass | 076e63b | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 38 | import ftest |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 39 | import image_test |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | import test |
| 41 | import doctest |
| 42 | |
| 43 | result = unittest.TestResult() |
| 44 | for module in []: |
| 45 | suite = doctest.DocTestSuite(module) |
| 46 | suite.run(result) |
| 47 | |
| 48 | sys.argv = [sys.argv[0]] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 49 | if debug: |
| 50 | sys.argv.append('-D') |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 51 | |
| 52 | # Run the entry tests first ,since these need to be the first to import the |
| 53 | # 'entry' module. |
| 54 | suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry) |
| 55 | suite.run(result) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 56 | for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf, |
| 57 | image_test.TestImage): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 58 | suite = unittest.TestLoader().loadTestsFromTestCase(module) |
| 59 | suite.run(result) |
| 60 | |
| 61 | print result |
| 62 | for test, err in result.errors: |
| 63 | print test.id(), err |
| 64 | for test, err in result.failures: |
Simon Glass | 9ba021c | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 65 | print err, result.failures |
| 66 | if result.errors or result.failures: |
| 67 | print 'binman tests FAILED' |
| 68 | return 1 |
| 69 | return 0 |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 70 | |
| 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 |
Simon Glass | c303b9a | 2017-11-12 21:52:19 -0700 | [diff] [blame] | 74 | cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run ' |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 75 | '--include "tools/binman/*.py" --omit "*test*,*binman.py" ' |
| 76 | 'tools/binman/binman.py -t' % options.build_dir) |
| 77 | os.system(cmd) |
| 78 | stdout = command.Output('coverage', 'report') |
Simon Glass | 2c3cf45 | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 79 | lines = stdout.splitlines() |
| 80 | |
| 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] |
| 89 | ok = True |
| 90 | if missing_list: |
| 91 | print 'Missing tests for %s' % (', '.join(missing_list)) |
| 92 | ok = False |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | if coverage != '100%': |
| 94 | print stdout |
| 95 | print "Type 'coverage html' to get a report in htmlcov/index.html" |
Simon Glass | 2c3cf45 | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 96 | print 'Coverage error: %s, but should be 100%%' % coverage |
| 97 | ok = False |
| 98 | if not ok: |
| 99 | raise ValueError('Test coverage failure') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 100 | |
| 101 | def RunBinman(options, args): |
| 102 | """Main entry point to binman once arguments are parsed |
| 103 | |
| 104 | Args: |
| 105 | options: Command-line options |
| 106 | args: Non-option arguments |
| 107 | """ |
| 108 | ret_code = 0 |
| 109 | |
| 110 | # For testing: This enables full exception traces. |
| 111 | #options.debug = True |
| 112 | |
| 113 | if not options.debug: |
| 114 | sys.tracebacklimit = 0 |
| 115 | |
| 116 | if options.test: |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 117 | ret_code = RunTests(options.debug) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 118 | |
| 119 | elif options.test_coverage: |
| 120 | RunTestCoverage() |
| 121 | |
| 122 | elif options.full_help: |
| 123 | pager = os.getenv('PAGER') |
| 124 | if not pager: |
| 125 | pager = 'more' |
| 126 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 127 | 'README') |
| 128 | command.Run(pager, fname) |
| 129 | |
| 130 | else: |
| 131 | try: |
| 132 | ret_code = control.Binman(options, args) |
| 133 | except Exception as e: |
| 134 | print 'binman: %s' % e |
| 135 | if options.debug: |
| 136 | print |
| 137 | traceback.print_exc() |
| 138 | ret_code = 1 |
| 139 | return ret_code |
| 140 | |
| 141 | |
| 142 | if __name__ == "__main__": |
| 143 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 144 | ret_code = RunBinman(options, args) |
| 145 | sys.exit(ret_code) |