forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCommandTests.swift
342 lines (307 loc) · 17.2 KB
/
TestCommandTests.swift
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import Commands
import PackageModel
import _InternalTestSupport
import XCTest
final class TestCommandTests: CommandsTestCase {
private func execute(_ args: [String], packagePath: AbsolutePath? = nil) async throws -> (stdout: String, stderr: String) {
try await SwiftPM.Test.execute(args, packagePath: packagePath)
}
func testUsage() async throws {
let stdout = try await execute(["-help"]).stdout
XCTAssert(stdout.contains("USAGE: swift test"), "got stdout:\n" + stdout)
}
func testSeeAlso() async throws {
let stdout = try await execute(["--help"]).stdout
XCTAssert(stdout.contains("SEE ALSO: swift build, swift run, swift package"), "got stdout:\n" + stdout)
}
func testVersion() async throws {
let stdout = try await execute(["--version"]).stdout
XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout)
}
func testNumWorkersParallelRequirement() async throws {
#if !os(macOS)
// Running swift-test fixtures on linux is not yet possible.
try XCTSkipIf(true, "test is only supported on macOS")
#endif
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
await XCTAssertThrowsCommandExecutionError(try await execute(["--num-workers", "1"])) { error in
XCTAssertMatch(error.stderr, .contains("error: --num-workers must be used with --parallel"))
}
}
}
func testNumWorkersValue() async throws {
#if !os(macOS)
// Running swift-test fixtures on linux is not yet possible.
try XCTSkipIf(true, "test is only supported on macOS")
#endif
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
await XCTAssertThrowsCommandExecutionError(try await execute(["--parallel", "--num-workers", "0"])) { error in
XCTAssertMatch(error.stderr, .contains("error: '--num-workers' must be greater than zero"))
}
}
}
func testEnableDisableTestability() async throws {
// default should run with testability
try await fixture(name: "Miscellaneous/TestableExe") { fixturePath in
do {
let result = try await execute(["--vv"], packagePath: fixturePath)
XCTAssertMatch(result.stderr, .contains("-enable-testing"))
}
}
// disabled
try await fixture(name: "Miscellaneous/TestableExe") { fixturePath in
await XCTAssertThrowsCommandExecutionError(try await execute(["--disable-testable-imports", "--vv"], packagePath: fixturePath)) { error in
XCTAssertMatch(error.stderr, .contains("was not compiled for testing"))
}
}
// enabled
try await fixture(name: "Miscellaneous/TestableExe") { fixturePath in
do {
let result = try await execute(["--enable-testable-imports", "--vv"], packagePath: fixturePath)
XCTAssertMatch(result.stderr, .contains("-enable-testing"))
}
}
}
func testSwiftTestParallel() async throws {
try await fixture(name: "Miscellaneous/ParallelTestsPkg") { fixturePath in
// First try normal serial testing.
await XCTAssertThrowsCommandExecutionError(try await SwiftPM.Test.execute(packagePath: fixturePath)) { error in
// in "swift test" test output goes to stdout
XCTAssertMatch(error.stdout, .contains("Executed 2 tests"))
XCTAssertNoMatch(error.stdout, .contains("[3/3]"))
}
// Try --no-parallel.
await XCTAssertThrowsCommandExecutionError(try await SwiftPM.Test.execute(["--no-parallel"], packagePath: fixturePath)) { error in
// in "swift test" test output goes to stdout
XCTAssertMatch(error.stdout, .contains("Executed 2 tests"))
XCTAssertNoMatch(error.stdout, .contains("[3/3]"))
}
// Run tests in parallel.
await XCTAssertThrowsCommandExecutionError(try await SwiftPM.Test.execute(["--parallel"], packagePath: fixturePath)) { error in
// in "swift test" test output goes to stdout
XCTAssertMatch(error.stdout, .contains("testExample1"))
XCTAssertMatch(error.stdout, .contains("testExample2"))
XCTAssertNoMatch(error.stdout, .contains("'ParallelTestsTests' passed"))
XCTAssertMatch(error.stdout, .contains("'ParallelTestsFailureTests' failed"))
XCTAssertMatch(error.stdout, .contains("[3/3]"))
}
do {
let xUnitOutput = fixturePath.appending("result.xml")
// Run tests in parallel with verbose output.
await XCTAssertThrowsCommandExecutionError(
try await SwiftPM.Test.execute(["--parallel", "--verbose", "--xunit-output", xUnitOutput.pathString], packagePath: fixturePath)
) { error in
// in "swift test" test output goes to stdout
XCTAssertMatch(error.stdout, .contains("testExample1"))
XCTAssertMatch(error.stdout, .contains("testExample2"))
XCTAssertMatch(error.stdout, .contains("'ParallelTestsTests' passed"))
XCTAssertMatch(error.stdout, .contains("'ParallelTestsFailureTests' failed"))
XCTAssertMatch(error.stdout, .contains("[3/3]"))
}
// Check the xUnit output.
XCTAssertFileExists(xUnitOutput)
let contents: String = try localFileSystem.readFileContents(xUnitOutput)
XCTAssertMatch(contents, .contains("tests=\"3\" failures=\"1\""))
XCTAssertMatch(contents, .regex("time=\"[0-9]+\\.[0-9]+\""))
XCTAssertNoMatch(contents, .contains("time=\"0.0\""))
}
}
}
func testSwiftTestXMLOutputWhenEmpty() async throws {
try await fixture(name: "Miscellaneous/EmptyTestsPkg") { fixturePath in
let xUnitOutput = fixturePath.appending("result.xml")
// Run tests in parallel with verbose output.
_ = try await SwiftPM.Test.execute(["--parallel", "--verbose", "--xunit-output", xUnitOutput.pathString], packagePath: fixturePath).stdout
// Check the xUnit output.
XCTAssertFileExists(xUnitOutput)
let contents: String = try localFileSystem.readFileContents(xUnitOutput)
XCTAssertMatch(contents, .contains("tests=\"0\" failures=\"0\""))
}
}
func testSwiftTestFilter() async throws {
try await fixture(name: "Miscellaneous/SkipTests") { fixturePath in
let (stdout, _) = try await SwiftPM.Test.execute(["--filter", ".*1"], packagePath: fixturePath)
// in "swift test" test output goes to stdout
XCTAssertMatch(stdout, .contains("testExample1"))
XCTAssertNoMatch(stdout, .contains("testExample2"))
XCTAssertNoMatch(stdout, .contains("testExample3"))
XCTAssertNoMatch(stdout, .contains("testExample4"))
}
try await fixture(name: "Miscellaneous/SkipTests") { fixturePath in
let (stdout, _) = try await SwiftPM.Test.execute(["--filter", "SomeTests", "--skip", ".*1", "--filter", "testExample3"], packagePath: fixturePath)
// in "swift test" test output goes to stdout
XCTAssertNoMatch(stdout, .contains("testExample1"))
XCTAssertMatch(stdout, .contains("testExample2"))
XCTAssertMatch(stdout, .contains("testExample3"))
XCTAssertNoMatch(stdout, .contains("testExample4"))
}
}
func testSwiftTestSkip() async throws {
try await fixture(name: "Miscellaneous/SkipTests") { fixturePath in
let (stdout, _) = try await SwiftPM.Test.execute(["--skip", "SomeTests"], packagePath: fixturePath)
// in "swift test" test output goes to stdout
XCTAssertNoMatch(stdout, .contains("testExample1"))
XCTAssertNoMatch(stdout, .contains("testExample2"))
XCTAssertMatch(stdout, .contains("testExample3"))
XCTAssertMatch(stdout, .contains("testExample4"))
}
try await fixture(name: "Miscellaneous/SkipTests") { fixturePath in
let (stdout, _) = try await SwiftPM.Test.execute(["--filter", "ExampleTests", "--skip", ".*2", "--filter", "MoreTests", "--skip", "testExample3"], packagePath: fixturePath)
// in "swift test" test output goes to stdout
XCTAssertMatch(stdout, .contains("testExample1"))
XCTAssertNoMatch(stdout, .contains("testExample2"))
XCTAssertNoMatch(stdout, .contains("testExample3"))
XCTAssertMatch(stdout, .contains("testExample4"))
}
try await fixture(name: "Miscellaneous/SkipTests") { fixturePath in
let (stdout, _) = try await SwiftPM.Test.execute(["--skip", "Tests"], packagePath: fixturePath)
// in "swift test" test output goes to stdout
XCTAssertNoMatch(stdout, .contains("testExample1"))
XCTAssertNoMatch(stdout, .contains("testExample2"))
XCTAssertNoMatch(stdout, .contains("testExample3"))
XCTAssertNoMatch(stdout, .contains("testExample4"))
}
}
func testEnableTestDiscoveryDeprecation() async throws {
let compilerDiagnosticFlags = ["-Xswiftc", "-Xfrontend", "-Xswiftc", "-Rmodule-interface-rebuild"]
#if canImport(Darwin)
// should emit when LinuxMain is present
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try await SwiftPM.Test.execute(["--enable-test-discovery"] + compilerDiagnosticFlags, packagePath: fixturePath)
XCTAssertMatch(stderr, .contains("warning: '--enable-test-discovery' option is deprecated"))
}
// should emit when LinuxMain is not present
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
try localFileSystem.writeFileContents(fixturePath.appending(components: "Tests", SwiftModule.defaultTestEntryPointName), bytes: "fatalError(\"boom\")")
let (_, stderr) = try await SwiftPM.Test.execute(["--enable-test-discovery"] + compilerDiagnosticFlags, packagePath: fixturePath)
XCTAssertMatch(stderr, .contains("warning: '--enable-test-discovery' option is deprecated"))
}
#else
// should emit when LinuxMain is present
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try await SwiftPM.Test.execute(["--enable-test-discovery"] + compilerDiagnosticFlags, packagePath: fixturePath)
XCTAssertMatch(stderr, .contains("warning: '--enable-test-discovery' option is deprecated"))
}
// should not emit when LinuxMain is present
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
try localFileSystem.writeFileContents(fixturePath.appending(components: "Tests", SwiftModule.defaultTestEntryPointName), bytes: "fatalError(\"boom\")")
let (_, stderr) = try await SwiftPM.Test.execute(["--enable-test-discovery"] + compilerDiagnosticFlags, packagePath: fixturePath)
XCTAssertNoMatch(stderr, .contains("warning: '--enable-test-discovery' option is deprecated"))
}
#endif
}
func testList() async throws {
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (stdout, stderr) = try await SwiftPM.Test.execute(["list"], packagePath: fixturePath)
// build was run
XCTAssertMatch(stderr, .contains("Build complete!"))
// getting the lists
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testExample1"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/test_Example2"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testThrowing"))
}
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
// build first
do {
let (stdout, _) = try await SwiftPM.Build.execute(["--build-tests"], packagePath: fixturePath)
XCTAssertMatch(stdout, .contains("Build complete!"))
}
// list
do {
let (stdout, stderr) = try await SwiftPM.Test.execute(["list"], packagePath: fixturePath)
// build was run
XCTAssertMatch(stderr, .contains("Build complete!"))
// getting the lists
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testExample1"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/test_Example2"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testThrowing"))
}
}
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
// build first
do {
let (stdout, _) = try await SwiftPM.Build.execute(["--build-tests"], packagePath: fixturePath)
XCTAssertMatch(stdout, .contains("Build complete!"))
}
// list while skipping build
do {
let (stdout, stderr) = try await SwiftPM.Test.execute(["list", "--skip-build"], packagePath: fixturePath)
// build was not run
XCTAssertNoMatch(stderr, .contains("Build complete!"))
// getting the lists
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testExample1"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/test_Example2"))
XCTAssertMatch(stdout, .contains("SimpleTests.SimpleTests/testThrowing"))
}
}
}
func testBasicSwiftTestingIntegration() async throws {
#if !canImport(TestingDisabled)
try XCTSkipUnless(
nil != Environment.current["SWIFT_PM_SWIFT_TESTING_TESTS_ENABLED"],
"Skipping \(#function) because swift-testing tests are not explicitly enabled"
)
#endif
try await fixture(name: "Miscellaneous/TestDiscovery/SwiftTesting") { fixturePath in
do {
let (stdout, _) = try await SwiftPM.Test.execute(["--enable-swift-testing", "--disable-xctest"], packagePath: fixturePath)
XCTAssertMatch(stdout, .contains(#"Test "SOME TEST FUNCTION" started"#))
}
}
}
func testBasicSwiftTestingIntegration_ExperimentalFlag() async throws {
#if !canImport(TestingDisabled)
try XCTSkipUnless(
nil != Environment.current["SWIFT_PM_SWIFT_TESTING_TESTS_ENABLED"],
"Skipping \(#function) because swift-testing tests are not explicitly enabled"
)
#endif
try await fixture(name: "Miscellaneous/TestDiscovery/SwiftTesting") { fixturePath in
do {
let (stdout, _) = try await SwiftPM.Test.execute(["--enable-experimental-swift-testing", "--disable-xctest"], packagePath: fixturePath)
XCTAssertMatch(stdout, .contains(#"Test "SOME TEST FUNCTION" started"#))
}
}
}
#if !canImport(Darwin)
func testGeneratedMainIsConcurrencySafe_XCTest() async throws {
let strictConcurrencyFlags = ["-Xswiftc", "-strict-concurrency=complete"]
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try await SwiftPM.Test.execute(strictConcurrencyFlags, packagePath: fixturePath)
XCTAssertNoMatch(stderr, .contains("is not concurrency-safe"))
}
}
#endif
#if !canImport(Darwin)
func testGeneratedMainIsExistentialAnyClean() async throws {
let existentialAnyFlags = ["-Xswiftc", "-enable-upcoming-feature", "-Xswiftc", "ExistentialAny"]
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try await SwiftPM.Test.execute(existentialAnyFlags, packagePath: fixturePath)
XCTAssertNoMatch(stderr, .contains("error: use of protocol"))
}
}
#endif
func testLibraryEnvironmentVariable() async throws {
try await fixture(name: "Miscellaneous/CheckTestLibraryEnvironmentVariable") { fixturePath in
await XCTAssertAsyncNoThrow(try await SwiftPM.Test.execute(packagePath: fixturePath))
}
}
func testXCTestOnlyDoesNotLogAboutNoMatchingTests() async throws {
try await fixture(name: "Miscellaneous/TestDiscovery/Simple") { fixturePath in
let (_, stderr) = try await SwiftPM.Test.execute(["--disable-swift-testing"], packagePath: fixturePath)
XCTAssertNoMatch(stderr, .contains("No matching test cases were run"))
}
}
}