blob: 1c8e8dbff65a04dc7b07828a3dc579bb97bdc758 [file] [log] [blame]
Jörg Krausecd6d46e2017-03-06 21:07:11 +01001#!/usr/bin/env python2
Simon Glass2574ef62016-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 Glass2c3cf452017-11-12 21:52:24 -070013import glob
Simon Glass2574ef62016-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 Glassdde3e712017-06-20 21:28:49 -060021for dirname in ['../patman', '../dtoc', '..']:
22 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070023
Simon Glass55901ff2017-05-27 07:38:22 -060024# Bring in the libfdt module
Masahiro Yamada47ae5392017-10-17 13:42:43 +090025sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glass55901ff2017-05-27 07:38:22 -060026
Simon Glass2574ef62016-11-25 20:15:51 -070027# Also allow entry-type modules to be brought in from the etype directory.
Simon Glassdde3e712017-06-20 21:28:49 -060028sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glass2574ef62016-11-25 20:15:51 -070029
30import cmdline
31import command
32import control
33
Simon Glass075a45c2017-11-13 18:55:00 -070034def RunTests(debug):
Simon Glass2574ef62016-11-25 20:15:51 -070035 """Run the functional tests and any embedded doctests"""
Simon Glass24ad3652017-11-13 18:54:54 -070036 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070037 import entry_test
38 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070039 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070040 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070041 import test
42 import doctest
43
44 result = unittest.TestResult()
45 for module in []:
46 suite = doctest.DocTestSuite(module)
47 suite.run(result)
48
49 sys.argv = [sys.argv[0]]
Simon Glass075a45c2017-11-13 18:55:00 -070050 if debug:
51 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070052
53 # Run the entry tests first ,since these need to be the first to import the
54 # 'entry' module.
55 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
56 suite.run(result)
Simon Glass4ca8e042017-11-13 18:55:01 -070057 for module in (ftest.TestFunctional, fdt_test.TestFdt, elf_test.TestElf,
58 image_test.TestImage):
Simon Glass2574ef62016-11-25 20:15:51 -070059 suite = unittest.TestLoader().loadTestsFromTestCase(module)
60 suite.run(result)
61
62 print result
63 for test, err in result.errors:
64 print test.id(), err
65 for test, err in result.failures:
Simon Glass9ba021c2017-11-12 21:52:29 -070066 print err, result.failures
67 if result.errors or result.failures:
68 print 'binman tests FAILED'
69 return 1
70 return 0
Simon Glass2574ef62016-11-25 20:15:51 -070071
72def RunTestCoverage():
73 """Run the tests and check that we get 100% coverage"""
74 # This uses the build output from sandbox_spl to get _libfdt.so
Simon Glassc303b9a2017-11-12 21:52:19 -070075 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
Simon Glass2574ef62016-11-25 20:15:51 -070076 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
77 'tools/binman/binman.py -t' % options.build_dir)
78 os.system(cmd)
79 stdout = command.Output('coverage', 'report')
Simon Glass2c3cf452017-11-12 21:52:24 -070080 lines = stdout.splitlines()
81
82 test_set= set([os.path.basename(line.split()[0])
83 for line in lines if '/etype/' in line])
84 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
85 all_set = set([os.path.basename(item) for item in glob_list])
86 missing_list = all_set
87 missing_list.difference_update(test_set)
88 missing_list.remove('_testing.py')
89 coverage = lines[-1].split(' ')[-1]
90 ok = True
91 if missing_list:
92 print 'Missing tests for %s' % (', '.join(missing_list))
93 ok = False
Simon Glass2574ef62016-11-25 20:15:51 -070094 if coverage != '100%':
95 print stdout
96 print "Type 'coverage html' to get a report in htmlcov/index.html"
Simon Glass2c3cf452017-11-12 21:52:24 -070097 print 'Coverage error: %s, but should be 100%%' % coverage
98 ok = False
99 if not ok:
100 raise ValueError('Test coverage failure')
Simon Glass2574ef62016-11-25 20:15:51 -0700101
102def RunBinman(options, args):
103 """Main entry point to binman once arguments are parsed
104
105 Args:
106 options: Command-line options
107 args: Non-option arguments
108 """
109 ret_code = 0
110
111 # For testing: This enables full exception traces.
112 #options.debug = True
113
114 if not options.debug:
115 sys.tracebacklimit = 0
116
117 if options.test:
Simon Glass075a45c2017-11-13 18:55:00 -0700118 ret_code = RunTests(options.debug)
Simon Glass2574ef62016-11-25 20:15:51 -0700119
120 elif options.test_coverage:
121 RunTestCoverage()
122
123 elif options.full_help:
124 pager = os.getenv('PAGER')
125 if not pager:
126 pager = 'more'
127 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
128 'README')
129 command.Run(pager, fname)
130
131 else:
132 try:
133 ret_code = control.Binman(options, args)
134 except Exception as e:
135 print 'binman: %s' % e
136 if options.debug:
137 print
138 traceback.print_exc()
139 ret_code = 1
140 return ret_code
141
142
143if __name__ == "__main__":
144 (options, args) = cmdline.ParseArgs(sys.argv)
145 ret_code = RunBinman(options, args)
146 sys.exit(ret_code)