blob: b179121b1c884b054a27b16036e9f09e29660111 [file] [log] [blame]
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -07001# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Tool to run ebuild unittests."""
6
7from __future__ import print_function
8
9import os
10
11from chromite.lib import commandline
12from chromite.lib import chroot_util
13from chromite.lib import cros_build_lib
14from chromite.lib import cros_logging as logging
15from chromite.lib import osutils
16from chromite.lib import workon_helper
17from chromite.lib import portage_util
18
19
20def ParseArgs(argv):
21 """Parse arguments.
22
23 Args:
24 argv: array of arguments passed to the script.
25 """
26 parser = commandline.ArgumentParser(description=__doc__)
27
28 target = parser.add_mutually_exclusive_group(required=True)
29 target.add_argument('--sysroot', type='path', help='Path to the sysroot.')
30 target.add_argument('--board', help='Board name.')
31
32 parser.add_argument('--pretend', default=False, action='store_true',
33 help='Show the list of packages to be tested and return.')
34 parser.add_argument('--noinstalled_only', dest='installed', default=True,
35 action='store_false',
36 help='Test all testable packages, even if they are not '
37 'currently installed.')
38 parser.add_argument('--package_file', type='path',
39 help='Path to a file containing the list of packages '
40 'that should be tested.')
41 parser.add_argument('--blacklist_packages', dest='package_blacklist',
42 help='Space-separated list of blacklisted packages.')
43 parser.add_argument('--packages',
44 help='Space-separated list of packages to test.')
45 parser.add_argument('--nowithdebug', action='store_true',
46 help="Don't build the tests with USE=cros-debug")
47
48 options = parser.parse_args(argv)
49 options.Freeze()
50 return options
51
52
53def main(argv):
54 opts = ParseArgs(argv)
55
56 cros_build_lib.AssertInsideChroot()
57
58 sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board)
59 package_blacklist = portage_util.UNITTEST_PACKAGE_BLACKLIST
60 if opts.package_blacklist:
61 package_blacklist |= set(opts.package_blacklist.split())
62
63 packages = set()
64 # The list of packages to test can be passed as a file containing a
65 # space-separated list of package names.
66 # This is used by the builder to test only the packages that were upreved.
67 if opts.package_file and os.path.exists(opts.package_file):
68 packages = set(osutils.ReadFile(opts.package_file).split())
69
70 if opts.packages:
71 packages |= set(opts.packages.split())
72
73 # If no packages were specified, use all testable packages.
74 if not (opts.packages or opts.package_file):
75 workon = workon_helper.WorkonHelper(sysroot)
76 packages = (workon.InstalledWorkonAtoms() if opts.installed
77 else workon.ListAtoms(use_all=True))
78
79 for cp in packages & package_blacklist:
80 logging.info('Skipping blacklisted package %s.', cp)
81
82 packages = packages - package_blacklist
83 pkg_with_test = portage_util.PackagesWithTest(sysroot, packages)
84
85 if packages - pkg_with_test:
86 logging.warning('The following packages do not have tests:')
87 logging.warning('\n'.join(sorted(packages - pkg_with_test)))
88
89 if opts.pretend:
90 print('\n'.join(sorted(pkg_with_test)))
91 return
92
93 env = None
94 if opts.nowithdebug:
95 use_flags = os.environ.get('USE', '')
96 use_flags += ' -cros-debug'
97 env = {'USE': use_flags}
98
99 try:
100 chroot_util.RunUnittests(sysroot, pkg_with_test, extra_env=env)
101 except cros_build_lib.RunCommandError:
102 logging.error('Unittests failed.')
103 raise