@@ -19,11 +19,11 @@ import Testing
19
19
import TSCTestSupport
20
20
import SPMBuildCore
21
21
22
- public func expectMatch( _ value: String , _ pattern: StringPattern , file : StaticString = #file , line : UInt = #line ) {
23
- #expect( pattern ~= value, " Expected match for ' \( value) ' with pattern ' \( pattern) ' " )
22
+ public func expectMatch( _ value: String , _ pattern: StringPattern , sourceLocation : SourceLocation = #_sourceLocation ) {
23
+ #expect( pattern ~= value, " Expected match for ' \( value) ' with pattern ' \( pattern) ' " , sourceLocation : sourceLocation )
24
24
}
25
- public func expectNoMatch( _ value: String , _ pattern: StringPattern , file : StaticString = #file , line : UInt = #line ) {
26
- #expect( !( pattern ~= value) , " Expected no match for ' \( value) ' with pattern ' \( pattern) ' " )
25
+ public func expectNoMatch( _ value: String, _ pattern: StringPattern, sourceLocation : SourceLocation = #_sourceLocation ) {
26
+ #expect( !( pattern ~= value) , " Expected no match for ' \( value) ' with pattern ' \( pattern) ' " , sourceLocation : sourceLocation )
27
27
}
28
28
29
29
// Should be replaced by https://github.com/swiftlang/swift-package-manager/pull/8993/files#diff-150cbfd25c6baadfd6b02914bfa68513168ae042a0b01c89bf326b2429ba242a
@@ -39,20 +39,41 @@ public func expectFileExists(
39
39
)
40
40
}
41
41
42
+ public func expectNoSuchPath(
43
+ _ path: AbsolutePath,
44
+ sourceLocation: SourceLocation = #_sourceLocation
45
+ ) {
46
+ #expect(
47
+ !localFileSystem. exists ( path) ,
48
+ " Expected no such path ' \( path) ' " ,
49
+ sourceLocation: sourceLocation
50
+ )
51
+ }
52
+
53
+ public func expectDirectoryExists (
54
+ _ path: AbsolutePath,
55
+ sourceLocation: SourceLocation = #_sourceLocation
56
+ ) {
57
+ #expect(
58
+ localFileSystem. isDirectory ( path) ,
59
+ " Expected directory at ' \( path) ' " ,
60
+ sourceLocation: sourceLocation
61
+ )
62
+ }
63
+
42
64
public func expectBuilds(
43
65
_ path: AbsolutePath,
66
+ buildSystem: BuildSystemProvider . Kind,
44
67
configurations: Set< BuildConfiguration> = [ . debug, . release] ,
45
68
extraArgs: [ String] = [ ] ,
46
69
Xcc: [ String] = [ ] ,
47
70
Xld: [ String] = [ ] ,
48
71
Xswiftc: [ String] = [ ] ,
49
72
env: Environment? = nil ,
50
- file: StaticString = #file,
51
- line: UInt = #line,
52
- buildSystem: BuildSystemProvider . Kind = . native
73
+ sourceLocation: SourceLocation = #_sourceLocation,
53
74
) async {
54
75
for conf in configurations {
55
- await #expect( throws: Never . self) {
76
+ await #expect( throws: Never . self, sourceLocation : sourceLocation ) {
56
77
try await executeSwiftBuild (
57
78
path,
58
79
configuration: conf,
@@ -69,6 +90,14 @@ public func expectBuilds(
69
90
70
91
struct InitTests {
71
92
93
+ static let targetTriple: Triple = {
94
+ do {
95
+ return try UserToolchain . default. targetTriple
96
+ } catch {
97
+ fatalError ( " Failed to determine target triple: \( error) " )
98
+ }
99
+ } ( )
100
+
72
101
// MARK: TSCBasic package creation for each package type.
73
102
74
103
@Test func initPackageEmpty( ) throws {
@@ -105,7 +134,8 @@ struct InitTests {
105
134
}
106
135
}
107
136
108
- @Test func initPackageExecutable( ) async throws {
137
+ @Test ( arguments: [ BuildSystemProvider . Kind. native, . swiftbuild] )
138
+ func initPackageExecutable( buildSystem: BuildSystemProvider . Kind ) async throws {
109
139
try await testWithTemporaryDirectory { tmpPath in
110
140
let fs = localFileSystem
111
141
let path = tmpPath. appending ( " Foo " )
@@ -137,19 +167,25 @@ struct InitTests {
137
167
expectMatch ( manifestContents, . prefix( " // swift-tools-version: \( version < . v5_4 ? " " : " " ) \( versionSpecifier) \n " ) )
138
168
139
169
#expect( try fs. getDirectoryContents ( path. appending ( " Sources " ) . appending ( " Foo " ) ) == [ " Foo.swift " ] )
140
- await expectBuilds ( path, buildSystem: . native)
141
- let triple = try UserToolchain . default. targetTriple
142
- let binPath = path. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " )
143
- #if os(Windows)
144
- expectFileExists ( at: binPath. appending ( " Foo.exe " ) )
145
- #else
146
- expectFileExists ( at: binPath. appending ( " Foo " ) )
147
- #endif
148
- expectFileExists ( at: binPath. appending ( components: " Modules " , " Foo.swiftmodule " ) )
170
+
171
+ await expectBuilds ( path, buildSystem: buildSystem)
172
+
173
+ // Assert that the expected build products exist
174
+ let expectedPath = path
175
+ . appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent)
176
+ . appending ( components: buildSystem. binPathSuffixes ( for: BuildConfiguration . debug) )
177
+
178
+ expectFileExists ( at: expectedPath. appending ( executableName ( " Foo " ) ) )
179
+ if buildSystem == . native {
180
+ expectFileExists ( at: expectedPath. appending ( " Modules " , " Foo.swiftmodule " ) )
181
+ } else {
182
+ expectFileExists ( at: expectedPath. appending ( " Foo.swiftmodule " ) )
183
+ }
149
184
}
150
185
}
151
186
152
- @Test func initPackageExecutableCalledMain( ) async throws {
187
+ @Test ( arguments: [ BuildSystemProvider . Kind. native, . swiftbuild] )
188
+ func initPackageExecutableCalledMain( buildSystem: BuildSystemProvider . Kind ) async throws {
153
189
try await testWithTemporaryDirectory { tmpPath in
154
190
let fs = localFileSystem
155
191
let path = tmpPath. appending ( " main " )
@@ -166,12 +202,12 @@ struct InitTests {
166
202
try initPackage. writePackageStructure ( )
167
203
168
204
#expect( try fs. getDirectoryContents ( path. appending ( " Sources " ) . appending ( " main " ) ) == [ " MainEntrypoint.swift " ] )
169
- await expectBuilds ( path, buildSystem: . native )
205
+ await expectBuilds ( path, buildSystem: buildSystem )
170
206
}
171
207
}
172
208
173
- @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] )
174
- func initPackageLibraryWithXCTestOnly( packageType: InitPackage . PackageType ) async throws {
209
+ @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] , [ BuildSystemProvider . Kind . native , . swiftbuild ] )
210
+ func initPackageLibraryWithXCTestOnly( packageType: InitPackage . PackageType , buildSystem : BuildSystemProvider . Kind ) async throws {
175
211
try await testWithTemporaryDirectory { tmpPath in
176
212
let fs = localFileSystem
177
213
let path = tmpPath. appending ( " Foo " )
@@ -217,14 +253,29 @@ struct InitTests {
217
253
expectMatch ( testFileContents, . contains( " func testExample() throws " ) )
218
254
219
255
// Try building it
220
- await expectBuilds ( path, buildSystem: . native)
221
- let triple = try UserToolchain . default. targetTriple
222
- expectFileExists ( at: path. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
256
+ await expectBuilds ( path, buildSystem: buildSystem)
257
+
258
+ // Assert that the expected build products exist
259
+ let expectedPath = path
260
+ . appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent)
261
+ . appending ( components: buildSystem. binPathSuffixes ( for: BuildConfiguration . debug) )
262
+
263
+ switch packageType {
264
+ case . library:
265
+ if buildSystem == . native {
266
+ expectFileExists ( at: expectedPath. appending ( " Modules " , " Foo.swiftmodule " ) )
267
+ } else {
268
+ expectFileExists ( at: expectedPath. appending ( " Foo.swiftmodule " ) )
269
+ }
270
+ case . executable, . tool:
271
+ expectFileExists ( at: expectedPath. appending ( executableName ( " Foo " ) ) )
272
+ default : Issue . record ( " Unsupported package type for this test: \( packageType) " )
273
+ }
223
274
}
224
275
}
225
276
226
- @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] )
227
- func initPackagesWithSwiftTestingOnly( packageType: InitPackage . PackageType ) async throws {
277
+ @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] , [ BuildSystemProvider . Kind . native , . swiftbuild ] )
278
+ func initPackagesWithSwiftTestingOnly( packageType: InitPackage . PackageType , buildSystem : BuildSystemProvider . Kind ) async throws {
228
279
try testWithTemporaryDirectory { tmpPath in
229
280
let fs = localFileSystem
230
281
let path = tmpPath. appending ( " Foo " )
@@ -254,15 +305,14 @@ struct InitTests {
254
305
255
306
#if canImport(TestingDisabled)
256
307
// Try building it
257
- await expectBuilds ( path, buildSystem: . native)
258
- let triple = try UserToolchain . default. targetTriple
259
- expectFileExists ( path. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
308
+ await expectBuilds ( path, buildSystem: buildSystem)
309
+ expectFileExists ( path. appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
260
310
#endif
261
311
}
262
312
}
263
313
264
- @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] )
265
- func initPackageWithBothSwiftTestingAndXCTest( packageType: InitPackage . PackageType ) async throws {
314
+ @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] , [ BuildSystemProvider . Kind . native , . swiftbuild ] )
315
+ func initPackageWithBothSwiftTestingAndXCTest( packageType: InitPackage . PackageType , buildSystem : BuildSystemProvider . Kind ) async throws {
266
316
try testWithTemporaryDirectory { tmpPath in
267
317
let fs = localFileSystem
268
318
let path = tmpPath. appending ( " Foo " )
@@ -292,15 +342,14 @@ struct InitTests {
292
342
293
343
#if canImport(TestingDisabled)
294
344
// Try building it
295
- await expectBuilds ( path, buildSystem: . native)
296
- let triple = try UserToolchain . default. targetTriple
297
- expectFileExists ( path. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
345
+ await expectBuilds ( path, buildSystem: buildSystem)
346
+ expectFileExists ( path. appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
298
347
#endif
299
348
}
300
349
}
301
350
302
- @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] )
303
- func initPackageWithNoTests( packageType: InitPackage . PackageType ) async throws {
351
+ @Test ( arguments: [ InitPackage . PackageType. library, . executable, . tool] , [ BuildSystemProvider . Kind . native , . swiftbuild ] )
352
+ func initPackageWithNoTests( packageType: InitPackage . PackageType , buildSystem : BuildSystemProvider . Kind ) async throws {
304
353
try testWithTemporaryDirectory { tmpPath in
305
354
let fs = localFileSystem
306
355
let path = tmpPath. appending ( " Foo " )
@@ -323,13 +372,12 @@ struct InitTests {
323
372
let manifestContents : String = try localFileSystem. readFileContents ( manifest)
324
373
expectNoMatch ( manifestContents, . contains( #".testTarget"# ) )
325
374
326
- XCTAssertNoSuchPath ( path. appending ( " Tests " ) )
375
+ expectNoSuchPath ( path. appending ( " Tests " ) )
327
376
328
377
#if canImport(TestingDisabled)
329
378
// Try building it
330
- await expectBuilds ( path, buildSystem: . native)
331
- let triple = try UserToolchain . default. targetTriple
332
- expectFileExists ( path. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
379
+ await expectBuilds ( path, buildSystem: buildSystem)
380
+ expectFileExists ( path. appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent, " debug " , " Modules " , " Foo.swiftmodule " ) )
333
381
#endif
334
382
}
335
383
}
@@ -405,15 +453,16 @@ struct InitTests {
405
453
406
454
// MARK: Special case testing
407
455
408
- @Test func initPackageNonc99Directory( ) async throws {
456
+ @Test ( arguments: [ BuildSystemProvider . Kind. native, . swiftbuild] )
457
+ func initPackageNonc99Directory( buildSystem: BuildSystemProvider . Kind ) async throws {
409
458
try await withTemporaryDirectory ( removeTreeOnDeinit: true ) { tempDirPath in
410
- XCTAssertDirectoryExists ( tempDirPath)
459
+ expectDirectoryExists ( tempDirPath)
411
460
412
461
// Create a directory with non c99name.
413
462
let packageRoot = tempDirPath. appending ( " some-package " )
414
463
let packageName = packageRoot. basename
415
464
try localFileSystem. createDirectory ( packageRoot)
416
- XCTAssertDirectoryExists ( packageRoot)
465
+ expectDirectoryExists ( packageRoot)
417
466
418
467
// Create the package
419
468
let initPackage = try InitPackage (
@@ -426,19 +475,28 @@ struct InitTests {
426
475
try initPackage. writePackageStructure ( )
427
476
428
477
// Try building it.
429
- await expectBuilds ( packageRoot, buildSystem: . native)
430
- let triple = try UserToolchain . default. targetTriple
431
- expectFileExists ( at: packageRoot. appending ( components: " .build " , triple. platformBuildPathComponent, " debug " , " Modules " , " some_package.swiftmodule " ) )
478
+ await expectBuilds ( packageRoot, buildSystem: buildSystem)
479
+
480
+ // Assert that the expected build products exist
481
+ let expectedPath = packageRoot
482
+ . appending ( components: " .build " , Self . targetTriple. platformBuildPathComponent)
483
+ . appending ( components: buildSystem. binPathSuffixes ( for: BuildConfiguration . debug) )
484
+ if buildSystem == . native {
485
+ expectFileExists ( at: expectedPath. appending ( " Modules " , " some_package.swiftmodule " ) )
486
+ } else {
487
+ expectFileExists ( at: expectedPath. appending ( " some_package.swiftmodule " ) )
488
+ }
432
489
}
433
490
}
434
491
435
- @Test func nonC99NameExecutablePackage( ) async throws {
492
+ @Test ( arguments: [ BuildSystemProvider . Kind. native, . swiftbuild] )
493
+ func nonC99NameExecutablePackage( buildSystem: BuildSystemProvider . Kind ) async throws {
436
494
try await withTemporaryDirectory ( removeTreeOnDeinit: true ) { tempDirPath in
437
- XCTAssertDirectoryExists ( tempDirPath)
495
+ expectDirectoryExists ( tempDirPath)
438
496
439
497
let packageRoot = tempDirPath. appending ( " Foo " )
440
498
try localFileSystem. createDirectory ( packageRoot)
441
- XCTAssertDirectoryExists ( packageRoot)
499
+ expectDirectoryExists ( packageRoot)
442
500
443
501
// Create package with non c99name.
444
502
let initPackage = try InitPackage (
@@ -449,7 +507,7 @@ struct InitTests {
449
507
)
450
508
try initPackage. writePackageStructure ( )
451
509
452
- await expectBuilds ( packageRoot, buildSystem: . native )
510
+ await expectBuilds ( packageRoot, buildSystem: buildSystem )
453
511
}
454
512
}
455
513
0 commit comments