blob: 74862b1146eb69dab374c3102a80a877e4c407a1 [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
29
Simon Glass5666f9a2018-06-01 09:38:18 -060030def RunTests(debug, args):
31 """Run the functional tests and any embedded doctests
32
33 Args:
34 debug: True to enable debugging, which shows a full stack trace on error
35 args: List of positional args provided to binman. This can hold a test
36 name to execute (as in 'binman -t testSections', for example)
37 """
Simon Glass24ad3652017-11-13 18:54:54 -070038 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070039 import entry_test
40 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070041 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070042 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070043 import test
44 import doctest
45
46 result = unittest.TestResult()
47 for module in []:
48 suite = doctest.DocTestSuite(module)
49 suite.run(result)
50
51 sys.argv = [sys.argv[0]]
Simon Glass075a45c2017-11-13 18:55:00 -070052 if debug:
53 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070054
55 # Run the entry tests first ,since these need to be the first to import the
56 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060057 test_name = args and args[0] or None
Simon Glass02e0fc62018-07-06 10:27:18 -060058 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
59 elf_test.TestElf, image_test.TestImage):
Simon Glass5666f9a2018-06-01 09:38:18 -060060 if test_name:
61 try:
Simon Glass02e0fc62018-07-06 10:27:18 -060062 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
Simon Glass5666f9a2018-06-01 09:38:18 -060063 except AttributeError:
64 continue
65 else:
66 suite = unittest.TestLoader().loadTestsFromTestCase(module)
Simon Glass2574ef62016-11-25 20:15:51 -070067 suite.run(result)
68
69 print result
70 for test, err in result.errors:
71 print test.id(), err
72 for test, err in result.failures:
Simon Glass9ba021c2017-11-12 21:52:29 -070073 print err, result.failures
74 if result.errors or result.failures:
75 print 'binman tests FAILED'
76 return 1
77 return 0
Simon Glass2574ef62016-11-25 20:15:51 -070078
79def RunTestCoverage():
80 """Run the tests and check that we get 100% coverage"""
81 # This uses the build output from sandbox_spl to get _libfdt.so
Tom Rinic2a849d2018-07-06 10:27:14 -060082 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools python-coverage run '
Simon Glass2574ef62016-11-25 20:15:51 -070083 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
84 'tools/binman/binman.py -t' % options.build_dir)
85 os.system(cmd)
Tom Rinic2a849d2018-07-06 10:27:14 -060086 stdout = command.Output('python-coverage', 'report')
Simon Glass2c3cf452017-11-12 21:52:24 -070087 lines = stdout.splitlines()
88
89 test_set= set([os.path.basename(line.split()[0])
90 for line in lines if '/etype/' in line])
91 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
Tom Rinic2a849d2018-07-06 10:27:14 -060092 all_set = set([os.path.splitext(os.path.basename(item))[0]
93 for item in glob_list if '_testing' not in item])
Simon Glass2c3cf452017-11-12 21:52:24 -070094 missing_list = all_set
95 missing_list.difference_update(test_set)
Simon Glass2c3cf452017-11-12 21:52:24 -070096 coverage = lines[-1].split(' ')[-1]
97 ok = True
98 if missing_list:
99 print 'Missing tests for %s' % (', '.join(missing_list))
Tom Rinic2a849d2018-07-06 10:27:14 -0600100 print stdout
Simon Glass2c3cf452017-11-12 21:52:24 -0700101 ok = False
Simon Glass2574ef62016-11-25 20:15:51 -0700102 if coverage != '100%':
103 print stdout
104 print "Type 'coverage html' to get a report in htmlcov/index.html"
Simon Glass2c3cf452017-11-12 21:52:24 -0700105 print 'Coverage error: %s, but should be 100%%' % coverage
106 ok = False
107 if not ok:
108 raise ValueError('Test coverage failure')
Simon Glass2574ef62016-11-25 20:15:51 -0700109
110def RunBinman(options, args):
111 """Main entry point to binman once arguments are parsed
112
113 Args:
114 options: Command-line options
115 args: Non-option arguments
116 """
117 ret_code = 0
118
119 # For testing: This enables full exception traces.
120 #options.debug = True
121
122 if not options.debug:
123 sys.tracebacklimit = 0
124
125 if options.test:
Simon Glass5666f9a2018-06-01 09:38:18 -0600126 ret_code = RunTests(options.debug, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700127
128 elif options.test_coverage:
129 RunTestCoverage()
130
131 elif options.full_help:
132 pager = os.getenv('PAGER')
133 if not pager:
134 pager = 'more'
135 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
136 'README')
137 command.Run(pager, fname)
138
139 else:
140 try:
141 ret_code = control.Binman(options, args)
142 except Exception as e:
143 print 'binman: %s' % e
144 if options.debug:
145 print
146 traceback.print_exc()
147 ret_code = 1
148 return ret_code
149
150
151if __name__ == "__main__":
152 (options, args) = cmdline.ParseArgs(sys.argv)
153 ret_code = RunBinman(options, args)
154 sys.exit(ret_code)