binman: Add a main program to the tests
[oweals/u-boot.git] / tools / binman / binman.py
1 #!/usr/bin/env python2
2
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
13 import os
14 import sys
15 import traceback
16 import unittest
17
18 # Bring in the patman and dtoc libraries
19 our_path = os.path.dirname(os.path.realpath(__file__))
20 for dirname in ['../patman', '../dtoc', '..']:
21     sys.path.insert(0, os.path.join(our_path, dirname))
22
23 # Bring in the libfdt module
24 sys.path.insert(0, 'scripts/dtc/pylibfdt')
25
26 # Also allow entry-type modules to be brought in from the etype directory.
27 sys.path.insert(0, os.path.join(our_path, 'etype'))
28
29 import cmdline
30 import command
31 import control
32
33 def RunTests():
34     """Run the functional tests and any embedded doctests"""
35     import entry_test
36     import fdt_test
37     import ftest
38     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]]
47
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):
53         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
62 def 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
65     cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
66             '--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
77 def 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
118 if __name__ == "__main__":
119     (options, args) = cmdline.ParseArgs(sys.argv)
120     ret_code = RunBinman(options, args)
121     sys.exit(ret_code)