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 | |
| 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 | 7feccfd | 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 | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 22 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 23 | # Bring in the libfdt module |
Masahiro Yamada | 15b97f5 | 2017-10-17 13:42:43 +0900 | [diff] [blame] | 24 | sys.path.insert(0, 'scripts/dtc/pylibfdt') |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 25 | |
Simon Glass | bf7fd50 | 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 | 7feccfd | 2017-06-20 21:28:49 -0600 | [diff] [blame] | 27 | sys.path.insert(0, os.path.join(our_path, 'etype')) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 28 | |
| 29 | import cmdline |
| 30 | import command |
| 31 | import control |
| 32 | |
| 33 | def RunTests(): |
| 34 | """Run the functional tests and any embedded doctests""" |
| 35 | import entry_test |
| 36 | import fdt_test |
Simon Glass | 680e331 | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 37 | import ftest |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | import test |
| 39 | import doctest |
| 40 | |
| 41 | result = unittest.TestResult() |
| 42 | for module in []: |
| 43 | suite = doctest.DocTestSuite(module) |
| 44 | suite.run(result) |
| 45 | |
| 46 | sys.argv = [sys.argv[0]] |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame^] | 47 | |
| 48 | # Run the entry tests first ,since these need to be the first to import the |
| 49 | # 'entry' module. |
| 50 | suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry) |
| 51 | suite.run(result) |
| 52 | for module in (ftest.TestFunctional, fdt_test.TestFdt): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 53 | suite = unittest.TestLoader().loadTestsFromTestCase(module) |
| 54 | suite.run(result) |
| 55 | |
| 56 | print result |
| 57 | for test, err in result.errors: |
| 58 | print test.id(), err |
| 59 | for test, err in result.failures: |
| 60 | print err |
| 61 | |
| 62 | def RunTestCoverage(): |
| 63 | """Run the tests and check that we get 100% coverage""" |
| 64 | # This uses the build output from sandbox_spl to get _libfdt.so |
Simon Glass | 5a3f222 | 2017-11-12 21:52:19 -0700 | [diff] [blame] | 65 | cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run ' |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 66 | '--include "tools/binman/*.py" --omit "*test*,*binman.py" ' |
| 67 | 'tools/binman/binman.py -t' % options.build_dir) |
| 68 | os.system(cmd) |
| 69 | stdout = command.Output('coverage', 'report') |
| 70 | coverage = stdout.splitlines()[-1].split(' ')[-1] |
| 71 | if coverage != '100%': |
| 72 | print stdout |
| 73 | print "Type 'coverage html' to get a report in htmlcov/index.html" |
| 74 | raise ValueError('Coverage error: %s, but should be 100%%' % coverage) |
| 75 | |
| 76 | |
| 77 | def RunBinman(options, args): |
| 78 | """Main entry point to binman once arguments are parsed |
| 79 | |
| 80 | Args: |
| 81 | options: Command-line options |
| 82 | args: Non-option arguments |
| 83 | """ |
| 84 | ret_code = 0 |
| 85 | |
| 86 | # For testing: This enables full exception traces. |
| 87 | #options.debug = True |
| 88 | |
| 89 | if not options.debug: |
| 90 | sys.tracebacklimit = 0 |
| 91 | |
| 92 | if options.test: |
| 93 | RunTests() |
| 94 | |
| 95 | elif options.test_coverage: |
| 96 | RunTestCoverage() |
| 97 | |
| 98 | elif options.full_help: |
| 99 | pager = os.getenv('PAGER') |
| 100 | if not pager: |
| 101 | pager = 'more' |
| 102 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 103 | 'README') |
| 104 | command.Run(pager, fname) |
| 105 | |
| 106 | else: |
| 107 | try: |
| 108 | ret_code = control.Binman(options, args) |
| 109 | except Exception as e: |
| 110 | print 'binman: %s' % e |
| 111 | if options.debug: |
| 112 | print |
| 113 | traceback.print_exc() |
| 114 | ret_code = 1 |
| 115 | return ret_code |
| 116 | |
| 117 | |
| 118 | if __name__ == "__main__": |
| 119 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 120 | ret_code = RunBinman(options, args) |
| 121 | sys.exit(ret_code) |