Skip to content

Commit c53c72f

Browse files
authored
Adopt async/await in more tests (#7182)
### Motivation: Same as before, `async`/`await` good ### Modifications: Moved multiple test targets to `async`/`await`. ### Result: Only three uses of `temp_await` remain in test targets after this change.
1 parent 79b7069 commit c53c72f

File tree

12 files changed

+505
-480
lines changed

12 files changed

+505
-480
lines changed

Sources/SPMBuildCore/Plugins/PluginInvocation.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@ public enum PluginAction {
2424
}
2525

2626
extension PluginTarget {
27+
public func invoke(
28+
action: PluginAction,
29+
buildEnvironment: BuildEnvironment,
30+
scriptRunner: PluginScriptRunner,
31+
workingDirectory: AbsolutePath,
32+
outputDirectory: AbsolutePath,
33+
toolSearchDirectories: [AbsolutePath],
34+
accessibleTools: [String: (path: AbsolutePath, triples: [String]?)],
35+
writableDirectories: [AbsolutePath],
36+
readOnlyDirectories: [AbsolutePath],
37+
allowNetworkConnections: [SandboxNetworkPermission],
38+
pkgConfigDirectories: [AbsolutePath],
39+
sdkRootPath: AbsolutePath?,
40+
fileSystem: FileSystem,
41+
observabilityScope: ObservabilityScope,
42+
callbackQueue: DispatchQueue,
43+
delegate: PluginInvocationDelegate
44+
) async throws -> Bool {
45+
try await safe_async {
46+
self.invoke(
47+
action: action,
48+
buildEnvironment: buildEnvironment,
49+
scriptRunner: scriptRunner,
50+
workingDirectory: workingDirectory,
51+
outputDirectory: outputDirectory,
52+
toolSearchDirectories: toolSearchDirectories,
53+
accessibleTools: accessibleTools,
54+
writableDirectories: writableDirectories,
55+
readOnlyDirectories: readOnlyDirectories,
56+
allowNetworkConnections: allowNetworkConnections,
57+
pkgConfigDirectories: pkgConfigDirectories,
58+
sdkRootPath: sdkRootPath,
59+
fileSystem: fileSystem,
60+
observabilityScope: observabilityScope,
61+
callbackQueue: callbackQueue,
62+
delegate: delegate,
63+
completion: $0
64+
)
65+
}
66+
}
2767
/// Invokes the plugin by compiling its source code (if needed) and then running it as a subprocess. The specified
2868
/// plugin action determines which entry point is called in the subprocess, and the package and the tool mapping
2969
/// determine the context that is available to the plugin.
@@ -47,6 +87,7 @@ extension PluginTarget {
4787
/// - fileSystem: The file system to which all of the paths refers.
4888
///
4989
/// - Returns: A PluginInvocationResult that contains the results of invoking the plugin.
90+
@available(*, noasync, message: "Use the async alternative")
5091
public func invoke(
5192
action: PluginAction,
5293
buildEnvironment: BuildEnvironment,

Sources/SPMBuildCore/Plugins/PluginScriptRunner.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import PackageGraph
2020
public protocol PluginScriptRunner {
2121

2222
/// Public protocol function that starts compiling the plugin script to an executable. The name is used as the basename for the executable and auxiliary files. The tools version controls the availability of APIs in PackagePlugin, and should be set to the tools version of the package that defines the plugin (not of the target to which it is being applied). This function returns immediately and then calls the completion handler on the callback queue when compilation ends.
23+
@available(*, noasync, message: "Use the async alternative")
2324
func compilePluginScript(
2425
sourceFiles: [AbsolutePath],
2526
pluginName: String,
@@ -61,6 +62,29 @@ public protocol PluginScriptRunner {
6162
var hostTriple: Triple { get throws }
6263
}
6364

65+
public extension PluginScriptRunner {
66+
func compilePluginScript(
67+
sourceFiles: [AbsolutePath],
68+
pluginName: String,
69+
toolsVersion: ToolsVersion,
70+
observabilityScope: ObservabilityScope,
71+
callbackQueue: DispatchQueue,
72+
delegate: PluginScriptCompilerDelegate
73+
) async throws -> PluginCompilationResult {
74+
try await safe_async {
75+
self.compilePluginScript(
76+
sourceFiles: sourceFiles,
77+
pluginName: pluginName,
78+
toolsVersion: toolsVersion,
79+
observabilityScope: observabilityScope,
80+
callbackQueue: callbackQueue,
81+
delegate: delegate,
82+
completion: $0
83+
)
84+
}
85+
}
86+
}
87+
6488
/// Protocol by which `PluginScriptRunner` communicates back to the caller as it compiles plugins.
6589
public protocol PluginScriptCompilerDelegate {
6690
/// Called immediately before compiling a plugin. Will not be called if the plugin didn't have to be compiled. This call is always followed by a `didCompilePlugin()` but is mutually exclusive with a `skippedCompilingPlugin()` call.

Sources/SPMTestSupport/misc.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,23 @@ public func testWithTemporaryDirectory(
4848
)
4949
}
5050

51-
public func testWithTemporaryDirectory(
51+
@discardableResult
52+
public func testWithTemporaryDirectory<Result>(
5253
function: StaticString = #function,
53-
body: (AbsolutePath) async throws -> Void
54-
) async throws {
54+
body: (AbsolutePath) async throws -> Result
55+
) async throws -> Result {
5556
let cleanedFunction = function.description
5657
.replacingOccurrences(of: "(", with: "")
5758
.replacingOccurrences(of: ")", with: "")
5859
.replacingOccurrences(of: ".", with: "")
5960
.replacingOccurrences(of: ":", with: "_")
60-
try await withTemporaryDirectory(prefix: "spm-tests-\(cleanedFunction)") { tmpDirPath in
61+
return try await withTemporaryDirectory(prefix: "spm-tests-\(cleanedFunction)") { tmpDirPath in
6162
defer {
6263
// Unblock and remove the tmp dir on deinit.
6364
try? localFileSystem.chmod(.userWritable, path: tmpDirPath, options: [.recursive])
6465
try? localFileSystem.removeFileTree(tmpDirPath)
6566
}
66-
try await body(tmpDirPath)
67+
return try await body(tmpDirPath)
6768
}
6869
}
6970

Sources/SourceControl/RepositoryManager.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ public class RepositoryManager: Cancellable {
9292
self.concurrencySemaphore = DispatchSemaphore(value: maxConcurrentOperations)
9393
}
9494

95+
public func lookup(
96+
package: PackageIdentity,
97+
repository: RepositorySpecifier,
98+
updateStrategy: RepositoryUpdateStrategy,
99+
observabilityScope: ObservabilityScope,
100+
delegateQueue: DispatchQueue,
101+
callbackQueue: DispatchQueue
102+
) async throws -> RepositoryHandle {
103+
try await safe_async {
104+
self.lookup(
105+
package: package,
106+
repository: repository,
107+
updateStrategy: updateStrategy,
108+
observabilityScope: observabilityScope,
109+
delegateQueue: delegateQueue,
110+
callbackQueue: callbackQueue,
111+
completion: $0
112+
)
113+
}
114+
}
115+
95116
/// Get a handle to a repository.
96117
///
97118
/// This will initiate a clone of the repository automatically, if necessary.
@@ -107,6 +128,7 @@ public class RepositoryManager: Cancellable {
107128
/// - delegateQueue: Dispatch queue for delegate events
108129
/// - callbackQueue: Dispatch queue for callbacks
109130
/// - completion: The completion block that should be called after lookup finishes.
131+
@available(*, noasync, message: "Use the async alternative")
110132
public func lookup(
111133
package: PackageIdentity,
112134
repository: RepositorySpecifier,

Tests/CommandsTests/PackageRegistryToolTests.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
643643

644644
// Validate signatures
645645
var verifierConfiguration = VerifierConfiguration()
646-
verifierConfiguration.trustedRoots = try temp_await { self.testRoots(callback: $0) }
646+
verifierConfiguration.trustedRoots = try testRoots()
647647

648648
// archive signature
649649
let archivePath = workingDirectory.appending("\(packageIdentity)-\(version).zip")
@@ -753,7 +753,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
753753

754754
// Validate signatures
755755
var verifierConfiguration = VerifierConfiguration()
756-
verifierConfiguration.trustedRoots = try temp_await { self.testRoots(callback: $0) }
756+
verifierConfiguration.trustedRoots = try testRoots()
757757

758758
// archive signature
759759
let archivePath = workingDirectory.appending("\(packageIdentity)-\(version).zip")
@@ -860,7 +860,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
860860

861861
// Validate signatures
862862
var verifierConfiguration = VerifierConfiguration()
863-
verifierConfiguration.trustedRoots = try temp_await { self.testRoots(callback: $0) }
863+
verifierConfiguration.trustedRoots = try testRoots()
864864

865865
// archive signature
866866
let archivePath = workingDirectory.appending("\(packageIdentity)-\(version).zip")
@@ -920,15 +920,11 @@ final class PackageRegistryToolTests: CommandsTestCase {
920920
XCTAssertEqual(try SwiftPackageRegistryTool.Login.loginURL(from: registryURL, loginAPIPath: "/secret-sign-in").absoluteString, "https://packages.example.com:8081/secret-sign-in")
921921
}
922922

923-
private func testRoots(callback: (Result<[[UInt8]], Error>) -> Void) {
924-
do {
925-
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
926-
let rootCA = try localFileSystem
927-
.readFileContents(fixturePath.appending(components: "Certificates", "TestRootCA.cer")).contents
928-
callback(.success([rootCA]))
929-
}
930-
} catch {
931-
callback(.failure(error))
923+
private func testRoots() throws -> [[UInt8]] {
924+
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
925+
let rootCA = try localFileSystem
926+
.readFileContents(fixturePath.appending(components: "Certificates", "TestRootCA.cer")).contents
927+
return [rootCA]
932928
}
933929
}
934930

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,11 +2903,11 @@ final class PackageToolTests: CommandsTestCase {
29032903
}
29042904
}
29052905

2906-
func testSinglePluginTarget() throws {
2906+
func testSinglePluginTarget() async throws {
29072907
// Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
29082908
try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")
29092909

2910-
try testWithTemporaryDirectory { tmpPath in
2910+
try await testWithTemporaryDirectory { tmpPath in
29112911
// Create a sample package with a library target and a plugin.
29122912
let packageDir = tmpPath.appending(components: "MyPackage")
29132913
try localFileSystem.createDirectory(packageDir, recursive: true)
@@ -2956,13 +2956,10 @@ final class PackageToolTests: CommandsTestCase {
29562956

29572957
// Load the root manifest.
29582958
let rootInput = PackageGraphRootInput(packages: [packageDir], dependencies: [])
2959-
let rootManifests = try temp_await {
2960-
workspace.loadRootManifests(
2961-
packages: rootInput.packages,
2962-
observabilityScope: observability.topScope,
2963-
completion: $0
2964-
)
2965-
}
2959+
let rootManifests = try await workspace.loadRootManifests(
2960+
packages: rootInput.packages,
2961+
observabilityScope: observability.topScope
2962+
)
29662963
XCTAssert(rootManifests.count == 1, "\(rootManifests)")
29672964

29682965
// Load the package graph.

0 commit comments

Comments
 (0)