22//
33// This source file is part of the Swift open source project
44//
5- // Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
5+ // Copyright (c) 2014-2025 Apple Inc. and the Swift project authors
66// Licensed under Apache License v2.0 with Runtime Library Exception
77//
88// See http://swift.org/LICENSE.txt for license information
99// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010//
1111//===----------------------------------------------------------------------===//
12+ import Foundation
1213
1314import Basics
1415import Commands
@@ -18,98 +19,246 @@ import PackageModel
1819import SourceControl
1920import _InternalTestSupport
2021import Workspace
21- import XCTest
22+ import Testing
2223
2324import class Basics. AsyncProcess
2425
25- /// Asserts if a directory (recursively) contains a file.
26- private func XCTAssertDirectoryContainsFile( dir: AbsolutePath , filename: String , file: StaticString = #file, line: UInt = #line) {
26+ /// Expects a directory (recursively) contains a file.
27+ fileprivate func expectDirectoryContainsFile(
28+ dir: AbsolutePath ,
29+ filename: String ,
30+ sourceLocation: SourceLocation = #_sourceLocation,
31+ ) {
2732 do {
2833 for entry in try walk ( dir) {
2934 if entry. basename == filename { return }
3035 }
3136 } catch {
32- XCTFail ( " Failed with error \( error) " , file : file , line : line )
37+ Issue . record ( " Failed with error \( error) " , sourceLocation : sourceLocation )
3338 }
34- XCTFail ( " Directory \( dir) does not contain \( file ) " , file : file , line : line )
39+ Issue . record ( " Directory \( dir) does not contain \( filename ) " , sourceLocation : sourceLocation )
3540}
3641
37- final class CFamilyTargetTestCase : XCTestCase {
38- func testCLibraryWithSpaces( ) async throws {
39- try await fixtureXCTest ( name: " CFamilyTargets/CLibraryWithSpaces " ) { fixturePath in
40- await XCTAssertBuilds ( fixturePath, buildSystem: . native)
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 " )
42+ @Suite (
43+ . tags(
44+ . TestSize. large,
45+ ) ,
46+ )
47+ struct CFamilyTargetTestCase {
48+ @Test (
49+ . issue( " https://github.com/swiftlang/swift-build/issues/333 " , relationship: . defect) ,
50+ . tags(
51+ . Feature. Command. Build,
52+ ) ,
53+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
54+ )
55+ func cLibraryWithSpaces(
56+ data: BuildData ,
57+ ) async throws {
58+ try await withKnownIssue ( isIntermittent: true ) {
59+ try await fixture ( name: " CFamilyTargets/CLibraryWithSpaces " ) { fixturePath in
60+ try await executeSwiftBuild (
61+ fixturePath,
62+ configuration: data. config,
63+ buildSystem: data. buildSystem,
64+ )
65+ if data. buildSystem == . native {
66+ let binPath = try fixturePath. appending ( components: data. buildSystem. binPath ( for: data. config) )
67+ expectDirectoryContainsFile ( dir: binPath, filename: " Bar.c.o " )
68+ expectDirectoryContainsFile ( dir: binPath, filename: " Foo.c.o " )
69+ }
70+ }
71+ } when: {
72+ data. buildSystem == . swiftbuild
4473 }
4574 }
4675
47- func testCUsingCAndSwiftDep( ) async throws {
48- try await fixtureXCTest ( name: " DependencyResolution/External/CUsingCDep " ) { fixturePath in
49- let packageRoot = fixturePath. appending ( " Bar " )
50- await XCTAssertBuilds ( packageRoot, buildSystem: . native)
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 " )
54- let path = try SwiftPM . packagePath ( for: " Foo " , packageRoot: packageRoot)
55- XCTAssertEqual ( try GitRepository ( path: path) . getTags ( ) , [ " 1.2.3 " ] )
76+ @Test (
77+ . tags(
78+ . Feature. Command. Build,
79+ ) ,
80+ . IssueWindowsLongPath,
81+ . IssueWindowsPathLastConponent,
82+ . IssueWindowsRelativePathAssert,
83+ . IssueWindowsCannotSaveAttachment,
84+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
85+ )
86+ func cUsingCAndSwiftDep(
87+ data: BuildData ,
88+ ) async throws {
89+ try await withKnownIssue ( isIntermittent: true ) {
90+ try await fixture ( name: " DependencyResolution/External/CUsingCDep " ) { fixturePath in
91+ let packageRoot = fixturePath. appending ( " Bar " )
92+ try await executeSwiftBuild (
93+ packageRoot,
94+ configuration: data. config,
95+ buildSystem: data. buildSystem,
96+ )
97+ if data. buildSystem == . native {
98+ let binPath = try packageRoot. appending ( components: data. buildSystem. binPath ( for: data. config) )
99+ expectDirectoryContainsFile ( dir: binPath, filename: " Sea.c.o " )
100+ expectDirectoryContainsFile ( dir: binPath, filename: " Foo.c.o " )
101+ }
102+ let path = try SwiftPM . packagePath ( for: " Foo " , packageRoot: packageRoot)
103+ let actualTags = try GitRepository ( path: path) . getTags ( )
104+ #expect( actualTags == [ " 1.2.3 " ] )
105+ }
106+ } when: {
107+ ProcessInfo . hostOperatingSystem == . windows && data. buildSystem == . swiftbuild
56108 }
57109 }
58110
59- func testModuleMapGenerationCases( ) async throws {
60- try await fixtureXCTest ( name: " CFamilyTargets/ModuleMapGenerationCases " ) { fixturePath in
61- await XCTAssertBuilds ( fixturePath, buildSystem: . native)
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 " )
111+ @Test (
112+ . tags(
113+ . Feature. Command. Build,
114+ ) ,
115+ . IssueWindowsLongPath,
116+ . IssueWindowsPathLastConponent,
117+ . IssueWindowsRelativePathAssert,
118+ . IssueWindowsCannotSaveAttachment,
119+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
120+ )
121+ func moduleMapGenerationCases(
122+ data: BuildData ,
123+ ) async throws {
124+ try await withKnownIssue ( isIntermittent: true ) {
125+ try await fixture ( name: " CFamilyTargets/ModuleMapGenerationCases " ) { fixturePath in
126+ try await executeSwiftBuild (
127+ fixturePath,
128+ configuration: data. config,
129+ buildSystem: data. buildSystem,
130+ )
131+ if data. buildSystem == . native {
132+ let binPath = try fixturePath. appending ( components: data. buildSystem. binPath ( for: data. config) )
133+ expectDirectoryContainsFile ( dir: binPath, filename: " Jaz.c.o " )
134+ expectDirectoryContainsFile ( dir: binPath, filename: " main.swift.o " )
135+ expectDirectoryContainsFile ( dir: binPath, filename: " FlatInclude.c.o " )
136+ expectDirectoryContainsFile ( dir: binPath, filename: " UmbrellaHeader.c.o " )
137+ }
138+ }
139+ } when: {
140+ ProcessInfo . hostOperatingSystem == . windows && data. buildSystem == . swiftbuild
67141 }
68142 }
69-
70- func testNoIncludeDirCheck( ) async throws {
71- try await fixtureXCTest ( name: " CFamilyTargets/CLibraryNoIncludeDir " ) { fixturePath in
72- await XCTAssertAsyncThrowsError (
143+
144+ @Test (
145+ . tags(
146+ . Feature. Command. Build,
147+ ) ,
148+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
149+ )
150+ func noIncludeDirCheck(
151+ data: BuildData ,
152+ ) async throws {
153+ try await fixture ( name: " CFamilyTargets/CLibraryNoIncludeDir " ) { fixturePath in
154+ let error = try await #require( throws: ( any Error ) . self) {
73155 try await executeSwiftBuild (
74156 fixturePath,
75- buildSystem: . native,
76- ) ,
77- " This build should throw an error " ,
78- ) { err in
79- // The err.localizedDescription doesn't capture the detailed error string so interpolate
80- let errStr = " \( err) "
81- let missingIncludeDirStr = " \( ModuleError . invalidPublicHeadersDirectory ( " Cfactorial " ) ) "
82- XCTAssert ( errStr. contains ( missingIncludeDirStr) )
157+ configuration: data. config,
158+ buildSystem: data. buildSystem,
159+ )
83160 }
161+
162+ let errString = " \( error) "
163+ let missingIncludeDirStr = " \( ModuleError . invalidPublicHeadersDirectory ( " Cfactorial " ) ) "
164+ #expect( errString. contains ( missingIncludeDirStr) )
84165 }
85166 }
86167
87- func testCanForwardExtraFlagsToClang( ) async throws {
88- // Try building a fixture which needs extra flags to be able to build.
89- try await fixtureXCTest ( name: " CFamilyTargets/CDynamicLookup " ) { fixturePath in
90- await XCTAssertBuilds ( fixturePath, Xld: [ " -undefined " , " dynamic_lookup " ] , buildSystem: . native)
91- let debugPath = fixturePath. appending ( components: " .build " , try UserToolchain . default. targetTriple. platformBuildPathComponent, " debug " )
92- XCTAssertDirectoryContainsFile ( dir: debugPath, filename: " Foo.c.o " )
168+ @Test (
169+ . tags(
170+ . Feature. Command. Build,
171+ ) ,
172+ . IssueWindowsLongPath,
173+ . IssueWindowsPathLastConponent,
174+ . IssueWindowsRelativePathAssert,
175+ . IssueWindowsCannotSaveAttachment,
176+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
177+ )
178+ func canForwardExtraFlagsToClang(
179+ data: BuildData ,
180+ ) async throws {
181+ try await withKnownIssue ( isIntermittent: true ) {
182+ try await fixture ( name: " CFamilyTargets/CDynamicLookup " ) { fixturePath in
183+ try await executeSwiftBuild (
184+ fixturePath,
185+ configuration: data. config,
186+ Xld: [ " -undefined " , " dynamic_lookup " ] ,
187+ buildSystem: data. buildSystem,
188+ )
189+ if data. buildSystem == . native {
190+ let binPath = try fixturePath. appending ( components: data. buildSystem. binPath ( for: data. config) )
191+ expectDirectoryContainsFile ( dir: binPath, filename: " Foo.c.o " )
192+ }
193+ }
194+ } when: {
195+ ProcessInfo . hostOperatingSystem == . windows && data. buildSystem == . swiftbuild
93196 }
94197 }
95198
96- func testObjectiveCPackageWithTestTarget( ) async throws {
97- #if !os(macOS)
98- try XCTSkipIf ( true , " test is only supported on macOS " )
99- #endif
100- try await fixtureXCTest ( name: " CFamilyTargets/ObjCmacOSPackage " ) { fixturePath in
199+ @Test (
200+ . tags(
201+ . Feature. Command. Build,
202+ . Feature. Command. Test,
203+ ) ,
204+ . requireHostOS( . macOS) ,
205+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
206+
207+ )
208+ func objectiveCPackageWithTestTarget(
209+ data: BuildData ,
210+
211+ ) async throws {
212+ try await fixture ( name: " CFamilyTargets/ObjCmacOSPackage " ) { fixturePath in
101213 // Build the package.
102- await XCTAssertBuilds ( fixturePath, buildSystem: . native)
103- XCTAssertDirectoryContainsFile ( dir: fixturePath. appending ( components: " .build " , try UserToolchain . default. targetTriple. platformBuildPathComponent, " debug " ) , filename: " HelloWorldExample.m.o " )
214+ try await executeSwiftBuild (
215+ fixturePath,
216+ configuration: data. config,
217+ buildSystem: data. buildSystem,
218+ )
219+ if data. buildSystem == . native {
220+ let binPath = try fixturePath. appending ( components: data. buildSystem. binPath ( for: data. config) )
221+ expectDirectoryContainsFile ( dir: binPath, filename: " HelloWorldExample.m.o " )
222+ expectDirectoryContainsFile ( dir: binPath, filename: " HelloWorldExample.m.o " )
223+ }
104224 // Run swift-test on package.
105- await XCTAssertSwiftTest ( fixturePath, buildSystem: . native)
225+ try await executeSwiftTest (
226+ fixturePath,
227+ configuration: data. config,
228+ buildSystem: data. buildSystem,
229+ )
230+
106231 }
107232 }
108-
109- func testCanBuildRelativeHeaderSearchPaths( ) async throws {
110- try await fixtureXCTest ( name: " CFamilyTargets/CLibraryParentSearchPath " ) { fixturePath in
111- await XCTAssertBuilds ( fixturePath, buildSystem: . native)
112- XCTAssertDirectoryContainsFile ( dir: fixturePath. appending ( components: " .build " , try UserToolchain . default. targetTriple. platformBuildPathComponent, " debug " ) , filename: " HeaderInclude.swiftmodule " )
233+
234+ @Test (
235+ . tags(
236+ . Feature. Command. Build,
237+ ) ,
238+ . IssueWindowsLongPath,
239+ . IssueWindowsPathLastConponent,
240+ . IssueWindowsRelativePathAssert,
241+ . IssueWindowsCannotSaveAttachment,
242+ arguments: getBuildData ( for: SupportedBuildSystemOnAllPlatforms) ,
243+ )
244+ func canBuildRelativeHeaderSearchPaths(
245+ data: BuildData ,
246+
247+ ) async throws {
248+ try await withKnownIssue ( isIntermittent: true ) {
249+ try await fixture ( name: " CFamilyTargets/CLibraryParentSearchPath " ) { fixturePath in
250+ try await executeSwiftBuild (
251+ fixturePath,
252+ configuration: data. config,
253+ buildSystem: data. buildSystem,
254+ )
255+ if data. buildSystem == . native {
256+ let binPath = try fixturePath. appending ( components: data. buildSystem. binPath ( for: data. config) )
257+ expectDirectoryContainsFile ( dir: binPath, filename: " HeaderInclude.swiftmodule " )
258+ }
259+ }
260+ } when: {
261+ ProcessInfo . hostOperatingSystem == . windows && data. buildSystem == . swiftbuild
113262 }
114263 }
115264}
0 commit comments