Jörg Krause | 66a7a24 | 2017-03-06 21:07:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Simon Glass | bf7fd50 | 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 | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 13 | import glob |
Simon Glass | bf7fd50 | 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 | 7feccfd | 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 | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 23 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 24 | # Bring in the libfdt module |
Masahiro Yamada | 15b97f5 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 25 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 26 | |
Simon Glass | bf7fd50 | 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 | 7feccfd | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 28 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 29 | |
| 30 | import cmdline |
| 31 | import command |
| 32 | import control |
| 33 | |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 34 | def RunTests(debug): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 35 | """Run the functional tests and any embedded doctests""" |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 36 | import elf_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 37 | import entry_test |
| 38 | import fdt_test |
Simon Glass | 680e331 | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 39 | import ftest |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 40 | import image_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 41 | import test |
| 42 | import doctest |
| 43 | |
| 44 | result = unittest.TestResult() |
| 45 | for module in []: |
| 46 | suite = doctest.DocTestSuite(module) |
| 47 | suite.run(result) |
| 48 | |
| 49 | sys.argv = [sys.argv[0]] |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 50 | if debug: |
| 51 | sys.argv.append('-D') |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 52 | |
| 53 | # Run the entry tests first ,since these need to be the first to import the |
| 54 | # 'entry' module. |
| 55 | suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry) |
| 56 | suite.run(result) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 57 | for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf, |
| 58 | image_test.TestImage): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 59 | suite = unittest.TestLoader().loadTestsFromTestCase(module) |
| 60 | suite.run(result) |
| 61 | |
| 62 | print result |
| 63 | for test, err in result.errors: |
| 64 | print test.id(), err |
| 65 | for test, err in result.failures: |
Simon Glass | 9677faa | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 66 | print err, result.failures |
| 67 | if result.errors or result.failures: |
| 68 | print 'binman tests FAILED' |
| 69 | return 1 |
| 70 | return 0 |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 71 | |
| 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 |
Simon Glass | 5a3f222 | 2017-11-12 21:52:19 -0700 | [diff] [blame] | 75 | cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run ' |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 76 | '--include "tools/binman/*.py" --omit "*test*,*binman.py" ' |
| 77 | 'tools/binman/binman.py -t' % options.build_dir) |
| 78 | os.system(cmd) |
| 79 | stdout = command.Output('coverage', 'report') |
Simon Glass | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 80 | lines = stdout.splitlines() |
| 81 | |
| 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] |
| 90 | ok = True |
| 91 | if missing_list: |
| 92 | print 'Missing tests for %s' % (', '.join(missing_list)) |
| 93 | ok = False |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 94 | if coverage != '100%': |
| 95 | print stdout |
| 96 | print "Type 'coverage html' to get a report in htmlcov/index.html" |
Simon Glass | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 97 | print 'Coverage error: %s, but should be 100%%' % coverage |
| 98 | ok = False |
| 99 | if not ok: |
| 100 | raise ValueError('Test coverage failure') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 101 | |
| 102 | def RunBinman(options, args): |
| 103 | """Main entry point to binman once arguments are parsed |
| 104 | |
| 105 | Args: |
| 106 | options: Command-line options |
| 107 | args: Non-option arguments |
| 108 | """ |
| 109 | ret_code = 0 |
| 110 | |
| 111 | # For testing: This enables full exception traces. |
| 112 | #options.debug = True |
| 113 | |
| 114 | if not options.debug: |
| 115 | sys.tracebacklimit = 0 |
| 116 | |
| 117 | if options.test: |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 118 | ret_code = RunTests(options.debug) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 119 | |
| 120 | elif options.test_coverage: |
| 121 | RunTestCoverage() |
| 122 | |
| 123 | elif options.full_help: |
| 124 | pager = os.getenv('PAGER') |
| 125 | if not pager: |
| 126 | pager = 'more' |
| 127 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 128 | 'README') |
| 129 | command.Run(pager, fname) |
| 130 | |
| 131 | else: |
| 132 | try: |
| 133 | ret_code = control.Binman(options, args) |
| 134 | except Exception as e: |
| 135 | print 'binman: %s' % e |
| 136 | if options.debug: |
| 137 | print |
| 138 | traceback.print_exc() |
| 139 | ret_code = 1 |
| 140 | return ret_code |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 145 | ret_code = RunBinman(options, args) |
| 146 | sys.exit(ret_code) |