Skip to content

Commit 53e8515

Browse files
committed
Move remaining tests to to async/await
Tests moved: CommandsTests FunctionalTests SourceControlTests SPMBuildCoreTests PackageFingerprintTests PackageLoadingTests
1 parent 380b953 commit 53e8515

File tree

12 files changed

+516
-491
lines changed

12 files changed

+516
-491
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: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
279279

280280
// TODO: Test example with login and password
281281

282-
func testArchiving() throws {
282+
func testArchiving() async throws {
283283
#if os(Linux)
284284
// needed for archiving
285285
guard SPM_posix_spawn_file_actions_addchdir_np_supported() else {
@@ -293,7 +293,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
293293
let metadataFilename = SwiftPackageRegistryTool.Publish.metadataFilename
294294

295295
// git repo
296-
try withTemporaryDirectory { temporaryDirectory in
296+
try await withTemporaryDirectory { temporaryDirectory in
297297
let packageDirectory = temporaryDirectory.appending("MyPackage")
298298
try localFileSystem.createDirectory(packageDirectory)
299299

@@ -320,12 +320,12 @@ final class PackageRegistryToolTests: CommandsTestCase {
320320
observabilityScope: observability.topScope
321321
)
322322

323-
try validatePackageArchive(at: archivePath)
323+
try await validatePackageArchive(at: archivePath)
324324
XCTAssertTrue(archivePath.isDescendant(of: workingDirectory))
325325
}
326326

327327
// not a git repo
328-
try withTemporaryDirectory { temporaryDirectory in
328+
try await withTemporaryDirectory { temporaryDirectory in
329329
let packageDirectory = temporaryDirectory.appending("MyPackage")
330330
try localFileSystem.createDirectory(packageDirectory)
331331

@@ -350,11 +350,11 @@ final class PackageRegistryToolTests: CommandsTestCase {
350350
observabilityScope: observability.topScope
351351
)
352352

353-
try validatePackageArchive(at: archivePath)
353+
try await validatePackageArchive(at: archivePath)
354354
}
355355

356356
// canonical metadata location
357-
try withTemporaryDirectory { temporaryDirectory in
357+
try await withTemporaryDirectory { temporaryDirectory in
358358
let packageDirectory = temporaryDirectory.appending("MyPackage")
359359
try localFileSystem.createDirectory(packageDirectory)
360360

@@ -385,17 +385,17 @@ final class PackageRegistryToolTests: CommandsTestCase {
385385
observabilityScope: observability.topScope
386386
)
387387

388-
let extractedPath = try validatePackageArchive(at: archivePath)
388+
let extractedPath = try await validatePackageArchive(at: archivePath)
389389
XCTAssertFileExists(extractedPath.appending(component: metadataFilename))
390390
}
391391

392392
@discardableResult
393-
func validatePackageArchive(at archivePath: AbsolutePath) throws -> AbsolutePath {
393+
func validatePackageArchive(at archivePath: AbsolutePath) async throws -> AbsolutePath {
394394
XCTAssertFileExists(archivePath)
395395
let archiver = ZipArchiver(fileSystem: localFileSystem)
396396
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
397397
try localFileSystem.createDirectory(extractPath)
398-
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
398+
try await archiver.extract(from: archivePath, to: extractPath)
399399
try localFileSystem.stripFirstLevel(of: extractPath)
400400
XCTAssertFileExists(extractPath.appending("Package.swift"))
401401
return extractPath
@@ -550,7 +550,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
550550
let archiver = ZipArchiver(fileSystem: localFileSystem)
551551
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
552552
try localFileSystem.createDirectory(extractPath)
553-
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
553+
try await archiver.extract(from: archivePath, to: extractPath)
554554
try localFileSystem.stripFirstLevel(of: extractPath)
555555

556556
let manifestInArchive = try localFileSystem.readFileContents(extractPath.appending(manifestFile)).contents
@@ -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

@@ -963,7 +959,7 @@ final class PackageRegistryToolTests: CommandsTestCase {
963959
let archiver = ZipArchiver(fileSystem: localFileSystem)
964960
let extractPath = archivePath.parentDirectory.appending(component: UUID().uuidString)
965961
try localFileSystem.createDirectory(extractPath)
966-
try temp_await { archiver.extract(from: archivePath, to: extractPath, completion: $0) }
962+
try await archiver.extract(from: archivePath, to: extractPath)
967963
try localFileSystem.stripFirstLevel(of: extractPath)
968964

969965
let manifestSignature = try ManifestSignatureParser.parse(

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)