Skip to content

Commit f417778

Browse files
committed
Run CFamilyTargetTests with Swift Build backend
1 parent 985b56f commit f417778

File tree

1 file changed

+70
-44
lines changed

1 file changed

+70
-44
lines changed

Tests/FunctionalTests/CFamilyTargetTests.swift

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,94 +16,120 @@ import PackageGraph
1616
import PackageLoading
1717
import PackageModel
1818
import SourceControl
19+
import SPMBuildCore
1920
import _InternalTestSupport
2021
import Workspace
21-
import XCTest
22+
import Testing
2223

2324
import class Basics.AsyncProcess
2425

2526
/// Asserts if a directory (recursively) contains a file.
26-
private func XCTAssertDirectoryContainsFile(dir: AbsolutePath, filename: String, file: StaticString = #file, line: UInt = #line) {
27+
private func assertDirectoryContainsFile(dir: AbsolutePath, filename: String, sourceLocation: SourceLocation = #_sourceLocation) {
2728
do {
2829
for entry in try walk(dir) {
2930
if entry.basename == filename { return }
3031
}
3132
} catch {
32-
XCTFail("Failed with error \(error)", file: file, line: line)
33+
Issue.record(StringError("Failed with error \(error)"), sourceLocation: sourceLocation)
3334
}
34-
XCTFail("Directory \(dir) does not contain \(file)", file: file, line: line)
35+
Issue.record(StringError("Directory \(dir) does not contain \(filename)"), sourceLocation: sourceLocation)
3536
}
3637

37-
final class CFamilyTargetTestCase: XCTestCase {
38-
func testCLibraryWithSpaces() async throws {
38+
@Suite(.serialized)
39+
struct CFamilyTargetTestCase {
40+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
41+
func testCLibraryWithSpaces(buildSystem: BuildSystemProvider.Kind) async throws {
3942
try await fixture(name: "CFamilyTargets/CLibraryWithSpaces") { fixturePath in
40-
await XCTAssertBuilds(fixturePath)
41-
let debugPath = fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
42-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Bar.c.o")
43-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
43+
try await executeSwiftBuild(fixturePath, buildSystem: buildSystem)
44+
if buildSystem == .native {
45+
let debugPath = fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
46+
assertDirectoryContainsFile(dir: debugPath, filename: "Bar.c.o")
47+
assertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
48+
}
4449
}
4550
}
4651

47-
func testCUsingCAndSwiftDep() async throws {
52+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
53+
func testCUsingCAndSwiftDep(buildSystem: BuildSystemProvider.Kind) async throws {
4854
try await fixture(name: "DependencyResolution/External/CUsingCDep") { fixturePath in
4955
let packageRoot = fixturePath.appending("Bar")
50-
await XCTAssertBuilds(packageRoot)
51-
let debugPath = fixturePath.appending(components: "Bar", ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
52-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Sea.c.o")
53-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
56+
try await executeSwiftBuild(packageRoot, buildSystem: buildSystem)
57+
if buildSystem == .native {
58+
let debugPath = fixturePath.appending(components: "Bar", ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
59+
assertDirectoryContainsFile(dir: debugPath, filename: "Sea.c.o")
60+
assertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
61+
}
5462
let path = try SwiftPM.packagePath(for: "Foo", packageRoot: packageRoot)
55-
XCTAssertEqual(try GitRepository(path: path).getTags(), ["1.2.3"])
63+
#expect(try GitRepository(path: path).getTags() == ["1.2.3"])
5664
}
5765
}
5866

59-
func testModuleMapGenerationCases() async throws {
67+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
68+
func testModuleMapGenerationCases(buildSystem: BuildSystemProvider.Kind) async throws {
6069
try await fixture(name: "CFamilyTargets/ModuleMapGenerationCases") { fixturePath in
61-
await XCTAssertBuilds(fixturePath)
62-
let debugPath = fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
63-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Jaz.c.o")
64-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "main.swift.o")
65-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "FlatInclude.c.o")
66-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "UmbrellaHeader.c.o")
70+
try await executeSwiftBuild(fixturePath, buildSystem: buildSystem)
71+
if buildSystem == .native {
72+
let debugPath = fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
73+
assertDirectoryContainsFile(dir: debugPath, filename: "Jaz.c.o")
74+
assertDirectoryContainsFile(dir: debugPath, filename: "main.swift.o")
75+
assertDirectoryContainsFile(dir: debugPath, filename: "FlatInclude.c.o")
76+
assertDirectoryContainsFile(dir: debugPath, filename: "UmbrellaHeader.c.o")
77+
}
6778
}
6879
}
69-
70-
func testNoIncludeDirCheck() async throws {
80+
81+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
82+
func testNoIncludeDirCheck(buildSystem: BuildSystemProvider.Kind) async throws {
7183
try await fixture(name: "CFamilyTargets/CLibraryNoIncludeDir") { fixturePath in
72-
await XCTAssertAsyncThrowsError(try await executeSwiftBuild(fixturePath), "This build should throw an error") { err in
73-
// The err.localizedDescription doesn't capture the detailed error string so interpolate
74-
let errStr = "\(err)"
75-
let missingIncludeDirStr = "\(ModuleError.invalidPublicHeadersDirectory("Cfactorial"))"
76-
XCTAssert(errStr.contains(missingIncludeDirStr))
84+
var buildError: (any Error)? = nil
85+
do {
86+
try await executeSwiftBuild(fixturePath, buildSystem: buildSystem)
87+
} catch {
88+
buildError = error
7789
}
90+
guard let buildError else {
91+
Issue.record(StringError("This build should throw an error"))
92+
return
93+
}
94+
// The err.localizedDescription doesn't capture the detailed error string so interpolate
95+
let errStr = "\(buildError)"
96+
let missingIncludeDirStr = "\(ModuleError.invalidPublicHeadersDirectory("Cfactorial"))"
97+
#expect(errStr.contains(missingIncludeDirStr))
7898
}
7999
}
80100

81-
func testCanForwardExtraFlagsToClang() async throws {
101+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
102+
func testCanForwardExtraFlagsToClang(buildSystem: BuildSystemProvider.Kind) async throws {
82103
// Try building a fixture which needs extra flags to be able to build.
83104
try await fixture(name: "CFamilyTargets/CDynamicLookup") { fixturePath in
84-
await XCTAssertBuilds(fixturePath, Xld: ["-undefined", "dynamic_lookup"])
105+
try await executeSwiftBuild(fixturePath, Xld: ["-undefined", "dynamic_lookup"], buildSystem: buildSystem)
85106
let debugPath = fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug")
86-
XCTAssertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
107+
if buildSystem == .native {
108+
assertDirectoryContainsFile(dir: debugPath, filename: "Foo.c.o")
109+
}
87110
}
88111
}
89112

90-
func testObjectiveCPackageWithTestTarget() async throws {
91-
#if !os(macOS)
92-
try XCTSkipIf(true, "test is only supported on macOS")
93-
#endif
113+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
114+
func testObjectiveCPackageWithTestTarget(buildSystem: BuildSystemProvider.Kind) async throws {
94115
try await fixture(name: "CFamilyTargets/ObjCmacOSPackage") { fixturePath in
95116
// Build the package.
96-
await XCTAssertBuilds(fixturePath)
97-
XCTAssertDirectoryContainsFile(dir: fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug"), filename: "HelloWorldExample.m.o")
117+
try await executeSwiftBuild(fixturePath, buildSystem: buildSystem)
118+
if buildSystem == .native {
119+
assertDirectoryContainsFile(dir: fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug"), filename: "HelloWorldExample.m.o")
120+
}
98121
// Run swift-test on package.
99-
await XCTAssertSwiftTest(fixturePath)
122+
try await executeSwiftTest(fixturePath, buildSystem: buildSystem)
100123
}
101124
}
102-
103-
func testCanBuildRelativeHeaderSearchPaths() async throws {
125+
126+
@Test(arguments: [BuildSystemProvider.Kind.native, .swiftbuild])
127+
func testCanBuildRelativeHeaderSearchPaths(buildSystem: BuildSystemProvider.Kind) async throws {
104128
try await fixture(name: "CFamilyTargets/CLibraryParentSearchPath") { fixturePath in
105-
await XCTAssertBuilds(fixturePath)
106-
XCTAssertDirectoryContainsFile(dir: fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug"), filename: "HeaderInclude.swiftmodule")
129+
try await executeSwiftBuild(fixturePath, buildSystem: buildSystem)
130+
if buildSystem == .native {
131+
assertDirectoryContainsFile(dir: fixturePath.appending(components: ".build", try UserToolchain.default.targetTriple.platformBuildPathComponent, "debug"), filename: "HeaderInclude.swiftmodule")
132+
}
107133
}
108134
}
109135
}

0 commit comments

Comments
 (0)