• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2015 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Runs a script that can run as an isolate (or not).
7
8If optional argument --isolated-script-test-output=[FILENAME] is passed
9to the script, json is written to that file in the format detailed in
10//docs/testing/json-test-results-format.md.
11
12If optional argument --isolated-script-test-filter=[TEST_NAMES] is passed to
13the script, it should be a  double-colon-separated ("::") list of test names,
14to run just that subset of tests.
15
16This script is intended to be the base command invoked by the isolate,
17followed by a subsequent Python script. It could be generalized to
18invoke an arbitrary executable.
19"""
20
21import argparse
22import json
23import os
24import pprint
25import sys
26import tempfile
27
28
29# Add src/testing/ into sys.path for importing common.
30sys.path.append(
31    os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
32from scripts import common
33
34
35# Some harnesses understand the --isolated-script-test arguments
36# directly and prefer that they be passed through.
37KNOWN_ISOLATED_SCRIPT_TEST_RUNNERS = {'run_web_tests.py', 'run_webgpu_cts.py'}
38
39
40# Known typ test runners this script wraps. They need a different argument name
41# when selecting which tests to run.
42# TODO(dpranke): Detect if the wrapped test suite uses typ better.
43KNOWN_TYP_TEST_RUNNERS = {
44    'metrics_python_tests.py',
45    'monochrome_python_tests.py',
46    'run_blinkpy_tests.py',
47    'run_mac_signing_tests.py',
48    'run_mini_installer_tests.py',
49    'test_suite_all.py',  # //tools/grit:grit_python_unittests
50}
51
52KNOWN_TYP_VPYTHON3_TEST_RUNNERS = {
53    'monochrome_python_tests.py',
54    'run_polymer_tools_tests.py',
55    'test_suite_all.py',  # //tools/grit:grit_python_unittests
56}
57
58# pylint: disable=super-with-arguments
59
60class BareScriptTestAdapter(common.BaseIsolatedScriptArgsAdapter):
61  def __init__(self):
62    super().__init__()
63    # Arguments that are ignored, but added here because it's easier to ignore
64    # them than to update bot configs to not pass them.
65    common.add_emulator_args(self._parser)
66    self._parser.add_argument(
67        '--use-persistent-shell', action='store_true', help='Unused')
68
69
70class IsolatedScriptTestAdapter(common.BaseIsolatedScriptArgsAdapter):
71  def generate_sharding_args(self, total_shards, shard_index):
72    # This script only uses environment variable for sharding.
73    del total_shards, shard_index  # unused
74    return []
75
76  def generate_test_also_run_disabled_tests_args(self):
77    return ['--isolated-script-test-also-run-disabled-tests']
78
79  def generate_test_filter_args(self, test_filter_str):
80    return ['--isolated-script-test-filter=%s' % test_filter_str]
81
82  def generate_test_output_args(self, output):
83    return ['--isolated-script-test-output=%s' % output]
84
85  def generate_test_launcher_retry_limit_args(self, retry_limit):
86    return ['--isolated-script-test-launcher-retry-limit=%d' % retry_limit]
87
88  def generate_test_repeat_args(self, repeat_count):
89    return ['--isolated-script-test-repeat=%d' % repeat_count]
90
91
92class TypUnittestAdapter(common.BaseIsolatedScriptArgsAdapter):
93
94  def __init__(self):
95    super(TypUnittestAdapter, self).__init__()
96    self._temp_filter_file = None
97
98  def generate_sharding_args(self, total_shards, shard_index):
99    # This script only uses environment variable for sharding.
100    del total_shards, shard_index  # unused
101    return []
102
103  def generate_test_filter_args(self, test_filter_str):
104    filter_list = common.extract_filter_list(test_filter_str)
105    self._temp_filter_file = tempfile.NamedTemporaryFile(
106        mode='w', delete=False)
107    self._temp_filter_file.write('\n'.join(filter_list))
108    self._temp_filter_file.close()
109    arg_name = 'test-list'
110    if any(r in self.rest_args[0] for r in KNOWN_TYP_TEST_RUNNERS):
111      arg_name = 'file-list'
112
113    return ['--%s=' % arg_name + self._temp_filter_file.name]
114
115  def generate_test_output_args(self, output):
116    return ['--write-full-results-to', output]
117
118  def generate_test_launcher_retry_limit_args(self, retry_limit):
119    return ['--isolated-script-test-launcher-retry-limit=%d' % retry_limit]
120
121  def generate_test_repeat_args(self, repeat_count):
122    return ['--isolated-script-test-repeat=%d' % repeat_count]
123
124  def clean_up_after_test_run(self):
125    if self._temp_filter_file:
126      os.unlink(self._temp_filter_file.name)
127
128  def select_python_executable(self):
129    if any(r in self.rest_args[0] for r in KNOWN_TYP_VPYTHON3_TEST_RUNNERS):
130      return 'vpython3.bat' if sys.platform == 'win32' else 'vpython3'
131    return super(TypUnittestAdapter, self).select_python_executable()
132
133
134def main():
135  parser = argparse.ArgumentParser()
136  parser.add_argument('--script-type', choices=['isolated', 'typ', 'bare'])
137  args, _ = parser.parse_known_args()
138
139  kind = args.script_type
140  if not kind:
141    if any(r in sys.argv[1] for r in KNOWN_ISOLATED_SCRIPT_TEST_RUNNERS):
142      kind = 'isolated'
143    else:
144      kind = 'typ'
145
146  if kind == 'isolated':
147    adapter = IsolatedScriptTestAdapter()
148  elif kind == 'typ':
149    adapter = TypUnittestAdapter()
150  else:
151    adapter = BareScriptTestAdapter()
152  return adapter.run_test()
153
154# This is not really a "script test" so does not need to manually add
155# any additional compile targets.
156def main_compile_targets(args):
157  json.dump([], args.output)
158
159
160if __name__ == '__main__':
161  # Conform minimally to the protocol defined by ScriptTest.
162  if 'compile_targets' in sys.argv:
163    funcs = {
164      'run': None,
165      'compile_targets': main_compile_targets,
166    }
167    sys.exit(common.run_script(sys.argv[1:], funcs))
168  sys.exit(main())
169