Cleanup: Pass std::string as const reference from chromeos/
[chromium-blink-merge.git] / chromecast / tools / build / generate_test_lists.py
blob46e8b3d293dc4ed511d3d81bd5b9be0983b6e878
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Helper script to generate unit test lists for the Chromecast build scripts.
7 """
9 import glob
10 import optparse
11 import sys
14 def CombineList(test_files_dir, list_output_file, include_filters,
15 additional_runtime_options):
16 """Writes a unit test file in a format compatible for Chromecast scripts.
18 If include_filters is True, uses filters to create a test runner list
19 and also include additional options, if any.
20 Otherwise, creates a list only of the tests to build.
22 Args:
23 test_files_dir: Path to the intermediate directory containing tests/filters.
24 list_output_file: Path to write the unit test file out to.
25 include_filters: Whether or not to include the filters when generating
26 the test list.
27 """
29 # GYP targets may provide a numbered priority for the filename. Sort to
30 # use that priority.
31 test_files = sorted(glob.glob(test_files_dir + "/*.tests"))
32 filter_files = sorted(glob.glob(test_files_dir + "/*.filters"))
34 test_bin_set = set()
35 for test_filename in test_files:
36 with open(test_filename, "r") as test_file:
37 for test_file_line in test_file:
38 # Binary name may be a simple test target (cast_net_unittests) or be a
39 # qualified gyp path (../base.gyp:base_unittests).
40 test_binary_name = test_file_line.split(":")[-1].strip()
41 test_bin_set.add(test_binary_name)
43 test_filters = {}
44 if include_filters:
45 for filter_filename in filter_files:
46 with open(filter_filename, "r") as filter_file:
47 for filter_line in filter_file:
48 filter = filter_line.strip()
49 test_binary_name = filter.split(" ", 1)[0]
51 if test_binary_name not in test_bin_set:
52 raise Exception("Filter found for unknown target: " +
53 test_binary_name)
55 # Note: This may overwrite a previous rule. This is okay, since higher
56 # priority files are evaluated after lower priority files.
57 test_filters[test_binary_name] = filter
59 test_binaries = (
60 list(test_bin_set - set(test_filters.keys())) +
61 test_filters.values())
63 if additional_runtime_options:
64 lines = [
65 binary + " " + additional_runtime_options
66 for binary in test_binaries
68 else:
69 lines = test_binaries
70 with open(list_output_file, "w") as f:
71 f.write("\n".join(sorted(lines)))
74 def CreateList(inputs, list_output_file):
75 with open(list_output_file, "w") as f:
76 f.write("\n".join(inputs))
79 def DoMain(argv):
80 """Main method. Runs helper commands for generating unit test lists."""
81 parser = optparse.OptionParser(
82 """usage: %prog [<options>] <command> [<test names>]
84 Valid commands:
85 create_list prints all given test names/args to a file, one line
86 per string
87 pack_build packs all test files from the given output directory
88 into a single test list file
89 pack_run packs all test and filter files from the given
90 output directory into a single test list file
91 """)
92 parser.add_option("-o", action="store", dest="list_output_file",
93 help="Output path in which to write the test list.")
94 parser.add_option("-t", action="store", dest="test_files_dir",
95 help="Intermediate test list directory.")
96 parser.add_option("-a", action="store", dest="additional_runtime_options",
97 help="Additional options applied to all tests.")
98 options, inputs = parser.parse_args(argv)
100 list_output_file = options.list_output_file
101 test_files_dir = options.test_files_dir
102 additional_runtime_options = options.additional_runtime_options
104 if len(inputs) < 1:
105 parser.error("No command given.\n")
106 command = inputs[0]
107 test_names = inputs[1:]
109 if not list_output_file:
110 parser.error("Output path (-o) is required.\n")
112 if command == "create_list":
113 return CreateList(test_names, list_output_file)
115 if command == "pack_build":
116 if not test_files_dir:
117 parser.error("pack_build require a test files directory (-t).\n")
118 return CombineList(test_files_dir, list_output_file, False, None)
120 if command == "pack_run":
121 if not test_files_dir:
122 parser.error("pack_run require a test files directory (-t).\n")
123 return CombineList(test_files_dir, list_output_file, True,
124 additional_runtime_options)
126 parser.error("Invalid command specified.")
129 if __name__ == "__main__":
130 DoMain(sys.argv[1:])