Jörg Krause | 66a7a24 | 2017-03-06 21:07:11 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 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 | bf7fd50 | 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 | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 12 | import glob |
Simon Glass | bf7fd50 | 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 | 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 | import cmdline |
| 27 | import command |
| 28 | import control |
Simon Glass | ff1fd6c | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 29 | import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 30 | |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 31 | def RunTests(debug, args): |
| 32 | """Run the functional tests and any embedded doctests |
| 33 | |
| 34 | Args: |
| 35 | debug: True to enable debugging, which shows a full stack trace on error |
| 36 | args: List of positional args provided to binman. This can hold a test |
| 37 | name to execute (as in 'binman -t testSections', for example) |
| 38 | """ |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 39 | import elf_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | import entry_test |
| 41 | import fdt_test |
Simon Glass | 680e331 | 2017-11-12 21:52:08 -0700 | [diff] [blame] | 42 | import ftest |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 43 | import image_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 44 | import test |
| 45 | import doctest |
| 46 | |
| 47 | result = unittest.TestResult() |
| 48 | for module in []: |
| 49 | suite = doctest.DocTestSuite(module) |
| 50 | suite.run(result) |
| 51 | |
| 52 | sys.argv = [sys.argv[0]] |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 53 | if debug: |
| 54 | sys.argv.append('-D') |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 55 | |
| 56 | # Run the entry tests first ,since these need to be the first to import the |
| 57 | # 'entry' module. |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 58 | test_name = args and args[0] or None |
Simon Glass | 2cd0128 | 2018-07-06 10:27:18 -0600 | [diff] [blame] | 59 | for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt, |
| 60 | elf_test.TestElf, image_test.TestImage): |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 61 | if test_name: |
| 62 | try: |
Simon Glass | 2cd0128 | 2018-07-06 10:27:18 -0600 | [diff] [blame] | 63 | suite = unittest.TestLoader().loadTestsFromName(test_name, module) |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 64 | except AttributeError: |
| 65 | continue |
| 66 | else: |
| 67 | suite = unittest.TestLoader().loadTestsFromTestCase(module) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 68 | suite.run(result) |
| 69 | |
| 70 | print result |
| 71 | for test, err in result.errors: |
| 72 | print test.id(), err |
| 73 | for test, err in result.failures: |
Simon Glass | 9677faa | 2017-11-12 21:52:29 -0700 | [diff] [blame] | 74 | print err, result.failures |
| 75 | if result.errors or result.failures: |
| 76 | print 'binman tests FAILED' |
| 77 | return 1 |
| 78 | return 0 |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 79 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 80 | def GetEntryModules(include_testing=True): |
| 81 | """Get a set of entry class implementations |
| 82 | |
| 83 | Returns: |
| 84 | Set of paths to entry class filenames |
| 85 | """ |
| 86 | glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) |
| 87 | return set([os.path.splitext(os.path.basename(item))[0] |
| 88 | for item in glob_list |
| 89 | if include_testing or '_testing' not in item]) |
| 90 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 91 | def RunTestCoverage(): |
| 92 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 93 | glob_list = GetEntryModules(False) |
Tom Rini | 16d836c | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 94 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 95 | for item in glob_list if '_testing' not in item]) |
Simon Glass | ff1fd6c | 2018-07-06 10:27:23 -0600 | [diff] [blame] | 96 | test_util.RunTestCoverage('tools/binman/binman.py', None, |
| 97 | ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'], |
| 98 | options.build_dir, all_set) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 99 | |
| 100 | def RunBinman(options, args): |
| 101 | """Main entry point to binman once arguments are parsed |
| 102 | |
| 103 | Args: |
| 104 | options: Command-line options |
| 105 | args: Non-option arguments |
| 106 | """ |
| 107 | ret_code = 0 |
| 108 | |
| 109 | # For testing: This enables full exception traces. |
| 110 | #options.debug = True |
| 111 | |
| 112 | if not options.debug: |
| 113 | sys.tracebacklimit = 0 |
| 114 | |
| 115 | if options.test: |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 116 | ret_code = RunTests(options.debug, args[1:]) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 117 | |
| 118 | elif options.test_coverage: |
| 119 | RunTestCoverage() |
| 120 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 121 | elif options.entry_docs: |
| 122 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 123 | |
| 124 | else: |
| 125 | try: |
| 126 | ret_code = control.Binman(options, args) |
| 127 | except Exception as e: |
| 128 | print 'binman: %s' % e |
| 129 | if options.debug: |
| 130 | print |
| 131 | traceback.print_exc() |
| 132 | ret_code = 1 |
| 133 | return ret_code |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | (options, args) = cmdline.ParseArgs(sys.argv) |
| 138 | ret_code = RunBinman(options, args) |
| 139 | sys.exit(ret_code) |