blob: 7ad4d3030b90115928633c807280e8edc5517914 [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
34def RunTests():
35 """Run the functional tests and any embedded doctests"""
36 import entry_test
37 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070038 import ftest
Simon Glass2574ef62016-11-25 20:15:51 -070039 import test
40 import doctest
41
42 result = unittest.TestResult()
43 for module in []:
44 suite = doctest.DocTestSuite(module)
45 suite.run(result)
46
47 sys.argv = [sys.argv[0]]
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)
53 for module in (ftest.TestFunctional, fdt_test.TestFdt):
Simon Glass2574ef62016-11-25 20:15:51 -070054 suite = unittest.TestLoader().loadTestsFromTestCase(module)
55 suite.run(result)
56
57 print result
58 for test, err in result.errors:
59 print test.id(), err
60 for test, err in result.failures:
61 print err
62
63def RunTestCoverage():
64 """Run the tests and check that we get 100% coverage"""
65 # This uses the build output from sandbox_spl to get _libfdt.so
Simon Glassc303b9a2017-11-12 21:52:19 -070066 cmd = ('PYTHONPATH=$PYTHONPATH:%s/sandbox_spl/tools coverage run '
Simon Glass2574ef62016-11-25 20:15:51 -070067 '--include "tools/binman/*.py" --omit "*test*,*binman.py" '
68 'tools/binman/binman.py -t' % options.build_dir)
69 os.system(cmd)
70 stdout = command.Output('coverage', 'report')
Simon Glass2c3cf452017-11-12 21:52:24 -070071 lines = stdout.splitlines()
72
73 test_set= set([os.path.basename(line.split()[0])
74 for line in lines if '/etype/' in line])
75 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
76 all_set = set([os.path.basename(item) for item in glob_list])
77 missing_list = all_set
78 missing_list.difference_update(test_set)
79 missing_list.remove('_testing.py')
80 coverage = lines[-1].split(' ')[-1]
81 ok = True
82 if missing_list:
83 print 'Missing tests for %s' % (', '.join(missing_list))
84 ok = False
Simon Glass2574ef62016-11-25 20:15:51 -070085 if coverage != '100%':
86 print stdout
87 print "Type 'coverage html' to get a report in htmlcov/index.html"
Simon Glass2c3cf452017-11-12 21:52:24 -070088 print 'Coverage error: %s, but should be 100%%' % coverage
89 ok = False
90 if not ok:
91 raise ValueError('Test coverage failure')
Simon Glass2574ef62016-11-25 20:15:51 -070092
93def RunBinman(options, args):
94 """Main entry point to binman once arguments are parsed
95
96 Args:
97 options: Command-line options
98 args: Non-option arguments
99 """
100 ret_code = 0
101
102 # For testing: This enables full exception traces.
103 #options.debug = True
104
105 if not options.debug:
106 sys.tracebacklimit = 0
107
108 if options.test:
109 RunTests()
110
111 elif options.test_coverage:
112 RunTestCoverage()
113
114 elif options.full_help:
115 pager = os.getenv('PAGER')
116 if not pager:
117 pager = 'more'
118 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
119 'README')
120 command.Run(pager, fname)
121
122 else:
123 try:
124 ret_code = control.Binman(options, args)
125 except Exception as e:
126 print 'binman: %s' % e
127 if options.debug:
128 print
129 traceback.print_exc()
130 ret_code = 1
131 return ret_code
132
133
134if __name__ == "__main__":
135 (options, args) = cmdline.ParseArgs(sys.argv)
136 ret_code = RunBinman(options, args)
137 sys.exit(ret_code)