Skip to content

Commit e9601b4

Browse files
committed
Adopt swift-subprocess when running unit tests
1 parent 46df923 commit e9601b4

File tree

9 files changed

+145
-115
lines changed

9 files changed

+145
-115
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ if(FIND_PM_DEPS)
4747
find_package(SwiftCertificates CONFIG REQUIRED)
4848
find_package(SwiftCrypto CONFIG REQUIRED)
4949
find_package(SwiftBuild CONFIG REQUIRED)
50+
find_package(SwiftSubprocess CONFIG REQUIRED)
5051
endif()
5152

5253
find_package(dispatch QUIET)

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ let package = Package(
239239
dependencies: [
240240
"_AsyncFileSystem",
241241
.target(name: "SPMSQLite3", condition: .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .visionOS, .macCatalyst, .linux, .openbsd, .custom("freebsd")])),
242+
.product(name: "Subprocess", package: "swift-subprocess"),
242243
.product(name: "SwiftToolchainCSQLite", package: "swift-toolchain-sqlite", condition: .when(platforms: [.windows, .android])),
243244
.product(name: "DequeModule", package: "swift-collections"),
244245
.product(name: "OrderedCollections", package: "swift-collections"),
@@ -575,6 +576,7 @@ let package = Package(
575576
dependencies: [
576577
.product(name: "ArgumentParser", package: "swift-argument-parser"),
577578
.product(name: "OrderedCollections", package: "swift-collections"),
579+
.product(name: "Subprocess", package: "swift-subprocess"),
578580
"Basics",
579581
"BinarySymbols",
580582
"Build",
@@ -1113,6 +1115,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
11131115
.package(url: "https://github.com/apple/swift-system.git", revision: "1.5.0"),
11141116
.package(url: "https://github.com/apple/swift-collections.git", revision: "1.1.6"),
11151117
.package(url: "https://github.com/apple/swift-certificates.git", revision: "1.10.1"),
1118+
.package(url: "https://github.com/swiftlang/swift-subprocess.git", .upToNextMinor(from: "0.2.0")),
11161119
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite.git", revision: "1.0.7"),
11171120
// Not in toolchain, used for use in previewing documentation
11181121
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
@@ -1131,6 +1134,7 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
11311134
.package(path: "../swift-system"),
11321135
.package(path: "../swift-collections"),
11331136
.package(path: "../swift-certificates"),
1137+
.package(path: "../swift-subprocess"),
11341138
.package(path: "../swift-toolchain-sqlite"),
11351139
]
11361140
if !swiftDriverDeps.isEmpty {

Sources/Basics/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ add_library(Basics
7373
Serialization/SerializedJSON.swift
7474
SwiftVersion.swift
7575
SQLiteBackedCache.swift
76+
Subprocess+Extensions.swift
7677
TestingLibrary.swift
7778
Triple+Basics.swift
7879
URL.swift
@@ -83,6 +84,7 @@ add_library(Basics
8384
target_link_libraries(Basics PUBLIC
8485
_AsyncFileSystem
8586
SwiftCollections::OrderedCollections
87+
SwiftSubprocess::Subprocess
8688
TSCBasic
8789
TSCUtility)
8890
target_link_libraries(Basics PRIVATE

Sources/Basics/Cancellator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ public final class Cancellator: Cancellable, Sendable {
190190
}
191191
}
192192

193+
extension Cancellator {
194+
public func run<T: Sendable>(name: String, _ block: @escaping @Sendable () async throws -> T) async throws -> T {
195+
let task = Task { try await block() }
196+
let token = register(name: name, handler: { _ in task.cancel() })
197+
defer {
198+
token.map { deregister($0) }
199+
}
200+
return try await task.value
201+
}
202+
}
203+
193204
public protocol Cancellable {
194205
func cancel(deadline: DispatchTime) throws -> Void
195206
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Subprocess
14+
import TSCBasic
15+
16+
#if canImport(System)
17+
import System
18+
#else
19+
import SystemPackage
20+
#endif
21+
22+
extension Subprocess.Environment {
23+
public init(_ env: Basics.Environment) {
24+
var newEnv: [Subprocess.Environment.Key: String] = [:]
25+
for (key, value) in env {
26+
newEnv[.init(rawValue: key.rawValue)!] = value
27+
}
28+
self = Subprocess.Environment.custom(newEnv)
29+
}
30+
}
31+
32+
extension Subprocess.Configuration {
33+
public init(commandLine: [String], environment: Subprocess.Environment) throws {
34+
guard let arg0 = commandLine.first else {
35+
throw StringError("command line was unexpectedly empty")
36+
}
37+
self.init(.path(FilePath(arg0)), arguments: .init(Array(commandLine.dropFirst())), environment: environment)
38+
}
39+
}

Sources/Commands/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_library(Commands
6161
target_link_libraries(Commands PUBLIC
6262
SwiftCollections::OrderedCollections
6363
SwiftSyntax::SwiftRefactor
64+
SwiftSubprocess::Subprocess
6465
ArgumentParser
6566
Basics
6667
BinarySymbols

0 commit comments

Comments
 (0)