binman: Pass the toolpath to tests
[oweals/u-boot.git] / tools / binman / binman.py
1 #!/usr/bin/env python2
2 # SPDX-License-Identifier: GPL-2.0+
3
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
6 #
7 # Creates binary images from input files controlled by a description
8 #
9
10 """See README for more information"""
11
12 from __future__ import print_function
13
14 from distutils.sysconfig import get_python_lib
15 import glob
16 import multiprocessing
17 import os
18 import site
19 import sys
20 import traceback
21 import unittest
22
23 # Bring in the patman and dtoc libraries
24 our_path = os.path.dirname(os.path.realpath(__file__))
25 for dirname in ['../patman', '../dtoc', '..', '../concurrencytest']:
26     sys.path.insert(0, os.path.join(our_path, dirname))
27
28 # Bring in the libfdt module
29 sys.path.insert(0, 'scripts/dtc/pylibfdt')
30 sys.path.insert(0, os.path.join(our_path,
31                 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
32
33 # When running under python-coverage on Ubuntu 16.04, the dist-packages
34 # directories are dropped from the python path. Add them in so that we can find
35 # the elffile module. We could use site.getsitepackages() here but unfortunately
36 # that is not available in a virtualenv.
37 sys.path.append(get_python_lib())
38
39 import cmdline
40 import command
41 use_concurrent = True
42 try:
43     from concurrencytest import ConcurrentTestSuite, fork_for_tests
44 except:
45     use_concurrent = False
46 import control
47 import test_util
48
49 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
50     """Run the functional tests and any embedded doctests
51
52     Args:
53         debug: True to enable debugging, which shows a full stack trace on error
54         verbosity: Verbosity level to use
55         test_preserve_dirs: True to preserve the input directory used by tests
56             so that it can be examined afterwards (only useful for debugging
57             tests). If a single test is selected (in args[0]) it also preserves
58             the output directory for this test. Both directories are displayed
59             on the command line.
60         processes: Number of processes to use to run tests (None=same as #CPUs)
61         args: List of positional args provided to binman. This can hold a test
62             name to execute (as in 'binman -t testSections', for example)
63         toolpath: List of paths to use for tools
64     """
65     import elf_test
66     import entry_test
67     import fdt_test
68     import ftest
69     import image_test
70     import test
71     import doctest
72
73     result = unittest.TestResult()
74     for module in []:
75         suite = doctest.DocTestSuite(module)
76         suite.run(result)
77
78     sys.argv = [sys.argv[0]]
79     if debug:
80         sys.argv.append('-D')
81     if verbosity:
82         sys.argv.append('-v%d' % verbosity)
83     if toolpath:
84         for path in toolpath:
85             sys.argv += ['--toolpath', path]
86
87     # Run the entry tests first ,since these need to be the first to import the
88     # 'entry' module.
89     test_name = args and args[0] or None
90     suite = unittest.TestSuite()
91     loader = unittest.TestLoader()
92     for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
93                    elf_test.TestElf, image_test.TestImage):
94         # Test the test module about our arguments, if it is interested
95         if hasattr(module, 'setup_test_args'):
96             setup_test_args = getattr(module, 'setup_test_args')
97             setup_test_args(preserve_indir=test_preserve_dirs,
98                 preserve_outdirs=test_preserve_dirs and test_name is not None,
99                 toolpath=toolpath)
100         if test_name:
101             try:
102                 suite.addTests(loader.loadTestsFromName(test_name, module))
103             except AttributeError:
104                 continue
105         else:
106             suite.addTests(loader.loadTestsFromTestCase(module))
107     if use_concurrent and processes != 1:
108         concurrent_suite = ConcurrentTestSuite(suite,
109                 fork_for_tests(processes or multiprocessing.cpu_count()))
110         concurrent_suite.run(result)
111     else:
112         suite.run(result)
113
114     # Remove errors which just indicate a missing test. Since Python v3.5 If an
115     # ImportError or AttributeError occurs while traversing name then a
116     # synthetic test that raises that error when run will be returned. These
117     # errors are included in the errors accumulated by result.errors.
118     if test_name:
119         errors = []
120         for test, err in result.errors:
121             if ("has no attribute '%s'" % test_name) not in err:
122                 errors.append((test, err))
123             result.testsRun -= 1
124         result.errors = errors
125
126     print(result)
127     for test, err in result.errors:
128         print(test.id(), err)
129     for test, err in result.failures:
130         print(err, result.failures)
131     if result.skipped:
132         print('%d binman test%s SKIPPED:' %
133               (len(result.skipped), 's' if len(result.skipped) > 1 else ''))
134         for skip_info in result.skipped:
135             print('%s: %s' % (skip_info[0], skip_info[1]))
136     if result.errors or result.failures:
137         print('binman tests FAILED')
138         return 1
139     return 0
140
141 def GetEntryModules(include_testing=True):
142     """Get a set of entry class implementations
143
144     Returns:
145         Set of paths to entry class filenames
146     """
147     glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
148     return set([os.path.splitext(os.path.basename(item))[0]
149                 for item in glob_list
150                 if include_testing or '_testing' not in item])
151
152 def RunTestCoverage():
153     """Run the tests and check that we get 100% coverage"""
154     glob_list = GetEntryModules(False)
155     all_set = set([os.path.splitext(os.path.basename(item))[0]
156                    for item in glob_list if '_testing' not in item])
157     test_util.RunTestCoverage('tools/binman/binman.py', None,
158             ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
159             options.build_dir, all_set)
160
161 def RunBinman(options, args):
162     """Main entry point to binman once arguments are parsed
163
164     Args:
165         options: Command-line options
166         args: Non-option arguments
167     """
168     ret_code = 0
169
170     if not options.debug:
171         sys.tracebacklimit = 0
172
173     if options.test:
174         ret_code = RunTests(options.debug, options.verbosity, options.processes,
175                             options.test_preserve_dirs, args[1:],
176                             options.toolpath)
177
178     elif options.test_coverage:
179         RunTestCoverage()
180
181     elif options.entry_docs:
182         control.WriteEntryDocs(GetEntryModules())
183
184     else:
185         try:
186             ret_code = control.Binman(options, args)
187         except Exception as e:
188             print('binman: %s' % e)
189             if options.debug:
190                 print()
191                 traceback.print_exc()
192             ret_code = 1
193     return ret_code
194
195
196 if __name__ == "__main__":
197     (options, args) = cmdline.ParseArgs(sys.argv)
198     ret_code = RunBinman(options, args)
199     sys.exit(ret_code)