blob: cf83edfd04446eed50da7c57469ddc7e84e742f6 [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
13import 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 Glass7feccfd2017-06-20 21:28:49 -060020for dirname in ['../patman', '../dtoc', '..']:
21 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glassbf7fd502016-11-25 20:15:51 -070022
Simon Glassb4360202017-05-27 07:38:22 -060023# Bring in the libfdt module
Masahiro Yamada15b97f52017-10-17 13:42:43 +090024sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glassb4360202017-05-27 07:38:22 -060025
Simon Glassbf7fd502016-11-25 20:15:51 -070026# Also allow entry-type modules to be brought in from the etype directory.
Simon Glass7feccfd2017-06-20 21:28:49 -060027sys.path.insert(0, os.path.join(our_path, 'etype'))
Simon Glassbf7fd502016-11-25 20:15:51 -070028
29import cmdline
30import command
31import control
32
33def RunTests():
34 """Run the functional tests and any embedded doctests"""
35 import entry_test
36 import fdt_test
Simon Glass680e3312017-11-12 21:52:08 -070037 import ftest
Simon Glassbf7fd502016-11-25 20:15:51 -070038 import test
39 import doctest
40
41 result = unittest.TestResult()
42 for module in []:
43 suite = doctest.DocTestSuite(module)
44 suite.run(result)
45
46 sys.argv = [sys.argv[0]]
Simon Glass934cdcf2017-11-12 21:52:21 -070047
48 # Run the entry tests first ,since these need to be the first to import the
49 # 'entry' module.
50 suite = unittest.TestLoader().loadTestsFromTestCase(entry_test.TestEntry)
51 suite.run(result)
52 for module in (ftest.TestFunctional, fdt_test.TestFdt):
Simon Glassbf7fd502016-11-25 20:15:51 -070053 suite = unittest.TestLoader().loadTestsFromTestCase(module)
54 suite.run(result)
55
56 print result
57 for test, err in result.errors:
58 print test.id(), err
59 for test, err in result.failures:
60 print err
61
62def RunTestCoverage():
63 """Run the tests and check that we get 100% coverage"""
64 # This uses the build output from sandbox_spl to get _libfdt.so
Simon Glass5a3f2222017-11-12 21:52:19 -070065 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
Simon Glassbf7fd502016-11-25 20:15:51 -070066 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
67 'tools/binman/binman.py -t' % options.build_dir)
68 os.system(cmd)
69 stdout = command.Output('coverage', 'report')
70 coverage = stdout.splitlines()[-1].split(' ')[-1]
71 if coverage != '100%':
72 print stdout
73 print "Type 'coverage html' to get a report in htmlcov/index.html"
74 raise ValueError('Coverage error: %s, but should be 100%%' % coverage)
75
76
77def RunBinman(options, args):
78 """Main entry point to binman once arguments are parsed
79
80 Args:
81 options: Command-line options
82 args: Non-option arguments
83 """
84 ret_code = 0
85
86 # For testing: This enables full exception traces.
87 #options.debug = True
88
89 if not options.debug:
90 sys.tracebacklimit = 0
91
92 if options.test:
93 RunTests()
94
95 elif options.test_coverage:
96 RunTestCoverage()
97
98 elif options.full_help:
99 pager = os.getenv('PAGER')
100 if not pager:
101 pager = 'more'
102 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
103 'README')
104 command.Run(pager, fname)
105
106 else:
107 try:
108 ret_code = control.Binman(options, args)
109 except Exception as e:
110 print 'binman: %s' % e
111 if options.debug:
112 print
113 traceback.print_exc()
114 ret_code = 1
115 return ret_code
116
117
118if __name__ == "__main__":
119 (options, args) = cmdline.ParseArgs(sys.argv)
120 ret_code = RunBinman(options, args)
121 sys.exit(ret_code)