blob: aa513962663497245e6c4d339583b4a369fb7a8f [file] [log] [blame]
Jörg Krause66a7a242017-03-06 21:07:11 +01001#!/usr/bin/env python2
Simon Glassbf7fd502016-11-25 20:15:51 -07002
3# Copyright (c) 2016 Google, Inc
4# Written by Simon Glass <sjg@chromium.org>
5#
6# SPDX-License-Identifier: GPL-2.0+
7#
8# Creates binary images from input files controlled by a description
9#
10
11"""See README for more information"""
12
Simon Glassa25ebed2017-11-12 21:52:24 -070013import glob
Simon Glassbf7fd502016-11-25 20:15:51 -070014import os
15import sys
16import traceback
17import unittest
18
19# Bring in the patman and dtoc libraries
20our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass7feccfd2017-06-20 21:28:49 -060021for dirname in ['../patman', '../dtoc', '..']:
22 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070023
Simon Glassb4360202017-05-27 07:38:22 -060024# Bring in the libfdt module
Masahiro Yamada15b97f52017-10-17 13:42:43 +090025sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glassb4360202017-05-27 07:38:22 -060026
Simon Glassbf7fd502016-11-25 20:15:51 -070027# Also allow entry-type modules to be brought in from the etype directory.
Simon Glass7feccfd2017-06-20 21:28:49 -060028sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -070029
30import cmdline
31import command
32import control
33
Simon Glass7fe91732017-11-13 18:55:00 -070034def RunTests(debug):
Simon Glassbf7fd502016-11-25 20:15:51 -070035 """Run the functional tests and any embedded doctests"""
Simon Glassb50e5612017-11-13 18:54:54 -070036 import elf_test
Simon Glassbf7fd502016-11-25 20:15:51 -070037 import entry_test
38 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070039 import ftest
Simon Glassbf7fd502016-11-25 20:15:51 -070040 import test
41 import doctest
42
43 result = unittest.TestResult()
44 for module in []:
45 suite = doctest.DocTestSuite(module)
46 suite.run(result)
47
48 sys.argv = [sys.argv[0]]
Simon Glass7fe91732017-11-13 18:55:00 -070049 if debug:
50 sys.argv.append('-D')
Simon Glass934cdcf2017-11-12 21:52:21 -070051
52 # Run the entry tests first ,since these need to be the first to import the
53 # 'entry' module.
54 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
55 suite.run(result)
Simon Glassb50e5612017-11-13 18:54:54 -070056 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf):
Simon Glassbf7fd502016-11-25 20:15:51 -070057 suite = unittest.TestLoader().loadTestsFromTestCase(module)
58 suite.run(result)
59
60 print result
61 for test, err in result.errors:
62 print test.id(), err
63 for test, err in result.failures:
Simon Glass9677faa2017-11-12 21:52:29 -070064 print err, result.failures
65 if result.errors or result.failures:
66 print 'binman tests FAILED'
67 return 1
68 return 0
Simon Glassbf7fd502016-11-25 20:15:51 -070069
70def RunTestCoverage():
71 """Run the tests and check that we get 100% coverage"""
72 # This uses the build output from sandbox_spl to get _libfdt.so
Simon Glass5a3f2222017-11-12 21:52:19 -070073 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
Simon Glassbf7fd502016-11-25 20:15:51 -070074 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
75 'tools/binman/binman.py -t' % options.build_dir)
76 os.system(cmd)
77 stdout = command.Output('coverage', 'report')
Simon Glassa25ebed2017-11-12 21:52:24 -070078 lines = stdout.splitlines()
79
80 test_set= set([os.path.basename(line.split()[0])
81 for line in lines if '/etype/' in line])
82 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
83 all_set = set([os.path.basename(item) for item in glob_list])
84 missing_list = all_set
85 missing_list.difference_update(test_set)
86 missing_list.remove('_testing.py')
87 coverage = lines[-1].split(' ')[-1]
88 ok = True
89 if missing_list:
90 print 'Missing tests for %s' % (', '.join(missing_list))
91 ok = False
Simon Glassbf7fd502016-11-25 20:15:51 -070092 if coverage != '100%':
93 print stdout
94 print "Type 'coverage html' to get a report in htmlcov/index.html"
Simon Glassa25ebed2017-11-12 21:52:24 -070095 print 'Coverage error: %s, but should be 100%%' % coverage
96 ok = False
97 if not ok:
98 raise ValueError('Test coverage failure')
Simon Glassbf7fd502016-11-25 20:15:51 -070099
100def 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 Glass7fe91732017-11-13 18:55:00 -0700116 ret_code = RunTests(options.debug)
Simon Glassbf7fd502016-11-25 20:15:51 -0700117
118 elif options.test_coverage:
119 RunTestCoverage()
120
121 elif options.full_help:
122 pager = os.getenv('PAGER')
123 if not pager:
124 pager = 'more'
125 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
126 'README')
127 command.Run(pager, fname)
128
129 else:
130 try:
131 ret_code = control.Binman(options, args)
132 except Exception as e:
133 print 'binman: %s' % e
134 if options.debug:
135 print
136 traceback.print_exc()
137 ret_code = 1
138 return ret_code
139
140
141if __name__ == "__main__":
142 (options, args) = cmdline.ParseArgs(sys.argv)
143 ret_code = RunBinman(options, args)
144 sys.exit(ret_code)