Skip to content

Commit 24929f3

Browse files
Remove usage of temp_await from WorkspaceTests (#7124)
Replace all usage of `temp_await` with `await` in `WorkspaceTests` ### Motivation: async/await is nicer than callbacks. I want to see it more broadly adopted in SwiftPM. ### Modifications: All usages of `temp_await` in the WorkspaceTests target have removed. Where necessary, `async` alternatives have been added and the completion handler methods have been marked no-async. ### Result: `async`/`await` adoption in `WorkspaceTests`. --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent a4b4fe0 commit 24929f3

File tree

7 files changed

+199
-157
lines changed

7 files changed

+199
-157
lines changed

Sources/PackageGraph/PackageContainer.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ extension PackageContainerConstraint: CustomStringConvertible {
164164
/// An interface for resolving package containers.
165165
public protocol PackageContainerProvider {
166166
/// Get the container for a particular identifier asynchronously.
167+
168+
@available(*, noasync, message: "Use the async alternative")
167169
func getContainer(
168170
for package: PackageReference,
169171
updateStrategy: ContainerUpdateStrategy,
@@ -173,6 +175,24 @@ public protocol PackageContainerProvider {
173175
)
174176
}
175177

178+
public extension PackageContainerProvider {
179+
func getContainer(
180+
for package: PackageReference,
181+
updateStrategy: ContainerUpdateStrategy,
182+
observabilityScope: ObservabilityScope,
183+
on queue: DispatchQueue
184+
) async throws -> PackageContainer {
185+
try await safe_async {
186+
self.getContainer(
187+
for: package,
188+
updateStrategy: updateStrategy,
189+
observabilityScope: observabilityScope,
190+
on: queue,
191+
completion: $0)
192+
}
193+
}
194+
}
195+
176196
/// Only used for source control containers and as such a mirror of RepositoryUpdateStrategy
177197
/// This duplication is unfortunate - ideally this is not a concern of the ContainerProvider at all
178198
/// but it is required give how PackageContainerProvider currently integrated into the resolver

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,41 @@ public final class ManifestLoader: ManifestLoaderProtocol {
293293
self.evaluationQueue.maxConcurrentOperationCount = Concurrency.maxOperations
294294
self.concurrencySemaphore = DispatchSemaphore(value: Concurrency.maxOperations)
295295
}
296-
296+
297+
public func load(
298+
manifestPath: AbsolutePath,
299+
manifestToolsVersion: ToolsVersion,
300+
packageIdentity: PackageIdentity,
301+
packageKind: PackageReference.Kind,
302+
packageLocation: String,
303+
packageVersion: (version: Version?, revision: String?)?,
304+
identityResolver: IdentityResolver,
305+
dependencyMapper: DependencyMapper,
306+
fileSystem: FileSystem,
307+
observabilityScope: ObservabilityScope,
308+
delegateQueue: DispatchQueue,
309+
callbackQueue: DispatchQueue
310+
) async throws -> Manifest {
311+
try await safe_async {
312+
self.load(
313+
manifestPath: manifestPath,
314+
manifestToolsVersion: manifestToolsVersion,
315+
packageIdentity: packageIdentity,
316+
packageKind: packageKind,
317+
packageLocation: packageLocation,
318+
packageVersion: packageVersion,
319+
identityResolver: identityResolver,
320+
dependencyMapper: dependencyMapper,
321+
fileSystem: fileSystem,
322+
observabilityScope: observabilityScope,
323+
delegateQueue: delegateQueue,
324+
callbackQueue: callbackQueue,
325+
completion: $0
326+
)
327+
}
328+
}
329+
330+
@available(*, noasync, message: "Use the async alternative")
297331
public func load(
298332
manifestPath: AbsolutePath,
299333
manifestToolsVersion: ToolsVersion,

Sources/Workspace/Workspace.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ extension Workspace {
950950
}
951951

952952
/// Loads and returns manifests at the given paths.
953+
@available(*, noasync, message: "Use the async alternative")
953954
public func loadRootManifests(
954955
packages: [AbsolutePath],
955956
observabilityScope: ObservabilityScope,
@@ -1123,9 +1124,20 @@ extension Workspace {
11231124
}
11241125
return importList
11251126
}
1127+
1128+
public func loadPackage(
1129+
with identity: PackageIdentity,
1130+
packageGraph: PackageGraph,
1131+
observabilityScope: ObservabilityScope
1132+
) async throws -> Package {
1133+
try await safe_async {
1134+
self.loadPackage(with: identity, packageGraph: packageGraph, observabilityScope: observabilityScope, completion: $0)
1135+
}
1136+
}
11261137

11271138
/// Loads a single package in the context of a previously loaded graph. This can be useful for incremental loading
11281139
/// in a longer-lived program, like an IDE.
1140+
@available(*, noasync, message: "Use the async alternative")
11291141
public func loadPackage(
11301142
with identity: PackageIdentity,
11311143
packageGraph: PackageGraph,

Tests/WorkspaceTests/ManifestSourceGenerationTests.swift

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class ManifestSourceGenerationTests: XCTestCase {
4343
toolsVersionHeaderComment: String? = .none,
4444
additionalImportModuleNames: [String] = [],
4545
fs: FileSystem = localFileSystem
46-
) throws -> String {
47-
try withTemporaryDirectory { packageDir in
46+
) async throws -> String {
47+
try await withTemporaryDirectory { packageDir in
4848
let observability = ObservabilitySystem.makeForTesting()
4949

5050
// Write the original manifest file contents, and load it.
@@ -53,23 +53,20 @@ class ManifestSourceGenerationTests: XCTestCase {
5353
let manifestLoader = ManifestLoader(toolchain: try UserToolchain.default)
5454
let identityResolver = DefaultIdentityResolver()
5555
let dependencyMapper = DefaultDependencyMapper(identityResolver: identityResolver)
56-
let manifest = try temp_await {
57-
manifestLoader.load(
58-
manifestPath: manifestPath,
59-
manifestToolsVersion: toolsVersion,
60-
packageIdentity: .plain("Root"),
61-
packageKind: .root(packageDir),
62-
packageLocation: packageDir.pathString,
63-
packageVersion: nil,
64-
identityResolver: identityResolver,
65-
dependencyMapper: dependencyMapper,
66-
fileSystem: fs,
67-
observabilityScope: observability.topScope,
68-
delegateQueue: .sharedConcurrent,
69-
callbackQueue: .sharedConcurrent,
70-
completion: $0
71-
)
72-
}
56+
let manifest = try await manifestLoader.load(
57+
manifestPath: manifestPath,
58+
manifestToolsVersion: toolsVersion,
59+
packageIdentity: .plain("Root"),
60+
packageKind: .root(packageDir),
61+
packageLocation: packageDir.pathString,
62+
packageVersion: nil,
63+
identityResolver: identityResolver,
64+
dependencyMapper: dependencyMapper,
65+
fileSystem: fs,
66+
observabilityScope: observability.topScope,
67+
delegateQueue: .sharedConcurrent,
68+
callbackQueue: .sharedConcurrent
69+
)
7370

7471
XCTAssertNoDiagnostics(observability.diagnostics)
7572

@@ -85,23 +82,20 @@ class ManifestSourceGenerationTests: XCTestCase {
8582

8683
// Write out the generated manifest to replace the old manifest file contents, and load it again.
8784
try fs.writeFileContents(manifestPath, string: newContents)
88-
let newManifest = try temp_await {
89-
manifestLoader.load(
90-
manifestPath: manifestPath,
91-
manifestToolsVersion: toolsVersion,
92-
packageIdentity: .plain("Root"),
93-
packageKind: .root(packageDir),
94-
packageLocation: packageDir.pathString,
95-
packageVersion: nil,
96-
identityResolver: identityResolver,
97-
dependencyMapper: dependencyMapper,
98-
fileSystem: fs,
99-
observabilityScope: observability.topScope,
100-
delegateQueue: .sharedConcurrent,
101-
callbackQueue: .sharedConcurrent,
102-
completion: $0
103-
)
104-
}
85+
let newManifest = try await manifestLoader.load(
86+
manifestPath: manifestPath,
87+
manifestToolsVersion: toolsVersion,
88+
packageIdentity: .plain("Root"),
89+
packageKind: .root(packageDir),
90+
packageLocation: packageDir.pathString,
91+
packageVersion: nil,
92+
identityResolver: identityResolver,
93+
dependencyMapper: dependencyMapper,
94+
fileSystem: fs,
95+
observabilityScope: observability.topScope,
96+
delegateQueue: .sharedConcurrent,
97+
callbackQueue: .sharedConcurrent
98+
)
10599

106100
XCTAssertNoDiagnostics(observability.diagnostics)
107101

@@ -125,7 +119,7 @@ class ManifestSourceGenerationTests: XCTestCase {
125119
}
126120
}
127121

128-
func testBasics() throws {
122+
func testBasics() async throws {
129123
let manifestContents = """
130124
// swift-tools-version:5.3
131125
// The swift-tools-version declares the minimum version of Swift required to build this package.
@@ -160,10 +154,10 @@ class ManifestSourceGenerationTests: XCTestCase {
160154
]
161155
)
162156
"""
163-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
157+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
164158
}
165159

166-
func testCustomPlatform() throws {
160+
func testCustomPlatform() async throws {
167161
let manifestContents = """
168162
// swift-tools-version:5.6
169163
// The swift-tools-version declares the minimum version of Swift required to build this package.
@@ -197,10 +191,10 @@ class ManifestSourceGenerationTests: XCTestCase {
197191
]
198192
)
199193
"""
200-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_6)
194+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_6)
201195
}
202196

203-
func testAdvancedFeatures() throws {
197+
func testAdvancedFeatures() async throws {
204198
let manifestContents = """
205199
// swift-tools-version:5.3
206200
// The swift-tools-version declares the minimum version of Swift required to build this package.
@@ -246,10 +240,10 @@ class ManifestSourceGenerationTests: XCTestCase {
246240
cxxLanguageStandard: .cxx11
247241
)
248242
"""
249-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
243+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
250244
}
251245

252-
func testPackageDependencyVariations() throws {
246+
func testPackageDependencyVariations() async throws {
253247
let manifestContents = """
254248
// swift-tools-version:5.4
255249
import PackageDescription
@@ -281,15 +275,15 @@ class ManifestSourceGenerationTests: XCTestCase {
281275
]
282276
)
283277
"""
284-
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
278+
let newContents = try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
285279

286280
// Check some things about the contents of the manifest.
287281
XCTAssertTrue(newContents.contains("url: \"\("../MyPkg10".nativePathString(escaped: true))\""), newContents)
288282
XCTAssertTrue(newContents.contains("path: \"\("../MyPkg11".nativePathString(escaped: true))\""), newContents)
289283
XCTAssertTrue(newContents.contains("path: \"\("packages/path/to/MyPkg12".nativePathString(escaped: true))"), newContents)
290284
}
291285

292-
func testResources() throws {
286+
func testResources() async throws {
293287
let manifestContents = """
294288
// swift-tools-version:5.3
295289
import PackageDescription
@@ -320,10 +314,10 @@ class ManifestSourceGenerationTests: XCTestCase {
320314
]
321315
)
322316
"""
323-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
317+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
324318
}
325319

326-
func testBuildSettings() throws {
320+
func testBuildSettings() async throws {
327321
let manifestContents = """
328322
// swift-tools-version:5.3
329323
import PackageDescription
@@ -356,10 +350,10 @@ class ManifestSourceGenerationTests: XCTestCase {
356350
]
357351
)
358352
"""
359-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
353+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
360354
}
361355

362-
func testPluginTargets() throws {
356+
func testPluginTargets() async throws {
363357
let manifestContents = """
364358
// swift-tools-version:5.5
365359
import PackageDescription
@@ -378,10 +372,10 @@ class ManifestSourceGenerationTests: XCTestCase {
378372
]
379373
)
380374
"""
381-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5)
375+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5)
382376
}
383377

384-
func testCustomToolsVersionHeaderComment() throws {
378+
func testCustomToolsVersionHeaderComment() async throws {
385379
let manifestContents = """
386380
// swift-tools-version:5.5
387381
import PackageDescription
@@ -400,12 +394,12 @@ class ManifestSourceGenerationTests: XCTestCase {
400394
]
401395
)
402396
"""
403-
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, toolsVersionHeaderComment: "a comment")
397+
let newContents = try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, toolsVersionHeaderComment: "a comment")
404398

405399
XCTAssertTrue(newContents.hasPrefix("// swift-tools-version: 5.5; a comment\n"), "contents: \(newContents)")
406400
}
407401

408-
func testAdditionalModuleImports() throws {
402+
func testAdditionalModuleImports() async throws {
409403
let manifestContents = """
410404
// swift-tools-version:5.5
411405
import PackageDescription
@@ -420,12 +414,12 @@ class ManifestSourceGenerationTests: XCTestCase {
420414
]
421415
)
422416
"""
423-
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, additionalImportModuleNames: ["Foundation"])
417+
let newContents = try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_5, additionalImportModuleNames: ["Foundation"])
424418

425419
XCTAssertTrue(newContents.contains("import Foundation\n"), "contents: \(newContents)")
426420
}
427421

428-
func testLatestPlatformVersions() throws {
422+
func testLatestPlatformVersions() async throws {
429423
let manifestContents = """
430424
// swift-tools-version: 5.9
431425
// The swift-tools-version declares the minimum version of Swift required to build this package.
@@ -447,10 +441,10 @@ class ManifestSourceGenerationTests: XCTestCase {
447441
]
448442
)
449443
"""
450-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
444+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
451445
}
452446

453-
func testTargetPlatformConditions() throws {
447+
func testTargetPlatformConditions() async throws {
454448
let manifestContents = """
455449
// swift-tools-version: 5.9
456450
// The swift-tools-version declares the minimum version of Swift required to build this package.
@@ -475,7 +469,7 @@ class ManifestSourceGenerationTests: XCTestCase {
475469
]
476470
)
477471
"""
478-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
472+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_9)
479473
}
480474

481475
func testCustomProductSourceGeneration() throws {
@@ -517,7 +511,7 @@ class ManifestSourceGenerationTests: XCTestCase {
517511
XCTAssertTrue(contents.contains(".library(name: \"Foo\", targets: [\"Bar\"], type: .static)"), "contents: \(contents)")
518512
}
519513

520-
func testModuleAliasGeneration() throws {
514+
func testModuleAliasGeneration() async throws {
521515
let manifest = Manifest.createRootManifest(
522516
displayName: "thisPkg",
523517
path: "/thisPkg",
@@ -558,10 +552,10 @@ class ManifestSourceGenerationTests: XCTestCase {
558552
let isContained = trimmedParts.allSatisfy(trimmedContents.contains(_:))
559553
XCTAssertTrue(isContained)
560554

561-
try testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_8)
555+
try await testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_8)
562556
}
563557

564-
func testUpcomingAndExperimentalFeatures() throws {
558+
func testUpcomingAndExperimentalFeatures() async throws {
565559
let manifestContents = """
566560
// swift-tools-version:5.8
567561
import PackageDescription
@@ -580,10 +574,10 @@ class ManifestSourceGenerationTests: XCTestCase {
580574
]
581575
)
582576
"""
583-
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_8)
577+
try await testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_8)
584578
}
585579

586-
func testPluginNetworkingPermissionGeneration() throws {
580+
func testPluginNetworkingPermissionGeneration() async throws {
587581
let manifest = Manifest.createRootManifest(
588582
displayName: "thisPkg",
589583
path: "/thisPkg",
@@ -593,6 +587,6 @@ class ManifestSourceGenerationTests: XCTestCase {
593587
try TargetDescription(name: "MyPlugin", type: .plugin, pluginCapability: .command(intent: .custom(verb: "foo", description: "bar"), permissions: [.allowNetworkConnections(scope: .all(ports: [23, 42, 443, 8080]), reason: "internet good")]))
594588
])
595589
let contents = try manifest.generateManifestFileContents(packageDirectory: manifest.path.parentDirectory)
596-
try testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_9)
590+
try await testManifestWritingRoundTrip(manifestContents: contents, toolsVersion: .v5_9)
597591
}
598592
}

0 commit comments

Comments
 (0)