Skip to content

Commit db5a646

Browse files
authored
Move __swiftPMEntryPoint() to its own file. (#384)
Tidying up the entry point code and moving the SwiftPM-specific entry points to their own file. Renames internal functions to not mention SwiftPM if they also support the general entry point mechanism. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 2bfffa9 commit db5a646

File tree

5 files changed

+81
-70
lines changed

5 files changed

+81
-70
lines changed

Sources/Testing/EntryPoints/EntryPoint.swift

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,6 @@
1010

1111
private import TestingInternals
1212

13-
/// The entry point to the testing library used by Swift Package Manager.
14-
///
15-
/// - Parameters:
16-
/// - args: A previously-parsed command-line arguments structure to interpret.
17-
/// If `nil`, a new instance is created from the command-line arguments to
18-
/// the current process.
19-
///
20-
/// - Returns: The result of invoking the testing library. The type of this
21-
/// value is subject to change.
22-
///
23-
/// This function examines the command-line arguments represented by `args` and
24-
/// then invokes available tests in the current process.
25-
///
26-
/// - Warning: This function is used by Swift Package Manager. Do not call it
27-
/// directly.
28-
@_disfavoredOverload public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> CInt {
29-
await entryPoint(passing: args, eventHandler: nil)
30-
}
31-
32-
/// The entry point to the testing library used by Swift Package Manager.
33-
///
34-
/// - Parameters:
35-
/// - args: A previously-parsed command-line arguments structure to interpret.
36-
/// If `nil`, a new instance is created from the command-line arguments to
37-
/// the current process.
38-
///
39-
/// This function examines the command-line arguments to the current process
40-
/// and then invokes available tests in the current process. When the tests
41-
/// complete, the process is terminated. If tests were successful, an exit code
42-
/// of `EXIT_SUCCESS` is used; otherwise, a (possibly platform-specific) value
43-
/// such as `EXIT_FAILURE` is used instead.
44-
///
45-
/// - Warning: This function is used by Swift Package Manager. Do not call it
46-
/// directly.
47-
public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> Never {
48-
let exitCode: CInt = await __swiftPMEntryPoint(passing: args)
49-
exit(exitCode)
50-
}
51-
5213
/// The common implementation of the entry point functions in this file.
5314
///
5415
/// - Parameters:
@@ -62,7 +23,7 @@ func entryPoint(passing args: consuming __CommandLineArguments_v0?, eventHandler
6223
do {
6324
let args = try args ?? parseCommandLineArguments(from: CommandLine.arguments())
6425
if args.listTests {
65-
for testID in await listTestsForSwiftPM(Test.all) {
26+
for testID in await listTestsForEntryPoint(Test.all) {
6627
#if SWT_TARGET_OS_APPLE && !SWT_NO_FILE_IO
6728
try? FileHandle.stdout.write("\(testID)\n")
6829
#else
@@ -72,13 +33,13 @@ func entryPoint(passing args: consuming __CommandLineArguments_v0?, eventHandler
7233
} else {
7334
#if !SWT_NO_EXIT_TESTS
7435
// If an exit test was specified, run it. `exitTest` returns `Never`.
75-
if let exitTest = ExitTest.findInEnvironmentForSwiftPM() {
36+
if let exitTest = ExitTest.findInEnvironmentForEntryPoint() {
7637
await exitTest()
7738
}
7839
#endif
7940

8041
// Configure the test runner.
81-
var configuration = try configurationForSwiftPMEntryPoint(from: args)
42+
var configuration = try configurationForEntryPoint(from: args)
8243

8344
// Set up the event handler.
8445
configuration.eventHandler = { [oldEventHandler = configuration.eventHandler] event, context in
@@ -138,7 +99,7 @@ func entryPoint(passing args: consuming __CommandLineArguments_v0?, eventHandler
13899
/// - tests: The tests to list.
139100
///
140101
/// - Returns: An array of strings representing the IDs of `tests`.
141-
func listTestsForSwiftPM(_ tests: some Sequence<Test>) -> [String] {
102+
func listTestsForEntryPoint(_ tests: some Sequence<Test>) -> [String] {
142103
// Filter out hidden tests and test suites. Hidden tests should not generally
143104
// be presented to the user, and suites (XCTestCase classes) are not included
144105
// in the equivalent XCTest-based output.
@@ -291,7 +252,7 @@ func parseCommandLineArguments(from args: [String]) throws -> __CommandLineArgum
291252
///
292253
/// - Throws: If an argument is invalid, such as a malformed regular expression.
293254
@_spi(ForToolsIntegrationOnly)
294-
public func configurationForSwiftPMEntryPoint(from args: __CommandLineArguments_v0) throws -> Configuration {
255+
public func configurationForEntryPoint(from args: __CommandLineArguments_v0) throws -> Configuration {
295256
var configuration = Configuration()
296257

297258
// Parallelization (on by default)
@@ -372,7 +333,7 @@ public func configurationForSwiftPMEntryPoint(from args: __CommandLineArguments_
372333

373334
#if !SWT_NO_EXIT_TESTS
374335
// Enable exit test handling via __swiftPMEntryPoint().
375-
configuration.exitTestHandler = ExitTest.handlerForSwiftPM(forXCTestCaseIdentifiedBy: args.xcTestCaseHostIdentifier)
336+
configuration.exitTestHandler = ExitTest.handlerForEntryPoint(forXCTestCaseIdentifiedBy: args.xcTestCaseHostIdentifier)
376337
#endif
377338

378339
return configuration
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
private import TestingInternals
12+
13+
/// The entry point to the testing library used by Swift Package Manager.
14+
///
15+
/// - Parameters:
16+
/// - args: A previously-parsed command-line arguments structure to interpret.
17+
/// If `nil`, a new instance is created from the command-line arguments to
18+
/// the current process.
19+
///
20+
/// - Returns: The result of invoking the testing library. The type of this
21+
/// value is subject to change.
22+
///
23+
/// This function examines the command-line arguments represented by `args` and
24+
/// then invokes available tests in the current process.
25+
///
26+
/// - Warning: This function is used by Swift Package Manager. Do not call it
27+
/// directly.
28+
@_disfavoredOverload public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> CInt {
29+
await entryPoint(passing: args, eventHandler: nil)
30+
}
31+
32+
/// The entry point to the testing library used by Swift Package Manager.
33+
///
34+
/// - Parameters:
35+
/// - args: A previously-parsed command-line arguments structure to interpret.
36+
/// If `nil`, a new instance is created from the command-line arguments to
37+
/// the current process.
38+
///
39+
/// This function examines the command-line arguments to the current process
40+
/// and then invokes available tests in the current process. When the tests
41+
/// complete, the process is terminated. If tests were successful, an exit code
42+
/// of `EXIT_SUCCESS` is used; otherwise, a (possibly platform-specific) value
43+
/// such as `EXIT_FAILURE` is used instead.
44+
///
45+
/// - Warning: This function is used by Swift Package Manager. Do not call it
46+
/// directly.
47+
public func __swiftPMEntryPoint(passing args: __CommandLineArguments_v0? = nil) async -> Never {
48+
let exitCode: CInt = await __swiftPMEntryPoint(passing: args)
49+
exit(exitCode)
50+
}

Sources/Testing/ExitTests/ExitTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ extension ExitTest {
218218
/// This function should only be used when the process was started via the
219219
/// `__swiftPMEntryPoint()` function. The effect of using it under other
220220
/// configurations is undefined.
221-
static func findInEnvironmentForSwiftPM() -> Self? {
221+
static func findInEnvironmentForEntryPoint() -> Self? {
222222
if var sourceLocationString = Environment.variable(named: "SWT_EXPERIMENTAL_EXIT_TEST_SOURCE_LOCATION") {
223223
return try? sourceLocationString.withUTF8 { sourceLocationBuffer in
224224
let sourceLocationBuffer = UnsafeRawBufferPointer(sourceLocationBuffer)
@@ -238,7 +238,7 @@ extension ExitTest {
238238
///
239239
/// For a description of the inputs and outputs of this function, see the
240240
/// documentation for ``ExitTest/Handler``.
241-
static func handlerForSwiftPM(forXCTestCaseIdentifiedBy xcTestCaseIdentifier: String? = nil) -> Handler {
241+
static func handlerForEntryPoint(forXCTestCaseIdentifiedBy xcTestCaseIdentifier: String? = nil) -> Handler {
242242
// The environment could change between invocations if a test calls setenv()
243243
// or unsetenv(), so we need to recompute the child environment each time.
244244
// The executable and XCTest bundle paths should not change over time, so we

Tests/TestingTests/ExitTestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private import TestingInternals
7373
failed()
7474
}
7575
}
76-
configuration.exitTestHandler = ExitTest.handlerForSwiftPM()
76+
configuration.exitTestHandler = ExitTest.handlerForEntryPoint()
7777

7878
await runTest(for: FailingExitTests.self, configuration: configuration)
7979
}

Tests/TestingTests/SwiftPMTests.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
1212
private import TestingInternals
1313

14-
private func configurationForSwiftPMEntryPoint(withArguments args: [String]) throws -> Configuration {
14+
private func configurationForEntryPoint(withArguments args: [String]) throws -> Configuration {
1515
let args = try parseCommandLineArguments(from: args)
16-
return try configurationForSwiftPMEntryPoint(from: args)
16+
return try configurationForEntryPoint(from: args)
1717
}
1818

1919
@Suite("Swift Package Manager Integration Tests")
@@ -27,19 +27,19 @@ struct SwiftPMTests {
2727

2828
@Test("--parallel/--no-parallel argument")
2929
func parallel() throws {
30-
var configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH"])
30+
var configuration = try configurationForEntryPoint(withArguments: ["PATH"])
3131
#expect(configuration.isParallelizationEnabled)
3232

33-
configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--parallel"])
33+
configuration = try configurationForEntryPoint(withArguments: ["PATH", "--parallel"])
3434
#expect(configuration.isParallelizationEnabled)
3535

36-
configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--no-parallel"])
36+
configuration = try configurationForEntryPoint(withArguments: ["PATH", "--no-parallel"])
3737
#expect(!configuration.isParallelizationEnabled)
3838
}
3939

4040
@Test("No --filter or --skip argument")
4141
func defaultFiltering() async throws {
42-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH"])
42+
let configuration = try configurationForEntryPoint(withArguments: ["PATH"])
4343
let test1 = Test(name: "hello") {}
4444
let test2 = Test(name: "goodbye") {}
4545
let plan = await Runner.Plan(tests: [test1, test2], configuration: configuration)
@@ -51,7 +51,7 @@ struct SwiftPMTests {
5151
@Test("--filter argument")
5252
@available(_regexAPI, *)
5353
func filter() async throws {
54-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "hello"])
54+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--filter", "hello"])
5555
let test1 = Test(name: "hello") {}
5656
let test2 = Test(name: "goodbye") {}
5757
let plan = await Runner.Plan(tests: [test1, test2], configuration: configuration)
@@ -63,7 +63,7 @@ struct SwiftPMTests {
6363
@Test("Multiple --filter arguments")
6464
@available(_regexAPI, *)
6565
func multipleFilter() async throws {
66-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "hello", "--filter", "sorry"])
66+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--filter", "hello", "--filter", "sorry"])
6767
let test1 = Test(name: "hello") {}
6868
let test2 = Test(name: "goodbye") {}
6969
let test3 = Test(name: "sorry") {}
@@ -77,17 +77,17 @@ struct SwiftPMTests {
7777
@Test("--filter or --skip argument with bad regex")
7878
func badArguments() throws {
7979
#expect(throws: (any Error).self) {
80-
_ = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "("])
80+
_ = try configurationForEntryPoint(withArguments: ["PATH", "--filter", "("])
8181
}
8282
#expect(throws: (any Error).self) {
83-
_ = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--skip", ")"])
83+
_ = try configurationForEntryPoint(withArguments: ["PATH", "--skip", ")"])
8484
}
8585
}
8686

8787
@Test("--skip argument")
8888
@available(_regexAPI, *)
8989
func skip() async throws {
90-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--skip", "hello"])
90+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--skip", "hello"])
9191
let test1 = Test(name: "hello") {}
9292
let test2 = Test(name: "goodbye") {}
9393
let plan = await Runner.Plan(tests: [test1, test2], configuration: configuration)
@@ -98,7 +98,7 @@ struct SwiftPMTests {
9898

9999
@Test(".hidden trait")
100100
func hidden() async throws {
101-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH"])
101+
let configuration = try configurationForEntryPoint(withArguments: ["PATH"])
102102
let test1 = Test(name: "hello") {}
103103
let test2 = Test(.hidden, name: "goodbye") {}
104104
let plan = await Runner.Plan(tests: [test1, test2], configuration: configuration)
@@ -110,7 +110,7 @@ struct SwiftPMTests {
110110
@Test("--filter/--skip arguments and .hidden trait")
111111
@available(_regexAPI, *)
112112
func filterAndSkipAndHidden() async throws {
113-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--filter", "hello", "--skip", "hello2"])
113+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--filter", "hello", "--skip", "hello2"])
114114
let test1 = Test(name: "hello") {}
115115
let test2 = Test(name: "hello2") {}
116116
let test3 = Test(.hidden, name: "hello") {}
@@ -128,7 +128,7 @@ struct SwiftPMTests {
128128
func xunitOutputWithBadPath() {
129129
// Test that a bad path produces an error.
130130
#expect(throws: CError.self) {
131-
_ = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--xunit-output", "/nonexistent/path/we/cannot/write/to"])
131+
_ = try configurationForEntryPoint(withArguments: ["PATH", "--xunit-output", "/nonexistent/path/we/cannot/write/to"])
132132
}
133133
}
134134

@@ -142,7 +142,7 @@ struct SwiftPMTests {
142142
_ = remove(temporaryFilePath)
143143
}
144144
do {
145-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--xunit-output", temporaryFilePath])
145+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--xunit-output", temporaryFilePath])
146146
let eventContext = Event.Context()
147147
configuration.eventHandler(Event(.runStarted, testID: nil, testCaseID: nil), eventContext)
148148
configuration.eventHandler(Event(.runEnded, testID: nil, testCaseID: nil), eventContext)
@@ -176,7 +176,7 @@ struct SwiftPMTests {
176176
_ = remove(temporaryFilePath)
177177
}
178178
do {
179-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--experimental-event-stream-output", temporaryFilePath])
179+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--experimental-event-stream-output", temporaryFilePath])
180180
let eventContext = Event.Context()
181181
configuration.handleEvent(Event(.runStarted, testID: nil, testCaseID: nil), in: eventContext)
182182
do {
@@ -197,23 +197,23 @@ struct SwiftPMTests {
197197
@Test("--repetitions argument (alone)")
198198
@available(_regexAPI, *)
199199
func repetitions() throws {
200-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--repetitions", "2468"])
200+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--repetitions", "2468"])
201201
#expect(configuration.repetitionPolicy.maximumIterationCount == 2468)
202202
#expect(configuration.repetitionPolicy.continuationCondition == nil)
203203
}
204204

205205
@Test("--repeat-until pass argument (alone)")
206206
@available(_regexAPI, *)
207207
func repeatUntilPass() throws {
208-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--repeat-until", "pass"])
208+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--repeat-until", "pass"])
209209
#expect(configuration.repetitionPolicy.maximumIterationCount == .max)
210210
#expect(configuration.repetitionPolicy.continuationCondition == .whileIssueRecorded)
211211
}
212212

213213
@Test("--repeat-until fail argument (alone)")
214214
@available(_regexAPI, *)
215215
func repeatUntilFail() throws {
216-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--repeat-until", "fail"])
216+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--repeat-until", "fail"])
217217
#expect(configuration.repetitionPolicy.maximumIterationCount == .max)
218218
#expect(configuration.repetitionPolicy.continuationCondition == .untilIssueRecorded)
219219
}
@@ -222,21 +222,21 @@ struct SwiftPMTests {
222222
@available(_regexAPI, *)
223223
func repeatUntilGarbage() {
224224
#expect(throws: (any Error).self) {
225-
_ = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--repeat-until", "qwertyuiop"])
225+
_ = try configurationForEntryPoint(withArguments: ["PATH", "--repeat-until", "qwertyuiop"])
226226
}
227227
}
228228

229229
@Test("--repetitions and --repeat-until arguments")
230230
@available(_regexAPI, *)
231231
func repetitionsAndRepeatUntil() throws {
232-
let configuration = try configurationForSwiftPMEntryPoint(withArguments: ["PATH", "--repetitions", "2468", "--repeat-until", "pass"])
232+
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--repetitions", "2468", "--repeat-until", "pass"])
233233
#expect(configuration.repetitionPolicy.maximumIterationCount == 2468)
234234
#expect(configuration.repetitionPolicy.continuationCondition == .whileIssueRecorded)
235235
}
236236

237237
@Test("list subcommand")
238238
func list() async throws {
239-
let testIDs = await listTestsForSwiftPM(Test.all)
239+
let testIDs = await listTestsForEntryPoint(Test.all)
240240
let currentTestID = try #require(
241241
Test.current
242242
.flatMap(\.id.parent)

0 commit comments

Comments
 (0)