blob: d49402a977e8c9034c5745b2cadfcdd3e718b993 [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 Glass075a45c2017-11-13 18:55:00 -070030def RunTests(debug):
Simon Glass2574ef62016-11-25 20:15:51 -070031 """Run the functional tests and any embedded doctests"""
Simon Glass24ad3652017-11-13 18:54:54 -070032 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070033 import entry_test
34 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070035 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070036 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070037 import test
38 import doctest
39
40 result = unittest.TestResult()
41 for module in []:
42 suite = doctest.DocTestSuite(module)
43 suite.run(result)
44
45 sys.argv = [sys.argv[0]]
Simon Glass075a45c2017-11-13 18:55:00 -070046 if debug:
47 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070048
49 # Run the entry tests first ,since these need to be the first to import the
50 # 'entry' module.
51 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
52 suite.run(result)
Simon Glass4ca8e042017-11-13 18:55:01 -070053 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf,
54 image_test.TestImage):
Simon Glass2574ef62016-11-25 20:15:51 -070055 suite = unittest.TestLoader().loadTestsFromTestCase(module)
56 suite.run(result)
57
58 print result
59 for test, err in result.errors:
60 print test.id(), err
61 for test, err in result.failures:
Simon Glass9ba021c2017-11-12 21:52:29 -070062 print err, result.failures
63 if result.errors or result.failures:
64 print 'binman tests FAILED'
65 return 1
66 return 0
Simon Glass2574ef62016-11-25 20:15:51 -070067
68def RunTestCoverage():
69 """Run the tests and check that we get 100% coverage"""
70 # This uses the build output from sandbox_spl to get _libfdt.so
Simon Glassc303b9a2017-11-12 21:52:19 -070071 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
Simon Glass2574ef62016-11-25 20:15:51 -070072 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
73 'tools/binman/binman.py -t' % options.build_dir)
74 os.system(cmd)
75 stdout = command.Output('coverage', 'report')
Simon Glass2c3cf452017-11-12 21:52:24 -070076 lines = stdout.splitlines()
77
78 test_set= set([os.path.basename(line.split()[0])
79 for line in lines if '/etype/' in line])
80 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
81 all_set = set([os.path.basename(item) for item in glob_list])
82 missing_list = all_set
83 missing_list.difference_update(test_set)
84 missing_list.remove('_testing.py')
85 coverage = lines[-1].split(' ')[-1]
86 ok = True
87 if missing_list:
88 print 'Missing tests for %s' % (', '.join(missing_list))
89 ok = False
Simon Glass2574ef62016-11-25 20:15:51 -070090 if coverage != '100%':
91 print stdout
92 print "Type 'coverage html' to get a report in htmlcov/index.html"
Simon Glass2c3cf452017-11-12 21:52:24 -070093 print 'Coverage error: %s, but should be 100%%' % coverage
94 ok = False
95 if not ok:
96 raise ValueError('Test coverage failure')
Simon Glass2574ef62016-11-25 20:15:51 -070097
98def RunBinman(options, args):
99 """Main entry point to binman once arguments are parsed
100
101 Args:
102 options: Command-line options
103 args: Non-option arguments
104 """
105 ret_code = 0
106
107 # For testing: This enables full exception traces.
108 #options.debug = True
109
110 if not options.debug:
111 sys.tracebacklimit = 0
112
113 if options.test:
Simon Glass075a45c2017-11-13 18:55:00 -0700114 ret_code = RunTests(options.debug)
Simon Glass2574ef62016-11-25 20:15:51 -0700115
116 elif options.test_coverage:
117 RunTestCoverage()
118
119 elif options.full_help:
120 pager = os.getenv('PAGER')
121 if not pager:
122 pager = 'more'
123 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
124 'README')
125 command.Run(pager, fname)
126
127 else:
128 try:
129 ret_code = control.Binman(options, args)
130 except Exception as e:
131 print 'binman: %s' % e
132 if options.debug:
133 print
134 traceback.print_exc()
135 ret_code = 1
136 return ret_code
137
138
139if __name__ == "__main__":
140 (options, args) = cmdline.ParseArgs(sys.argv)
141 ret_code = RunBinman(options, args)
142 sys.exit(ret_code)