@@ -25,24 +25,30 @@ class TestEntryPointGenerationTaskAction: TaskAction {
25
25
let options = try Options . parse ( Array ( task. commandLineAsStrings. dropFirst ( ) ) )
26
26
27
27
var tests : [ IndexStore . TestCaseClass ] = [ ]
28
- var objects : [ Path ] = [ ]
29
- for linkerFilelist in options. linkerFilelist {
30
- let filelistContents = String ( String ( decoding: try executionDelegate. fs. read ( linkerFilelist) , as: UTF8 . self) )
31
- let entries = filelistContents. split ( separator: " \n " , omittingEmptySubsequences: true ) . map { Path ( $0) } . map {
32
- for indexUnitBasePath in options. indexUnitBasePath {
33
- if let remappedPath = generateIndexOutputPath ( from: $0, basePath: indexUnitBasePath) {
34
- return remappedPath
28
+ if options. discoverTests {
29
+ var objects : [ Path ] = [ ]
30
+ for linkerFilelist in options. linkerFilelist {
31
+ let filelistContents = String ( String ( decoding: try executionDelegate. fs. read ( linkerFilelist) , as: UTF8 . self) )
32
+ let entries = filelistContents. split ( separator: " \n " , omittingEmptySubsequences: true ) . map { Path ( $0) } . map {
33
+ for indexUnitBasePath in options. indexUnitBasePath {
34
+ if let remappedPath = generateIndexOutputPath ( from: $0, basePath: indexUnitBasePath) {
35
+ return remappedPath
36
+ }
35
37
}
38
+ return $0
36
39
}
37
- return $0
40
+ objects. append ( contentsOf: entries)
41
+ }
42
+ guard let indexStoreLibraryPath = options. indexStoreLibraryPath else {
43
+ outputDelegate. emitError ( " Test discovery was requested, but failed to lookup index store library in toolchain " )
44
+ return . failed
45
+ }
46
+ let indexStoreAPI = try IndexStoreAPI ( dylib: indexStoreLibraryPath)
47
+ for indexStore in options. indexStore {
48
+ let store = try IndexStore . open ( store: indexStore, api: indexStoreAPI)
49
+ let testInfo = try store. listTests ( in: objects)
50
+ tests. append ( contentsOf: testInfo)
38
51
}
39
- objects. append ( contentsOf: entries)
40
- }
41
- let indexStoreAPI = try IndexStoreAPI ( dylib: options. indexStoreLibraryPath)
42
- for indexStore in options. indexStore {
43
- let store = try IndexStore . open ( store: indexStore, api: indexStoreAPI)
44
- let testInfo = try store. listTests ( in: objects)
45
- tests. append ( contentsOf: testInfo)
46
52
}
47
53
48
54
try executionDelegate. fs. write ( options. output, contents: ByteString ( encodingAsUTF8: """
@@ -52,8 +58,8 @@ class TestEntryPointGenerationTaskAction: TaskAction {
52
58
53
59
\( testObservationFragment)
54
60
55
- import XCTest
56
- \( discoveredTestsFragment ( tests: tests) )
61
+ public import XCTest
62
+ \( discoveredTestsFragment ( tests: tests, options : options ) )
57
63
58
64
@main
59
65
@available(macOS 10.15, iOS 11, watchOS 4, tvOS 11, visionOS 1, *)
@@ -94,16 +100,7 @@ class TestEntryPointGenerationTaskAction: TaskAction {
94
100
}
95
101
}
96
102
#endif
97
- if testingLibrary == " xctest " {
98
- #if !os(Windows) && \( options. enableExperimentalTestOutput)
99
- _ = Self.testOutputPath().map { SwiftPMXCTestObserver(testOutputPath: testOutputPath) }
100
- #endif
101
- #if os(WASI)
102
- await XCTMain(__allDiscoveredTests()) as Never
103
- #else
104
- XCTMain(__allDiscoveredTests()) as Never
105
- #endif
106
- }
103
+ \( xctestFragment ( enableExperimentalTestOutput: options. enableExperimentalTestOutput, disable: !options. discoverTests) )
107
104
}
108
105
#else
109
106
static func main() async {
@@ -113,16 +110,7 @@ class TestEntryPointGenerationTaskAction: TaskAction {
113
110
await Testing.__swiftPMEntryPoint() as Never
114
111
}
115
112
#endif
116
- if testingLibrary == " xctest " {
117
- #if !os(Windows) && \( options. enableExperimentalTestOutput)
118
- _ = Self.testOutputPath().map { SwiftPMXCTestObserver(testOutputPath: testOutputPath) }
119
- #endif
120
- #if os(WASI)
121
- await XCTMain(__allDiscoveredTests()) as Never
122
- #else
123
- XCTMain(__allDiscoveredTests()) as Never
124
- #endif
125
- }
113
+ \( xctestFragment ( enableExperimentalTestOutput: options. enableExperimentalTestOutput, disable: !options. discoverTests) )
126
114
}
127
115
#endif
128
116
}
@@ -137,14 +125,18 @@ class TestEntryPointGenerationTaskAction: TaskAction {
137
125
138
126
private struct Options : ParsableArguments {
139
127
@Option var output : Path
140
- @Option var indexStoreLibraryPath : Path
141
- @Option var linkerFilelist : [ Path ]
142
- @Option var indexStore : [ Path ]
143
- @Option var indexUnitBasePath : [ Path ]
128
+ @Option var indexStoreLibraryPath : Path ? = nil
129
+ @Option ( ) var linkerFilelist : [ Path ] = [ ]
130
+ @Option var indexStore : [ Path ] = [ ]
131
+ @Option var indexUnitBasePath : [ Path ] = [ ]
144
132
@Flag var enableExperimentalTestOutput : Bool = false
133
+ @Flag var discoverTests : Bool = false
145
134
}
146
135
147
- private func discoveredTestsFragment( tests: [ IndexStore . TestCaseClass ] ) -> String {
136
+ private func discoveredTestsFragment( tests: [ IndexStore . TestCaseClass ] , options: Options ) -> String {
137
+ guard options. discoverTests else {
138
+ return " "
139
+ }
148
140
var fragment = " "
149
141
for moduleName in Set ( tests. map { $0. module } ) . sorted ( ) {
150
142
fragment += " @testable import \( moduleName) \n "
@@ -174,11 +166,29 @@ class TestEntryPointGenerationTaskAction: TaskAction {
174
166
return fragment
175
167
}
176
168
169
+ private func xctestFragment( enableExperimentalTestOutput: Bool , disable: Bool ) -> String {
170
+ guard !disable else {
171
+ return " "
172
+ }
173
+ return """
174
+ if testingLibrary == " xctest " {
175
+ #if !os(Windows) && \( enableExperimentalTestOutput)
176
+ _ = Self.testOutputPath().map { SwiftPMXCTestObserver(testOutputPath: testOutputPath) }
177
+ #endif
178
+ #if os(WASI)
179
+ await XCTMain(__allDiscoveredTests()) as Never
180
+ #else
181
+ XCTMain(__allDiscoveredTests()) as Never
182
+ #endif
183
+ }
184
+ """
185
+ }
186
+
177
187
private var testObservationFragment : String =
178
188
"""
179
189
#if !os(Windows) // Test observation is not supported on Windows
180
- import Foundation
181
- import XCTest
190
+ public import Foundation
191
+ public import XCTest
182
192
183
193
public final class SwiftPMXCTestObserver: NSObject {
184
194
let testOutputPath: String
@@ -562,7 +572,7 @@ class TestEntryPointGenerationTaskAction: TaskAction {
562
572
}
563
573
}
564
574
565
- import XCTest
575
+ public import XCTest
566
576
567
577
#if canImport(Darwin) // XCTAttachment is unavailable in swift-corelibs-xctest.
568
578
extension TestAttachment {
0 commit comments