blob: 52e02ed91b4f7de47b995adbbcc055ffb49b67b5 [file] [log] [blame]
Jörg Krausecd6d46e2017-03-06 21:07:11 +01001#!/usr/bin/env python2
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glass2574ef62016-11-25 20:15:51 -07007# Creates binary images from input files controlled by a description
8#
9
10"""See README for more information"""
11
Simon Glass2c3cf452017-11-12 21:52:24 -070012import glob
Simon Glass2574ef62016-11-25 20:15:51 -070013import os
14import sys
15import traceback
16import unittest
17
18# Bring in the patman and dtoc libraries
19our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glassdde3e712017-06-20 21:28:49 -060020for dirname in ['../patman', '../dtoc', '..']:
21 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070022
Simon Glass55901ff2017-05-27 07:38:22 -060023# Bring in the libfdt module
Masahiro Yamada47ae5392017-10-17 13:42:43 +090024sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glass55901ff2017-05-27 07:38:22 -060025
Simon Glass2574ef62016-11-25 20:15:51 -070026import cmdline
27import command
28import control
Simon Glass132be852018-07-06 10:27:23 -060029import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070030
Simon Glass5666f9a2018-06-01 09:38:18 -060031def 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 Glass24ad3652017-11-13 18:54:54 -070039 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070040 import entry_test
41 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070042 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070043 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070044 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 Glass075a45c2017-11-13 18:55:00 -070053 if debug:
54 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070055
56 # Run the entry tests first ,since these need to be the first to import the
57 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060058 test_name = args and args[0] or None
Simon Glass02e0fc62018-07-06 10:27:18 -060059 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
60 elf_test.TestElf, image_test.TestImage):
Simon Glass5666f9a2018-06-01 09:38:18 -060061 if test_name:
62 try:
Simon Glass02e0fc62018-07-06 10:27:18 -060063 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
Simon Glass5666f9a2018-06-01 09:38:18 -060064 except AttributeError:
65 continue
66 else:
67 suite = unittest.TestLoader().loadTestsFromTestCase(module)
Simon Glass2574ef62016-11-25 20:15:51 -070068 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 Glass9ba021c2017-11-12 21:52:29 -070074 print err, result.failures
75 if result.errors or result.failures:
76 print 'binman tests FAILED'
77 return 1
78 return 0
Simon Glass2574ef62016-11-25 20:15:51 -070079
80def RunTestCoverage():
81 """Run the tests and check that we get 100% coverage"""
Simon Glass2c3cf452017-11-12 21:52:24 -070082 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
Tom Rinic2a849d2018-07-06 10:27:14 -060083 all_set = set([os.path.splitext(os.path.basename(item))[0]
84 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -060085 test_util.RunTestCoverage('tools/binman/binman.py', None,
86 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
87 options.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -070088
89def RunBinman(options, args):
90 """Main entry point to binman once arguments are parsed
91
92 Args:
93 options: Command-line options
94 args: Non-option arguments
95 """
96 ret_code = 0
97
98 # For testing: This enables full exception traces.
99 #options.debug = True
100
101 if not options.debug:
102 sys.tracebacklimit = 0
103
104 if options.test:
Simon Glass5666f9a2018-06-01 09:38:18 -0600105 ret_code = RunTests(options.debug, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700106
107 elif options.test_coverage:
108 RunTestCoverage()
109
110 elif options.full_help:
111 pager = os.getenv('PAGER')
112 if not pager:
113 pager = 'more'
114 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
115 'README')
116 command.Run(pager, fname)
117
118 else:
119 try:
120 ret_code = control.Binman(options, args)
121 except Exception as e:
122 print 'binman: %s' % e
123 if options.debug:
124 print
125 traceback.print_exc()
126 ret_code = 1
127 return ret_code
128
129
130if __name__ == "__main__":
131 (options, args) = cmdline.ParseArgs(sys.argv)
132 ret_code = RunBinman(options, args)
133 sys.exit(ret_code)