Skip to content

Commit 5dd6e5c

Browse files
committed
Run tests against swiftbuild and native build systems
1 parent 8bf6b92 commit 5dd6e5c

File tree

1 file changed

+109
-51
lines changed

1 file changed

+109
-51
lines changed

Tests/WorkspaceTests/InitTests.swift

Lines changed: 109 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import Testing
1919
import TSCTestSupport
2020
import SPMBuildCore
2121

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)
2424
}
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)
2727
}
2828

2929
// Should be replaced by https://github.com/swiftlang/swift-package-manager/pull/8993/files#diff-150cbfd25c6baadfd6b02914bfa68513168ae042a0b01c89bf326b2429ba242a
@@ -39,20 +39,41 @@ public func expectFileExists(
3939
)
4040
}
4141

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+
4264
public func expectBuilds(
4365
_ path: AbsolutePath,
66+
buildSystem: BuildSystemProvider.Kind,
4467
configurations: Set<BuildConfiguration> = [.debug, .release],
4568
extraArgs: [String] = [],
4669
Xcc: [String] = [],
4770
Xld: [String] = [],
4871
Xswiftc: [String] = [],
4972
env: Environment? = nil,
50-
file: StaticString = #file,
51-
line: UInt = #line,
52-
buildSystem: BuildSystemProvider.Kind = .native
73+
sourceLocation: SourceLocation = #_sourceLocation,
5374
) async {
5475
for conf in configurations {
55-
await #expect(throws: Never.self) {
76+
await #expect(throws: Never.self, sourceLocation: sourceLocation) {
5677
try await executeSwiftBuild(
5778
path,
5879
configuration: conf,
@@ -69,6 +90,14 @@ public func expectBuilds(
6990

7091
struct InitTests {
7192

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+
72101
// MARK: TSCBasic package creation for each package type.
73102

74103
@Test func initPackageEmpty() throws {
@@ -105,7 +134,8 @@ struct InitTests {
105134
}
106135
}
107136

108-
@Test func initPackageExecutable() async throws {
137+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
138+
func initPackageExecutable(buildSystem: BuildSystemProvider.Kind) async throws {
109139
try await testWithTemporaryDirectory { tmpPath in
110140
let fs = localFileSystem
111141
let path = tmpPath.appending("Foo")
@@ -137,19 +167,25 @@ struct InitTests {
137167
expectMatch(manifestContents, .prefix("// swift-tools-version:\(version < .v5_4 ? "" : " ")\(versionSpecifier)\n"))
138168

139169
#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+
}
149184
}
150185
}
151186

152-
@Test func initPackageExecutableCalledMain() async throws {
187+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
188+
func initPackageExecutableCalledMain(buildSystem: BuildSystemProvider.Kind) async throws {
153189
try await testWithTemporaryDirectory { tmpPath in
154190
let fs = localFileSystem
155191
let path = tmpPath.appending("main")
@@ -166,12 +202,12 @@ struct InitTests {
166202
try initPackage.writePackageStructure()
167203

168204
#expect(try fs.getDirectoryContents(path.appending("Sources").appending("main")) == ["MainEntrypoint.swift"])
169-
await expectBuilds(path, buildSystem: .native)
205+
await expectBuilds(path, buildSystem: buildSystem)
170206
}
171207
}
172208

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 {
175211
try await testWithTemporaryDirectory { tmpPath in
176212
let fs = localFileSystem
177213
let path = tmpPath.appending("Foo")
@@ -217,14 +253,29 @@ struct InitTests {
217253
expectMatch(testFileContents, .contains("func testExample() throws"))
218254

219255
// 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+
}
223274
}
224275
}
225276

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 {
228279
try testWithTemporaryDirectory { tmpPath in
229280
let fs = localFileSystem
230281
let path = tmpPath.appending("Foo")
@@ -254,15 +305,14 @@ struct InitTests {
254305

255306
#if canImport(TestingDisabled)
256307
// 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"))
260310
#endif
261311
}
262312
}
263313

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 {
266316
try testWithTemporaryDirectory { tmpPath in
267317
let fs = localFileSystem
268318
let path = tmpPath.appending("Foo")
@@ -292,15 +342,14 @@ struct InitTests {
292342

293343
#if canImport(TestingDisabled)
294344
// 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"))
298347
#endif
299348
}
300349
}
301350

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 {
304353
try testWithTemporaryDirectory { tmpPath in
305354
let fs = localFileSystem
306355
let path = tmpPath.appending("Foo")
@@ -323,13 +372,12 @@ struct InitTests {
323372
let manifestContents: String = try localFileSystem.readFileContents(manifest)
324373
expectNoMatch(manifestContents, .contains(#".testTarget"#))
325374

326-
XCTAssertNoSuchPath(path.appending("Tests"))
375+
expectNoSuchPath(path.appending("Tests"))
327376

328377
#if canImport(TestingDisabled)
329378
// 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"))
333381
#endif
334382
}
335383
}
@@ -405,15 +453,16 @@ struct InitTests {
405453

406454
// MARK: Special case testing
407455

408-
@Test func initPackageNonc99Directory() async throws {
456+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
457+
func initPackageNonc99Directory(buildSystem: BuildSystemProvider.Kind) async throws {
409458
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
410-
XCTAssertDirectoryExists(tempDirPath)
459+
expectDirectoryExists(tempDirPath)
411460

412461
// Create a directory with non c99name.
413462
let packageRoot = tempDirPath.appending("some-package")
414463
let packageName = packageRoot.basename
415464
try localFileSystem.createDirectory(packageRoot)
416-
XCTAssertDirectoryExists(packageRoot)
465+
expectDirectoryExists(packageRoot)
417466

418467
// Create the package
419468
let initPackage = try InitPackage(
@@ -426,19 +475,28 @@ struct InitTests {
426475
try initPackage.writePackageStructure()
427476

428477
// 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+
}
432489
}
433490
}
434491

435-
@Test func nonC99NameExecutablePackage() async throws {
492+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
493+
func nonC99NameExecutablePackage(buildSystem: BuildSystemProvider.Kind) async throws {
436494
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
437-
XCTAssertDirectoryExists(tempDirPath)
495+
expectDirectoryExists(tempDirPath)
438496

439497
let packageRoot = tempDirPath.appending("Foo")
440498
try localFileSystem.createDirectory(packageRoot)
441-
XCTAssertDirectoryExists(packageRoot)
499+
expectDirectoryExists(packageRoot)
442500

443501
// Create package with non c99name.
444502
let initPackage = try InitPackage(
@@ -449,7 +507,7 @@ struct InitTests {
449507
)
450508
try initPackage.writePackageStructure()
451509

452-
await expectBuilds(packageRoot, buildSystem: .native)
510+
await expectBuilds(packageRoot, buildSystem: buildSystem)
453511
}
454512
}
455513

0 commit comments

Comments
 (0)