- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathrun-test
executable file
·289 lines (240 loc) · 10.7 KB
/
run-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
# utils/run-test - test runner for Swift -*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
importmultiprocessing
importos
importshutil
importsys
frombuild_swift.build_swiftimportargparse
frombuild_swift.build_swift.constantsimportSWIFT_SOURCE_ROOT
fromswift_build_support.swift_build_supportimportshell
fromswift_build_support.swift_build_support.targetsimportStdlibDeploymentTarget
TEST_MODES= [
'optimize_none',
'optimize',
'optimize_unchecked',
'only_executable',
'only_non_executable',
]
TEST_SUBSETS= [
'primary',
'validation',
'all',
'only_validation',
'only_long',
'only_stress',
]
SWIFT_SOURCE_DIR=os.path.join(SWIFT_SOURCE_ROOT, 'swift')
TEST_SOURCE_DIR=os.path.join(SWIFT_SOURCE_DIR, 'test')
VALIDATION_TEST_SOURCE_DIR=os.path.join(SWIFT_SOURCE_DIR, 'validation-test')
def_get_default_llvm_source_dir():
legacy_llvm_dir_path=os.path.join(SWIFT_SOURCE_ROOT, 'llvm')
ifos.path.isdir(legacy_llvm_dir_path):
returnlegacy_llvm_dir_path
returnos.path.join(SWIFT_SOURCE_ROOT, 'llvm-project', 'llvm')
# Default path for "lit.py" executable.
LIT_BIN_DEFAULT=os.path.join(os.environ.get("LLVM_SOURCE_DIR",
_get_default_llvm_source_dir()),
'utils', 'lit', 'lit.py')
host_target=StdlibDeploymentTarget.host_target().name
deferror_exit(msg):
print("%s: %s"% (os.path.basename(sys.argv[0]), msg), file=sys.stderr)
sys.exit(1)
# Return true if the path looks like swift build directory.
defis_swift_build_dir(path, unified_build_dir):
ifnotunified_build_dir:
tests_path= [path, "test-%s"%host_target]
else:
tests_path= [path, "tools", "swift", "test-%s"%host_target]
return (os.path.exists(os.path.join(path, "CMakeCache.txt")) and
os.path.isdir(os.path.join(*tests_path)))
# Return true if the swift build directory is configured with `Xcode`
# generator.
defis_build_dir_xcode(path):
returnos.path.exists(os.path.join(path, 'Swift.xcodeproj'))
# Return true if 'path' is sub path of 'd'
defis_subpath(path, d):
path, d=os.path.abspath(path), os.path.abspath(d)
ifos.path.isdir(path):
path=os.path.join(path, '')
d=os.path.join(d, '')
returnpath.startswith(d)
# Convert test path in source directory to corresponding path in build
# directory. If the path is not sub path of test directories in source,
# return the path as is.
defnormalize_test_path(path, build_dir, variant, unified_build_dir):
ifnotunified_build_dir:
tests_path= [build_dir]
else:
tests_path= [build_dir, "tools", "swift"]
ford, prefixin [(TEST_SOURCE_DIR, 'test-%s'),
(VALIDATION_TEST_SOURCE_DIR, 'validation-test-%s')]:
ifis_subpath(path, d):
returnos.path.normpath(os.path.join(*(
tests_path+
[prefix%variant,
os.path.relpath(path, d)])))
returnpath
defmain():
parser=argparse.ArgumentParser()
parser.add_argument("paths", type=os.path.realpath,
nargs="*", metavar="PATH",
help="paths to test. Accept multiple. "
"If --build-dir is not specified, these paths "
"must be test paths in the Swift build "
"directory. (default: primary test suite if "
"--build-dir is specified, none otherwise)")
parser.add_argument("-v", "--verbose", action="store_true",
help="run test with verbose output")
parser.add_argument("--build-dir", type=os.path.realpath, metavar="PATH",
help="Swift build directory")
parser.add_argument("--build",
choices=["true", "verbose", "skip"], default='true',
help="build test dependencies before running tests "
"(default: true)")
parser.add_argument("--build-jobs",
type=int,
help="The number of parallel build jobs to use")
parser.add_argument("--color", choices=["true", "false"], default="true",
help="Whether to enable color output (default: true)")
parser.add_argument("--target",
type=argparse.types.ShellSplitType(),
action=argparse.actions.AppendAction,
dest="targets",
help="stdlib deployment targets to test. Accept "
"multiple (default: "+host_target+")")
parser.add_argument("--filter", type=str, metavar="REGEX",
help="only run tests with paths matching the given "
"regular expression")
parser.add_argument("--mode",
choices=TEST_MODES, default='optimize_none',
help="test mode (default: optimize_none)")
parser.add_argument("--subset",
choices=TEST_SUBSETS, default='primary',
help="test subset (default: primary)")
parser.add_argument("--param",
type=argparse.types.ShellSplitType(),
action=argparse.actions.AppendAction,
default=[],
help="key=value parameters they are directly passed "
"to lit command in addition to `mode` and "
"`subset`. Accept multiple.")
parser.add_argument("--result-dir", type=os.path.realpath, metavar="PATH",
help="directory to store test results (default: none)")
parser.add_argument("--lit", default=LIT_BIN_DEFAULT, metavar="PATH",
help="lit.py executable path "
"(default: ${LLVM_SOURCE_DIR}/utils/lit/lit.py)")
parser.add_argument("--unified", action="store_true",
help="The build directory is an unified LLVM build, "
"not a standalone Swift build")
args=parser.parse_args()
targets=args.targets
ifnottargets:
targets= [host_target]
paths= []
build_dir=args.build_dir
ifbuild_dirisnotNone:
# Fixup build directory.
# build_dir can be:
# build-root/ # assuming we are to test host deployment target.
# build-root/swift-{tool-deployment_target}/
fordin [
build_dir,
os.path.join(build_dir, 'swift-%s'%host_target)]:
ifis_swift_build_dir(d, args.unified):
build_dir=d
break
else:
error_exit("'%s' is not a swift build directory"%args.build_dir)
# If no path given, run primary test suite.
ifnotargs.paths:
args.paths= [TEST_SOURCE_DIR]
# $ run-test --build-dir=<swift-build-dir> <test-dir-in-source> ... \
# --target macosx-x86_64 --target iphonesimulator-i386
fortargetintargets:
paths+= [normalize_test_path(p, build_dir, target, args.unified)
forpinargs.paths]
else:
# Otherwise, we assume all given paths are valid test paths in the
# build_dir.
paths=args.paths
ifnotpaths:
parser.print_usage()
error_exit("error: too few arguments")
ifargs.build!='skip':
# Building dependencies requires `build_dir` set.
# Traverse the first test path to find the `build_dir`
d=os.path.dirname(paths[0])
whilednotin ['', os.sep]:
ifis_swift_build_dir(d, args.unified):
build_dir=d
break
d=os.path.dirname(d)
else:
error_exit("Can't infer swift build directory")
# Ensure we have up to date test dependency
ifargs.build!='skip'andis_build_dir_xcode(build_dir):
# We don't support Xcode Generator build yet.
print("warning: Building Xcode project is not supported yet. "
"Skipping...")
sys.stdout.flush()
elifargs.build!='skip':
dependency_targets= ["all", "SwiftUnitTests"]
upload_stdlib_targets= []
need_validation=any('/validation-test-'inpathforpathinpaths)
fortargetintargets:
ifargs.mode!='only_non_executable':
upload_stdlib_targets+= ["upload-stdlib-%s"%target]
ifneed_validation:
dependency_targets+= ["swift-stdlib-%s"%target]
else:
dependency_targets+= ["swift-test-stdlib-%s"%target]
cmake_build= ['cmake', '--build', build_dir, '--']
ifargs.build=='verbose':
cmake_build+= ['-v']
ifargs.build_jobsisnotNone:
cmake_build+= ['-j%d'%args.build_jobs]
else:
cmake_build+= ['-j%d'%multiprocessing.cpu_count()]
print("--- Building test dependencies %s ---"%
', '.join(dependency_targets))
sys.stdout.flush()
shell.call(cmake_build+dependency_targets)
shell.call(cmake_build+upload_stdlib_targets)
print("--- Build finished ---")
sys.stdout.flush()
ifargs.result_dirisnotNone:
# Clear result directory
ifos.path.exists(args.result_dir):
shutil.rmtree(args.result_dir)
os.makedirs(args.result_dir)
ifargs.verbose:
test_args= ["-a"]
else:
test_args= ["-sv"]
# Test parameters.
test_args+= ['--param', 'swift_test_mode=%s'%args.mode,
'--param', 'swift_test_subset=%s'%args.subset]
forparaminargs.param:
test_args+= ['--param', param]
# Enable colors if we're on a tty.
ifargs.color=='true'andsys.stdout.isatty():
test_args+= ['--param', 'color_output']
ifargs.result_dir:
test_args+= ['--param', 'swift_test_results_dir=%s'%args.result_dir,
'--xunit-xml-output=%s'%os.path.join(args.result_dir,
'lit-tests.xml')]
ifargs.filter:
test_args+= ['--filter', args.filter]
test_cmd= [sys.executable, args.lit] +test_args+paths
# Do execute test
shell.call(test_cmd)
if__name__=="__main__":
main()