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