blob: e39a79a8a0177e3e1f74c5558d6806dd8501e90e [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Mads Ager418d1ca2017-05-22 09:35:49 +02002# Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6# Convenience script for running tests. If no argument is given run all tests,
7# if an argument is given, run only tests with that pattern. This script will
8# force the tests to run, even if no input changed.
9
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020010import argparse
Ian Zerny5fffb0a2019-02-11 13:54:22 +010011import os
Ian Zerny24398bc2019-02-22 11:59:18 +010012import shutil
Rico Windf65a1d62017-06-30 09:41:56 +020013import subprocess
Mads Ager418d1ca2017-05-22 09:35:49 +020014import sys
Rico Windda6836e2018-12-07 12:32:03 +010015import time
Rico Wind06487ac2018-12-10 09:09:19 +010016import uuid
Ian Zerny5fffb0a2019-02-11 13:54:22 +010017
Ian Zerny3ced9262022-03-29 11:06:02 +020018import archive_desugar_jdk_libs
19import download_kotlin_dev
Ian Zerny5fffb0a2019-02-11 13:54:22 +010020import gradle
Ian Zerny3ced9262022-03-29 11:06:02 +020021import notify
Ian Zernyf271e8d2023-09-11 12:30:41 +020022import testing_state
Ian Zerny5fffb0a2019-02-11 13:54:22 +010023import utils
Jean-Marie Henaffce162f32017-10-04 10:39:27 +020024
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +010025if utils.is_python3():
26 import threading
27else:
28 import thread
29
Søren Gjessefe7c0112018-12-03 12:33:12 +010030ALL_ART_VMS = [
31 "default",
Søren Gjesseaa3eb742023-03-02 20:00:45 +010032 "14.0.0",
Søren Gjesse12a45f62022-02-04 12:05:27 +010033 "13.0.0",
Søren Gjesse953f7c42021-08-18 16:42:06 +020034 "12.0.0",
clementbera97d5cce2019-11-22 15:09:27 +010035 "10.0.0",
Søren Gjessefe7c0112018-12-03 12:33:12 +010036 "9.0.0",
37 "8.1.0",
38 "7.0.0",
39 "6.0.1",
40 "5.1.1",
41 "4.4.4",
42 "4.0.4"]
Mads Ager418d1ca2017-05-22 09:35:49 +020043
Rico Windda6836e2018-12-07 12:32:03 +010044# How often do we check for progress on the bots:
45# Should be long enough that a normal run would always have med progress
46# Should be short enough that we ensure that two calls are close enough
47# to happen before bot times out.
48# A false positiv, i.e., printing the stacks of non hanging processes
Rico Wind46cdf982021-09-27 19:53:45 +020049# is not a problem, no harm done except some logging in stdout.
Rico Windda6836e2018-12-07 12:32:03 +010050TIMEOUT_HANDLER_PERIOD = 60 * 18
51
Rico Wind06487ac2018-12-10 09:09:19 +010052BUCKET = 'r8-test-results'
53
Ian Zerny24398bc2019-02-22 11:59:18 +010054NUMBER_OF_TEST_REPORTS = 5
55REPORTS_PATH = os.path.join(utils.BUILD, 'reports')
56REPORT_INDEX = ['tests', 'test', 'index.html']
Ian Zernybcbd6c52019-10-29 15:27:04 +010057VALID_RUNTIMES = [
58 'none',
59 'jdk8',
60 'jdk9',
61 'jdk11',
Ian Zernyf0ce2c32022-03-28 15:39:32 +020062 'jdk17',
Søren Gjesse4b6b5fc2023-05-12 09:53:47 +020063 'jdk20',
Ian Zernybcbd6c52019-10-29 15:27:04 +010064] + [ 'dex-%s' % dexvm for dexvm in ALL_ART_VMS ]
Ian Zerny24398bc2019-02-22 11:59:18 +010065
Mads Ager418d1ca2017-05-22 09:35:49 +020066def ParseOptions():
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020067 result = argparse.ArgumentParser()
68 result.add_argument('--no-internal', '--no_internal',
Mads Ager418d1ca2017-05-22 09:35:49 +020069 help='Do not run Google internal tests.',
70 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020071 result.add_argument('--archive-failures', '--archive_failures',
Rico Winda94f01c2017-06-27 10:32:34 +020072 help='Upload test results to cloud storage on failure.',
73 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020074 result.add_argument('--archive-failures-file-name',
Morten Krogh-Jespersenef978ff2021-10-11 12:44:00 +020075 '--archive_failures_file_name',
76 help='Set file name for the archived failures file name',
77 default=uuid.uuid4())
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020078 result.add_argument('--only-internal', '--only_internal',
Mads Ager418d1ca2017-05-22 09:35:49 +020079 help='Only run Google internal tests.',
80 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020081 result.add_argument('--all-tests', '--all_tests',
Mads Ager418d1ca2017-05-22 09:35:49 +020082 help='Run tests in all configurations.',
83 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020084 result.add_argument('--slow-tests', '--slow_tests',
Christoffer Quist Adamsen748e4662019-08-23 14:53:49 +020085 help='Also run slow tests.',
86 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020087 result.add_argument('-v', '--verbose',
Mads Ager418d1ca2017-05-22 09:35:49 +020088 help='Print test stdout to, well, stdout.',
89 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020090 result.add_argument('--dex-vm', '--dex_vm',
Mads Ager418d1ca2017-05-22 09:35:49 +020091 help='The android version of the vm to use. "all" will run the tests on '
92 'all art vm versions (stopping after first failed execution)',
93 default="default",
94 choices=ALL_ART_VMS + ["all"])
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +020095 result.add_argument('--dex-vm-kind', '--dex_vm_kind',
Jean-Marie Henaffce162f32017-10-04 10:39:27 +020096 help='Whether to use host or target version of runtime',
97 default="host",
98 nargs=1,
99 choices=["host", "target"])
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200100 result.add_argument('--one-line-per-test', '--one_line_per_test',
Mads Ager418d1ca2017-05-22 09:35:49 +0200101 help='Print a line before a tests starts and after it ends to stdout.',
102 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200103 result.add_argument('--tool',
Tamas Kenezcfb2c052018-10-12 11:03:57 +0200104 help='Tool to run ART tests with: "r8" (default) or "d8" or "r8cf"'
105 ' (r8 w/CF-backend). Ignored if "--all_tests" enabled.',
106 default=None, choices=["r8", "d8", "r8cf"])
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200107 result.add_argument('--disable-assertions', '--disable_assertions', '-da',
Christoffer Quist Adamsen08da5e52023-09-22 09:37:37 +0200108 help='Disable Java assertions when running the compiler '
109 '(default enabled)',
Søren Gjesseaf1c5e22017-06-15 12:24:03 +0200110 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200111 result.add_argument('--with-code-coverage', '--with_code_coverage',
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200112 help='Enable code coverage with Jacoco.',
Sebastien Hertze2687b62017-07-25 11:16:04 +0200113 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200114 result.add_argument('--test-dir', '--test_dir',
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200115 help='Use a custom directory for the test artifacts instead of a'
116 ' temporary (which is automatically removed after the test).'
117 ' Note that the directory will not be cleared before the test.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200118 result.add_argument('--command-cache-dir', '--command_cache_dir',
Rico Windfc645a22023-06-14 12:37:18 +0200119 help='Cache command invocations to this directory, speeds up test runs',
Rico Wind8925a2d2023-06-14 12:43:59 +0200120 default=os.environ.get('R8_COMMAND_CACHE_DIR'))
Rico Windb28b9f72023-09-29 13:12:34 +0200121 result.add_argument('--command-cache-stats', '--command_cache_stats',
122 help='Collect and print statistics about the command cache.',
123 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200124 result.add_argument('--java-home', '--java_home',
Mikaël Peltier5c0a3232017-10-18 09:14:40 +0200125 help='Use a custom java version to run tests.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200126 result.add_argument('--java-max-memory-size', '--java_max_memory_size',
Rico Wind97b0a992019-08-30 11:09:15 +0200127 help='Set memory for running tests, default 4G',
Christoffer Quist Adamsen0a5f2ef2022-01-24 14:02:13 +0100128 default=os.environ.get('R8_JAVA_MAX_MEMORY_SIZE', '4G'))
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200129 result.add_argument('--test-namespace', '--test_namespace',
Rico Windba151112020-10-01 08:16:33 +0200130 help='Only run tests in this namespace. The namespace is relative to '
131 'com/android/tools/r8, e.g., desugar/desugaredlibrary',
132 default=None)
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200133 result.add_argument('--shard-count', '--shard_count',
Rico Wind8e2f7e42019-02-21 10:13:21 +0100134 help='We are running this many shards.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200135 result.add_argument('--shard-number', '--shard_number',
Rico Wind8e2f7e42019-02-21 10:13:21 +0100136 help='We are running this shard.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200137 result.add_argument('--generate-golden-files-to', '--generate_golden_files_to',
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200138 help='Store dex files produced by tests in the specified directory.'
139 ' It is aimed to be read on platforms with no host runtime available'
140 ' for comparison.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200141 result.add_argument('--use-golden-files-in', '--use_golden_files_in',
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200142 help='Download golden files hierarchy for this commit in the specified'
143 ' location and use them instead of executing on host runtime.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200144 result.add_argument('--no-r8lib', '--no_r8lib',
Søren Gjesse1956bdb2019-02-19 08:37:52 +0100145 default=False, action='store_true',
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100146 help='Run the tests on R8 full with relocated dependencies.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200147 result.add_argument('--no-arttests', '--no_arttests',
Rico Windbc820812022-05-31 09:19:56 +0200148 default=False, action='store_true',
149 help='Do not run the art tests.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200150 result.add_argument('--r8lib-no-deps', '--r8lib_no_deps',
Søren Gjesse1956bdb2019-02-19 08:37:52 +0100151 default=False, action='store_true',
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100152 help='Run the tests on r8lib without relocated dependencies.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200153 result.add_argument('--failed',
Ian Zerny24398bc2019-02-22 11:59:18 +0100154 default=False, action='store_true',
155 help='Run the tests that failed last execution.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200156 result.add_argument('--fail-fast', '--fail_fast',
Ian Zerny24398bc2019-02-22 11:59:18 +0100157 default=False, action='store_true',
158 help='Stop on first failure. Passes --fail-fast to gradle test runner.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200159 result.add_argument('--worktree',
Morten Krogh-Jespersen89d005b2019-10-15 13:32:23 +0200160 default=False, action='store_true',
161 help='Tests are run in worktree and should not use gradle user home.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200162 result.add_argument('--runtimes',
Ian Zernybcbd6c52019-10-29 15:27:04 +0100163 default=None,
164 help='Test parameter runtimes to use, separated by : (eg, none:jdk9).'
165 ' Special values include: all (for all runtimes)'
166 ' and empty (for no runtimes).')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200167 result.add_argument('--print-hanging-stacks', '--print_hanging_stacks',
168 default=-1, type=int, help='Print hanging stacks after timeout in seconds')
169 result.add_argument('--print-full-stacktraces', '--print_full_stacktraces',
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100170 default=False, action='store_true',
171 help='Print the full stacktraces without any filtering applied')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200172 result.add_argument(
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100173 '--print-obfuscated-stacktraces', '--print_obfuscated_stacktraces',
174 default=False, action='store_true',
175 help='Print the obfuscated stacktraces')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200176 result.add_argument(
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100177 '--debug-agent', '--debug_agent',
178 help='Enable Java debug agent and suspend compilation (default disabled)',
179 default=False,
180 action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200181 result.add_argument('--desugared-library-configuration',
Rico Winde1393092021-03-21 12:57:01 +0100182 '--desugared_library-configuration',
Søren Gjesseef195772021-03-11 16:04:42 +0100183 help='Use alternative desugared library configuration.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200184 result.add_argument('--desugared-library', '--desugared_library',
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100185 help='Build and use desugared library from GitHub.')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200186 result.add_argument('--print-times', '--print_times',
Rico Windf2f4c292021-04-23 07:06:13 +0200187 help='Print the execution time of the slowest tests..',
188 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200189 result.add_argument(
Ian Zernyf271e8d2023-09-11 12:30:41 +0200190 '--testing-state-dir',
191 help='Explicitly set the testing state directory '
192 '(defaults to build/test-state/<git-branch>).')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200193 result.add_argument(
Ian Zernyf271e8d2023-09-11 12:30:41 +0200194 '--rerun',
195 help='Rerun tests (implicitly enables testing state).',
196 choices=testing_state.CHOICES)
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200197 result.add_argument(
Ian Zerny9a0e96a2021-04-28 12:35:49 +0200198 '--stacktrace',
199 help='Pass --stacktrace to the gradle run',
200 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200201 result.add_argument('--kotlin-compiler-dev',
Morten Krogh-Jespersenc69a5542021-09-23 08:10:23 +0200202 help='Specify to download a kotlin dev compiler and run '
203 'tests with that',
204 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200205 result.add_argument('--kotlin-compiler-old',
Morten Krogh-Jespersen69ed1d82021-11-26 13:30:11 +0100206 help='Specify to run tests on older kotlin compilers',
207 default=False, action='store_true')
Christoffer Quist Adamsen4684a0b2023-09-22 10:21:39 +0200208 return result.parse_known_args()
Mads Ager418d1ca2017-05-22 09:35:49 +0200209
Rico Wind4cd3cbc2022-04-26 11:25:15 +0200210def has_failures(classes_file):
211 with open(classes_file) as f:
212 contents = f.read()
213 # The report has a div tag with the percentage of tests that succeeded.
214 assert '<div class="percent">' in contents
215 return '<div class="percent">100%</div>' not in contents
216
217def should_upload(filename, absolute_filename):
218 # filename is relative to REPO_ROOT/build/reports/tests
219 if filename.startswith('test/packages'):
220 # We don't upload the package overview
221 return False
222 if filename.startswith('test/classes'):
223 return has_failures(absolute_filename)
224 # Always upload index, css and js
225 return True
226
Morten Krogh-Jespersenef978ff2021-10-11 12:44:00 +0200227def archive_failures(options):
Rico Wind06487ac2018-12-10 09:09:19 +0100228 upload_dir = os.path.join(utils.REPO_ROOT, 'build', 'reports', 'tests')
Morten Krogh-Jespersenef978ff2021-10-11 12:44:00 +0200229 file_name = options.archive_failures_file_name
Rico Wind4cd3cbc2022-04-26 11:25:15 +0200230 destination_dir = 'gs://%s/%s/' % (BUCKET, file_name)
231 for (dir_path, dir_names, file_names) in os.walk(upload_dir):
232 for f in file_names:
233 absolute_file = os.path.join(dir_path, f)
234 relative_file = absolute_file[len(upload_dir)+1:]
235 if (should_upload(relative_file, absolute_file)):
236 utils.upload_file_to_cloud_storage(absolute_file,
Rico Wind03424282022-04-26 15:09:51 +0200237 destination_dir + relative_file)
Morten Krogh-Jespersenef978ff2021-10-11 12:44:00 +0200238 url = 'https://storage.googleapis.com/%s/%s/test/index.html' % (BUCKET, file_name)
239 print('Test results available at: %s' % url)
Rico Wind06487ac2018-12-10 09:09:19 +0100240
Mads Ager418d1ca2017-05-22 09:35:49 +0200241def Main():
242 (options, args) = ParseOptions()
Rico Windda6836e2018-12-07 12:32:03 +0100243 if utils.is_bot():
Rico Windcebdd432023-10-04 15:07:25 +0200244 gradle.RunGradle(['--no-daemon', 'clean'], new_gradle=True)
Rico Wind8753a852022-02-22 09:36:58 +0100245 print('Running with python ' + str(sys.version_info))
Rico Wind8717f792023-09-29 13:16:27 +0200246 # Always print stats on bots if command cache is enabled
247 options.command_cache_stats = options.command_cache_dir is not None
Sebastien Hertze2687b62017-07-25 11:16:04 +0200248
Søren Gjesseef195772021-03-11 16:04:42 +0100249 desugar_jdk_json_dir = None
250 if options.desugared_library_configuration:
251 if options.desugared_library_configuration != 'jdk11':
252 print("Only value supported for --desugared-library is 'jdk11'")
253 exit(1)
254 desugar_jdk_json_dir = 'src/library_desugar/jdk11'
255
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100256 desugar_jdk_libs = None
257 if options.desugared_library:
258 if options.desugared_library != 'HEAD':
259 print("Only value supported for --desugared-library is 'HEAD'")
260 exit(1)
261 desugar_jdk_libs_dir = 'build/desugar_jdk_libs'
262 shutil.rmtree(desugar_jdk_libs_dir, ignore_errors=True)
263 os.makedirs(desugar_jdk_libs_dir)
264 print('Building desugared library.')
265 with utils.TempDir() as checkout_dir:
Søren Gjessedfe6a9e2022-09-02 12:44:32 +0200266 archive_desugar_jdk_libs.CloneDesugaredLibrary('google', checkout_dir, 'HEAD')
Rico Winde4b43a32021-03-02 09:29:52 +0100267 # Make sure bazel is extracted in third_party.
268 utils.DownloadFromGoogleCloudStorage(utils.BAZEL_SHA_FILE)
269 utils.DownloadFromGoogleCloudStorage(utils.JAVA8_SHA_FILE)
Søren Gjesseef195772021-03-11 16:04:42 +0100270 utils.DownloadFromGoogleCloudStorage(utils.JAVA11_SHA_FILE)
Søren Gjesse2b047692022-08-19 16:34:38 +0200271 (library_jar, maven_zip) = archive_desugar_jdk_libs.BuildDesugaredLibrary(checkout_dir, 'jdk11_legacy' if options.desugared_library_configuration == 'jdk11' else 'jdk8')
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100272 desugar_jdk_libs = os.path.join(desugar_jdk_libs_dir, os.path.basename(library_jar))
273 shutil.copyfile(library_jar, desugar_jdk_libs)
274 print('Desugared library for test in ' + desugar_jdk_libs)
275
Ian Zerny9a0e96a2021-04-28 12:35:49 +0200276 gradle_args = []
277
278 if options.stacktrace or utils.is_bot():
279 gradle_args.append('--stacktrace')
280
Rico Wind22707fc2019-03-15 13:19:57 +0100281 if utils.is_bot():
Rico Wind24777b92020-06-19 22:44:14 +0200282 # Bots don't like dangling processes.
Rico Wind22707fc2019-03-15 13:19:57 +0100283 gradle_args.append('--no-daemon')
284
Sebastien Hertze2687b62017-07-25 11:16:04 +0200285 # Set all necessary Gradle properties and options first.
Rico Wind8e2f7e42019-02-21 10:13:21 +0100286 if options.shard_count:
287 assert options.shard_number
288 gradle_args.append('-Pshard_count=%s' % options.shard_count)
289 gradle_args.append('-Pshard_number=%s' % options.shard_number)
Mads Ager418d1ca2017-05-22 09:35:49 +0200290 if options.verbose:
291 gradle_args.append('-Pprint_test_stdout')
292 if options.no_internal:
293 gradle_args.append('-Pno_internal')
294 if options.only_internal:
295 gradle_args.append('-Ponly_internal')
296 if options.all_tests:
297 gradle_args.append('-Pall_tests')
Christoffer Quist Adamsen748e4662019-08-23 14:53:49 +0200298 if options.slow_tests:
299 gradle_args.append('-Pslow_tests=1')
Mads Ager418d1ca2017-05-22 09:35:49 +0200300 if options.tool:
301 gradle_args.append('-Ptool=%s' % options.tool)
Rico Windcebdd432023-10-04 15:07:25 +0200302 if options.one_line_per_test:
Mads Ager418d1ca2017-05-22 09:35:49 +0200303 gradle_args.append('-Pone_line_per_test')
Rico Windba151112020-10-01 08:16:33 +0200304 if options.test_namespace:
305 gradle_args.append('-Ptest_namespace=%s' % options.test_namespace)
Søren Gjesseaf1c5e22017-06-15 12:24:03 +0200306 if options.disable_assertions:
307 gradle_args.append('-Pdisable_assertions')
Sebastien Hertze2687b62017-07-25 11:16:04 +0200308 if options.with_code_coverage:
309 gradle_args.append('-Pwith_code_coverage')
Christoffer Quist Adamsen7bf60342020-11-09 14:00:27 +0100310 if options.print_full_stacktraces:
311 gradle_args.append('-Pprint_full_stacktraces')
312 if options.print_obfuscated_stacktraces:
313 gradle_args.append('-Pprint_obfuscated_stacktraces')
Morten Krogh-Jespersen69ed1d82021-11-26 13:30:11 +0100314 if options.kotlin_compiler_old:
Morten Krogh-Jespersendcb967e2021-12-02 11:18:39 +0100315 gradle_args.append('-Pkotlin_compiler_old')
Morten Krogh-Jespersen3e0054b2021-09-23 08:32:33 +0200316 if options.kotlin_compiler_dev:
Morten Krogh-Jespersendcb967e2021-12-02 11:18:39 +0100317 gradle_args.append('-Pkotlin_compiler_dev')
Morten Krogh-Jespersenc69a5542021-09-23 08:10:23 +0200318 download_kotlin_dev.download_newest()
Jean-Marie Henaff7b424e92017-06-15 11:02:56 +0200319 if os.name == 'nt':
Jean-Marie Henaff7b424e92017-06-15 11:02:56 +0200320 gradle_args.append('-Pno_internal')
Tamas Kenezb77b7d82017-08-17 14:05:16 +0200321 if options.test_dir:
322 gradle_args.append('-Ptest_dir=' + options.test_dir)
323 if not os.path.exists(options.test_dir):
324 os.makedirs(options.test_dir)
Rico Windfc645a22023-06-14 12:37:18 +0200325 if options.command_cache_dir:
326 gradle_args.append('-Pcommand_cache_dir=' + options.command_cache_dir)
327 if not os.path.exists(options.command_cache_dir):
328 os.makedirs(options.command_cache_dir)
Rico Windb28b9f72023-09-29 13:12:34 +0200329 if options.command_cache_stats:
330 stats_dir = os.path.join(options.command_cache_dir, 'stats')
331 gradle_args.append('-Pcommand_cache_stats_dir=' + stats_dir)
332 if not os.path.exists(stats_dir):
333 os.makedirs(stats_dir)
334 # Clean out old stats files
335 for (_, _, file_names) in os.walk(stats_dir):
336 for f in file_names:
337 os.remove(os.path.join(stats_dir, f))
Mikaël Peltier5c0a3232017-10-18 09:14:40 +0200338 if options.java_home:
339 gradle_args.append('-Dorg.gradle.java.home=' + options.java_home)
Ian Zerny5fffb0a2019-02-11 13:54:22 +0100340 if options.java_max_memory_size:
Rico Wind97b0a992019-08-30 11:09:15 +0200341 gradle_args.append('-Ptest_xmx=' + options.java_max_memory_size)
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200342 if options.generate_golden_files_to:
343 gradle_args.append('-Pgenerate_golden_files_to=' + options.generate_golden_files_to)
344 if not os.path.exists(options.generate_golden_files_to):
345 os.makedirs(options.generate_golden_files_to)
346 gradle_args.append('-PHEAD_sha1=' + utils.get_HEAD_sha1())
347 if options.use_golden_files_in:
348 gradle_args.append('-Puse_golden_files_in=' + options.use_golden_files_in)
349 if not os.path.exists(options.use_golden_files_in):
350 os.makedirs(options.use_golden_files_in)
351 gradle_args.append('-PHEAD_sha1=' + utils.get_HEAD_sha1())
Morten Krogh-Jespersen432dd912019-01-14 13:26:35 +0100352 if (not options.no_r8lib) and options.r8lib_no_deps:
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100353 print('Cannot run tests on r8lib with and without deps. R8lib is now default target.')
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100354 exit(1)
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100355 if not options.no_r8lib:
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100356 gradle_args.append('-Pr8lib')
357 if options.r8lib_no_deps:
358 gradle_args.append('-Pr8lib_no_deps')
Morten Krogh-Jespersen89d005b2019-10-15 13:32:23 +0200359 if options.worktree:
360 gradle_args.append('-g=' + os.path.join(utils.REPO_ROOT, ".gradle_user_home"))
Ian Zernya97ccc42020-02-10 14:18:07 +0100361 gradle_args.append('--no-daemon')
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100362 if options.debug_agent:
363 gradle_args.append('--no-daemon')
Søren Gjesseef195772021-03-11 16:04:42 +0100364 if desugar_jdk_json_dir:
365 gradle_args.append('-Pdesugar_jdk_json_dir=' + desugar_jdk_json_dir)
Søren Gjesse4a45f9b2021-02-11 14:05:29 +0100366 if desugar_jdk_libs:
367 gradle_args.append('-Pdesugar_jdk_libs=' + desugar_jdk_libs)
Rico Windbc820812022-05-31 09:19:56 +0200368 if options.no_arttests:
369 gradle_args.append('-Pno_arttests=true')
Ian Zernyc2de7b72023-09-06 20:52:16 +0200370
Rico Windcebdd432023-10-04 15:07:25 +0200371 if options.rerun:
Ian Zernyf271e8d2023-09-11 12:30:41 +0200372 testing_state.set_up_test_state(gradle_args, options.rerun, options.testing_state_dir)
Morten Krogh-Jespersen807b15f2018-12-17 14:24:22 +0100373
Christoffer Quist Adamsenfe892312023-02-16 20:58:41 +0100374 # Enable completeness testing of ART profile rewriting.
375 gradle_args.append('-Part_profile_rewriting_completeness_check=true')
376
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100377 # Build an R8 with dependencies for bootstrapping tests before adding test sources.
Rico Windcebdd432023-10-04 15:07:25 +0200378 gradle_args.append(':main:r8WithRelocatedDeps')
379 gradle_args.append(':test:cleanTest')
380 gradle_args.append('test:test')
381 gradle_args.append('--stacktrace')
382 gradle_args.append('-Pprint_full_stacktraces')
Morten Krogh-Jespersen54f196e2019-01-14 16:10:08 +0100383
Morten Krogh-Jespersen2d3aca62020-11-30 12:48:11 +0100384 if options.debug_agent:
385 gradle_args.append('--debug-jvm')
Ian Zerny24398bc2019-02-22 11:59:18 +0100386 if options.fail_fast:
387 gradle_args.append('--fail-fast')
388 if options.failed:
389 args = compute_failed_tests(args)
390 if args is None:
391 return 1
392 if len(args) == 0:
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100393 print("No failing tests")
Ian Zerny24398bc2019-02-22 11:59:18 +0100394 return 0
Sebastien Hertz0f4e7fb2017-10-02 11:33:45 +0200395 # Test filtering. Must always follow the 'test' task.
Morten Krogh-Jespersen95734d42023-09-13 13:09:58 +0200396 testFilterProperty = []
Sebastien Hertz0f4e7fb2017-10-02 11:33:45 +0200397 for testFilter in args:
Sebastien Hertze2687b62017-07-25 11:16:04 +0200398 gradle_args.append('--tests')
Sebastien Hertz0f4e7fb2017-10-02 11:33:45 +0200399 gradle_args.append(testFilter)
Morten Krogh-Jespersen95734d42023-09-13 13:09:58 +0200400 testFilterProperty.append(testFilter)
401 assert not ("|" in testFilter), "| is used as separating character"
402 if len(testFilterProperty) > 0:
403 gradle_args.append("-Ptestfilter=" + "|".join(testFilterProperty))
Sebastien Hertze2687b62017-07-25 11:16:04 +0200404 if options.with_code_coverage:
405 # Create Jacoco report after tests.
406 gradle_args.append('jacocoTestReport')
407
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200408 if options.use_golden_files_in:
409 sha1 = '%s' % utils.get_HEAD_sha1()
410 with utils.ChangedWorkingDirectory(options.use_golden_files_in):
411 utils.download_file_from_cloud_storage(
412 'gs://r8-test-results/golden-files/%s.tar.gz' % sha1,
413 '%s.tar.gz' % sha1)
414 utils.unpack_archive('%s.tar.gz' % sha1)
415
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200416 print_stacks_timeout = options.print_hanging_stacks
417 if (utils.is_bot() and not utils.IsWindows()) or print_stacks_timeout > -1:
Rico Windda6836e2018-12-07 12:32:03 +0100418 timestamp_file = os.path.join(utils.BUILD, 'last_test_time')
419 if os.path.exists(timestamp_file):
420 os.remove(timestamp_file)
421 gradle_args.append('-Pupdate_test_timestamp=' + timestamp_file)
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200422 print_stacks_timeout = (print_stacks_timeout
423 if print_stacks_timeout != -1
424 else TIMEOUT_HANDLER_PERIOD)
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100425 if utils.is_python3():
426 threading.Thread(
427 target=timeout_handler,
Christoffer Quist Adamsendb5b6aa2022-02-16 13:07:47 +0100428 args=(timestamp_file, print_stacks_timeout),
429 daemon=True).start()
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100430 else:
431 thread.start_new_thread(
432 timeout_handler, (timestamp_file, print_stacks_timeout,))
Ian Zerny24398bc2019-02-22 11:59:18 +0100433 rotate_test_reports()
434
Sebastien Hertze2687b62017-07-25 11:16:04 +0200435 # Now run tests on selected runtime(s).
Ian Zernybcbd6c52019-10-29 15:27:04 +0100436 if options.runtimes:
437 if options.dex_vm != 'default':
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100438 print('Unexpected runtimes and dex_vm argument: ' + options.dex_vm)
Ian Zernybcbd6c52019-10-29 15:27:04 +0100439 sys.exit(1)
440 if options.runtimes == 'empty':
441 # Set runtimes with no content will configure no runtimes.
442 gradle_args.append('-Pruntimes=')
443 elif options.runtimes == 'all':
444 # An unset runtimes will configure all runtimes
445 pass
446 else:
447 prefixes = [prefix.strip() for prefix in options.runtimes.split(':')]
448 runtimes = []
449 for prefix in prefixes:
450 matches = [ rt for rt in VALID_RUNTIMES if rt.startswith(prefix) ]
451 if len(matches) == 0:
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100452 print("Invalid runtime prefix '%s'." % prefix)
453 print("Must be just 'all', 'empty'," \
454 " or a prefix of %s" % ', '.join(VALID_RUNTIMES))
Ian Zernybcbd6c52019-10-29 15:27:04 +0100455 sys.exit(1)
456 runtimes.extend(matches)
457 gradle_args.append('-Pruntimes=%s' % ':'.join(runtimes))
458
Morten Krogh-Jespersen774701f2023-08-09 08:22:09 +0200459 return_code = gradle.RunGradle(
Rico Windcebdd432023-10-04 15:07:25 +0200460 gradle_args, throw_on_failure=False, new_gradle=True)
Ian Zernybcbd6c52019-10-29 15:27:04 +0100461 return archive_and_return(return_code, options)
462
463 # Legacy testing populates the runtimes based on dex_vm.
Mads Ager418d1ca2017-05-22 09:35:49 +0200464 vms_to_test = [options.dex_vm] if options.dex_vm != "all" else ALL_ART_VMS
Rico Wind124b2712019-02-28 13:49:25 +0100465
Rico Windf2f4c292021-04-23 07:06:13 +0200466 if options.print_times:
467 gradle_args.append('-Pprint_times=true')
Mads Ager418d1ca2017-05-22 09:35:49 +0200468 for art_vm in vms_to_test:
Ian Zerny4dfd5a52019-03-12 07:56:11 +0100469 vm_suffix = "_" + options.dex_vm_kind if art_vm != "default" else ""
Ian Zerny324d7612019-03-20 10:52:28 +0100470 runtimes = ['dex-' + art_vm]
Ian Zerny019e7782021-04-20 15:34:01 +0200471 # Append the "none" runtime and default JVM if running the "default" DEX VM.
Ian Zerny324d7612019-03-20 10:52:28 +0100472 if art_vm == "default":
Morten Krogh-Jespersen69358942020-10-09 11:07:09 +0200473 runtimes.extend(['jdk11', 'none'])
Ian Zerny4dfd5a52019-03-12 07:56:11 +0100474 return_code = gradle.RunGradle(
475 gradle_args + [
476 '-Pdex_vm=%s' % art_vm + vm_suffix,
Ian Zerny324d7612019-03-20 10:52:28 +0100477 '-Pruntimes=%s' % ':'.join(runtimes),
478 ],
Morten Krogh-Jespersen774701f2023-08-09 08:22:09 +0200479 throw_on_failure=False,
Rico Windcebdd432023-10-04 15:07:25 +0200480 new_gradle=True)
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200481 if options.generate_golden_files_to:
482 sha1 = '%s' % utils.get_HEAD_sha1()
483 with utils.ChangedWorkingDirectory(options.generate_golden_files_to):
484 archive = utils.create_archive(sha1)
485 utils.upload_file_to_cloud_storage(archive,
486 'gs://r8-test-results/golden-files/' + archive)
487
Rico Windb28b9f72023-09-29 13:12:34 +0200488 return archive_and_return(return_code, options)
Mads Ager418d1ca2017-05-22 09:35:49 +0200489
Jinseong Jeon9749d172017-09-19 00:25:01 -0700490 return 0
491
Ian Zerny17561362019-05-27 15:16:26 +0200492def archive_and_return(return_code, options):
Rico Windc58a20e2019-05-23 09:43:19 +0200493 if return_code != 0:
Rico Windef7420c2021-10-14 13:16:01 +0200494 if options.archive_failures:
Morten Krogh-Jespersenef978ff2021-10-11 12:44:00 +0200495 archive_failures(options)
Rico Windb28b9f72023-09-29 13:12:34 +0200496 if options.command_cache_stats:
497 stats_dir = os.path.join(options.command_cache_dir, 'stats')
498 cache_hit = 0
499 cache_miss = 0
500 cache_put = 0
501 for (_, _, file_names) in os.walk(stats_dir):
502 for f in file_names:
503 if f.endswith('CACHEHIT'):
504 cache_hit += os.stat(os.path.join(stats_dir, f)).st_size
505 if f.endswith('CACHEMISS'):
506 cache_miss += os.stat(os.path.join(stats_dir, f)).st_size
507 if f.endswith('CACHEPUT'):
508 cache_put += os.stat(os.path.join(stats_dir, f)).st_size
509 print('Command cache stats')
510 print(' Cache hits: ' + str(cache_hit))
511 print(' Cache miss: ' + str(cache_miss))
512 print(' Cache puts: ' + str(cache_put))
Rico Windc58a20e2019-05-23 09:43:19 +0200513 return return_code
514
Rico Windda6836e2018-12-07 12:32:03 +0100515def print_jstacks():
Rico Wind5a3dc122022-03-29 08:56:00 +0200516 processes = subprocess.check_output(['ps', 'aux']).decode('utf-8')
Rico Windda6836e2018-12-07 12:32:03 +0100517 for l in processes.splitlines():
Rico Wind274bb8a2021-11-03 12:39:27 +0100518 if 'art' in l or 'dalvik' in l:
519 print('Running art of dalvik process: \n%s' % l)
Rico Windda6836e2018-12-07 12:32:03 +0100520 if 'java' in l and 'openjdk' in l:
Rico Wind274bb8a2021-11-03 12:39:27 +0100521 print('Running jstack on process: \n%s' % l)
Rico Windda6836e2018-12-07 12:32:03 +0100522 # Example line:
523 # ricow 184313 2.6 0.0 36839068 31808 ? Sl 09:53 0:00 /us..
524 columns = l.split()
525 pid = columns[1]
526 return_value = subprocess.call(['jstack', pid])
527 if return_value:
528 print('Could not jstack %s' % l)
Rico Windda6836e2018-12-07 12:32:03 +0100529
530def get_time_from_file(timestamp_file):
531 if os.path.exists(timestamp_file):
532 timestamp = os.stat(timestamp_file).st_mtime
533 print('TIMEOUT HANDLER timestamp: %s' % (timestamp))
Rico Windda6836e2018-12-07 12:32:03 +0100534 sys.stdout.flush()
535 return timestamp
536 else:
537 print('TIMEOUT HANDLER no timestamp file yet')
Rico Windda6836e2018-12-07 12:32:03 +0100538 sys.stdout.flush()
539 return None
540
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200541def timeout_handler(timestamp_file, timeout_handler_period):
Rico Windda6836e2018-12-07 12:32:03 +0100542 last_timestamp = None
543 while True:
Morten Krogh-Jespersendcb8d452020-07-09 15:07:50 +0200544 time.sleep(timeout_handler_period)
Rico Windda6836e2018-12-07 12:32:03 +0100545 new_timestamp = get_time_from_file(timestamp_file)
546 if last_timestamp and new_timestamp == last_timestamp:
547 print_jstacks()
548 last_timestamp = new_timestamp
549
Ian Zerny24398bc2019-02-22 11:59:18 +0100550def report_dir_path(index):
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100551 if index == 0:
Ian Zerny24398bc2019-02-22 11:59:18 +0100552 return REPORTS_PATH
553 return '%s%d' % (REPORTS_PATH, index)
554
555def report_index_path(index):
556 return os.path.join(report_dir_path(index), *REPORT_INDEX)
557
558# Rotate test results so previous results are still accessible.
559def rotate_test_reports():
560 if not os.path.exists(report_dir_path(0)):
561 return
562 i = 1
563 while i < NUMBER_OF_TEST_REPORTS and os.path.exists(report_dir_path(i)):
564 i += 1
565 if i == NUMBER_OF_TEST_REPORTS and os.path.exists(report_dir_path(i)):
566 shutil.rmtree(report_dir_path(i))
567 while i > 0:
568 shutil.move(report_dir_path(i - 1), report_dir_path(i))
569 i -= 1
570
571def compute_failed_tests(args):
572 if len(args) > 1:
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100573 print("Running with --failed can take an optional path to a report index (or report number).")
Ian Zerny24398bc2019-02-22 11:59:18 +0100574 return None
575 report = report_index_path(0)
576 # If the default report does not exist, fall back to the previous report as it may be a failed
577 # gradle run which has already moved the report to report1, but did not produce a new report.
578 if not os.path.exists(report):
579 report1 = report_index_path(1)
580 if os.path.exists(report1):
581 report = report1
582 if len(args) == 1:
583 try:
584 # try to parse the arg as a report index.
585 index = int(args[0])
586 report = report_index_path(index)
587 except ValueError:
588 # if integer parsing failed assume it is a report file path.
589 report = args[0]
590 if not os.path.exists(report):
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100591 print("Can't re-run failing, no report at:", report)
Ian Zerny24398bc2019-02-22 11:59:18 +0100592 return None
Christoffer Quist Adamsen6962b1f2022-02-16 12:01:07 +0100593 print("Reading failed tests in", report)
Ian Zerny24398bc2019-02-22 11:59:18 +0100594 failing = set()
595 inFailedSection = False
Rico Wind34e63c82022-04-21 14:40:38 +0200596 for line in open(report):
Ian Zerny24398bc2019-02-22 11:59:18 +0100597 l = line.strip()
598 if l == "<h2>Failed tests</h2>":
599 inFailedSection = True
600 elif l.startswith("<h2>"):
601 inFailedSection = False
602 prefix = '<a href="classes/'
603 if inFailedSection and l.startswith(prefix):
604 href = l[len(prefix):l.index('">')]
605 # Ignore enties ending with .html which are test classes, not test methods.
606 if not href.endswith('.html'):
607 # Remove the .html and anchor separateor, also, a classMethod test is the static
608 # setup failing so rerun the full class of tests.
609 test = href.replace('.html','').replace('#', '.').replace('.classMethod', '')
610 failing.add(test)
611 return list(failing)
Mads Ager418d1ca2017-05-22 09:35:49 +0200612if __name__ == '__main__':
Stephan Herhutd24b1b72017-08-24 15:09:36 +0200613 return_code = Main()
614 if return_code != 0:
615 notify.notify("Tests failed.")
616 else:
617 notify.notify("Tests passed.")
618 sys.exit(return_code)