Skip to content

Commit bd30c05

Browse files
authored
Model remaining commands (#339)
* Model the tar command * Model swift commands * Model make, strip, and sha256sum * Model productbuild * Model gpg * Model pkgutil * Model installer * Fix file extension for toolchain tmpfile for macOS installer * Add test cases for each modeled command
1 parent 9f9c33a commit bd30c05

File tree

8 files changed

+1252
-115
lines changed

8 files changed

+1252
-115
lines changed

Sources/LinuxPlatform/Linux.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,11 @@ public struct Linux: Platform {
275275
try await fs.withTemporary(files: tmpFile) {
276276
try await ctx.httpClient.getGpgKeys().download(to: tmpFile)
277277
if let mockedHomeDir = ctx.mockedHomeDir {
278-
try self.runProgram(
279-
"gpg", "--import", "\(tmpFile)", quiet: true,
280-
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
281-
)
278+
var env = ProcessInfo.processInfo.environment
279+
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
280+
try await sys.gpg()._import(keys: tmpFile).run(self, env: env, quiet: true)
282281
} else {
283-
try self.runProgram("gpg", "--import", "\(tmpFile)", quiet: true)
282+
try await sys.gpg()._import(keys: tmpFile).run(self, quiet: true)
284283
}
285284
}
286285
}
@@ -417,12 +416,11 @@ public struct Linux: Platform {
417416
await ctx.print("Verifying toolchain signature...")
418417
do {
419418
if let mockedHomeDir = ctx.mockedHomeDir {
420-
try self.runProgram(
421-
"gpg", "--verify", "\(sigFile)", "\(archive)", quiet: false,
422-
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
423-
)
419+
var env = ProcessInfo.processInfo.environment
420+
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
421+
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, env: env, quiet: false)
424422
} else {
425-
try self.runProgram("gpg", "--verify", "\(sigFile)", "\(archive)", quiet: !verbose)
423+
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, quiet: !verbose)
426424
}
427425
} catch {
428426
throw SwiftlyError(message: "Signature verification failed: \(error).")
@@ -447,12 +445,11 @@ public struct Linux: Platform {
447445
await ctx.print("Verifying swiftly signature...")
448446
do {
449447
if let mockedHomeDir = ctx.mockedHomeDir {
450-
try self.runProgram(
451-
"gpg", "--verify", "\(sigFile)", "\(archive)", quiet: false,
452-
env: ["GNUPGHOME": (mockedHomeDir / ".gnupg").string]
453-
)
448+
var env = ProcessInfo.processInfo.environment
449+
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
450+
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, env: env, quiet: false)
454451
} else {
455-
try self.runProgram("gpg", "--verify", "\(sigFile)", "\(archive)", quiet: !verbose)
452+
try await sys.gpg().verify(detachedSignature: sigFile, signedData: archive).run(self, quiet: !verbose)
456453
}
457454
} catch {
458455
throw SwiftlyError(message: "Signature verification failed: \(error).")

Sources/MacOSPlatform/MacOS.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ public struct MacOS: Platform {
7070
if toolchainsDir == self.defaultToolchainsDirectory {
7171
// If the toolchains go into the default user location then we use the installer to install them
7272
await ctx.print("Installing package in user home directory...")
73-
try runProgram(
74-
"installer", "-verbose", "-pkg", "\(tmpFile)", "-target", "CurrentUserHomeDirectory",
75-
quiet: !verbose
76-
)
73+
74+
try await sys.installer(.verbose, pkg: tmpFile, target: "CurrentUserHomeDirectory").run(self, quiet: !verbose)
7775
} else {
7876
// Otherwise, we extract the pkg into the requested toolchains directory.
7977
await ctx.print("Expanding pkg...")
@@ -86,7 +84,7 @@ public struct MacOS: Platform {
8684

8785
await ctx.print("Checking package signature...")
8886
do {
89-
try runProgram("pkgutil", "--check-signature", "\(tmpFile)", quiet: !verbose)
87+
try await sys.pkgutil().checkSignature(pkgPath: tmpFile).run(self, quiet: !verbose)
9088
} catch {
9189
// If this is not a test that uses mocked toolchains then we must throw this error and abort installation
9290
guard ctx.mockedHomeDir != nil else {
@@ -96,7 +94,7 @@ public struct MacOS: Platform {
9694
// We permit the signature verification to fail during testing
9795
await ctx.print("Signature verification failed, which is allowable during testing with mocked toolchains")
9896
}
99-
try runProgram("pkgutil", "--verbose", "--expand", "\(tmpFile)", "\(tmpDir)", quiet: !verbose)
97+
try await sys.pkgutil(.verbose).expand(pkgPath: tmpFile, dirPath: tmpDir).run(self, quiet: !verbose)
10098

10199
// There's a slight difference in the location of the special Payload file between official swift packages
102100
// and the ones that are mocked here in the test framework.
@@ -106,7 +104,7 @@ public struct MacOS: Platform {
106104
}
107105

108106
await ctx.print("Untarring pkg Payload...")
109-
try runProgram("tar", "-C", "\(toolchainDir)", "-xvf", "\(payload)", quiet: !verbose)
107+
try await sys.tar(.directory(toolchainDir)).extract(.verbose, .archive(payload)).run(self, quiet: !verbose)
110108
}
111109
}
112110

@@ -119,16 +117,19 @@ public struct MacOS: Platform {
119117

120118
if ctx.mockedHomeDir == nil {
121119
await ctx.print("Extracting the swiftly package...")
122-
try runProgram("installer", "-pkg", "\(archive)", "-target", "CurrentUserHomeDirectory")
123-
try? runProgram("pkgutil", "--volume", "\(userHomeDir)", "--forget", "org.swift.swiftly")
120+
try await sys.installer(
121+
pkg: archive,
122+
target: "CurrentUserHomeDirectory"
123+
)
124+
try? await sys.pkgutil(.volume(userHomeDir)).forget(packageId: "org.swift.swiftly").run(self)
124125
} else {
125126
let installDir = userHomeDir / ".swiftly"
126127
try await fs.mkdir(.parents, atPath: installDir)
127128

128129
// In the case of a mock for testing purposes we won't use the installer, perferring a manual process because
129130
// the installer will not install to an arbitrary path, only a volume or user home directory.
130131
let tmpDir = fs.mktemp()
131-
try runProgram("pkgutil", "--expand", "\(archive)", "\(tmpDir)")
132+
try await sys.pkgutil().expand(pkgPath: archive, dirPath: tmpDir).run(self)
132133

133134
// There's a slight difference in the location of the special Payload file between official swift packages
134135
// and the ones that are mocked here in the test framework.
@@ -138,7 +139,7 @@ public struct MacOS: Platform {
138139
}
139140

140141
await ctx.print("Extracting the swiftly package into \(installDir)...")
141-
try runProgram("tar", "-C", "\(installDir)", "-xvf", "\(payload)", quiet: false)
142+
try await sys.tar(.directory(installDir)).extract(.verbose, .archive(payload)).run(self, quiet: false)
142143
}
143144

144145
try self.runProgram((userHomeDir / ".swiftly/bin/swiftly").string, "init")
@@ -161,9 +162,7 @@ public struct MacOS: Platform {
161162

162163
try await fs.remove(atPath: toolchainDir)
163164

164-
try? runProgram(
165-
"pkgutil", "--volume", "\(fs.home)", "--forget", pkgInfo.CFBundleIdentifier, quiet: !verbose
166-
)
165+
try? await sys.pkgutil(.volume(fs.home)).forget(packageId: pkgInfo.CFBundleIdentifier).run(self, quiet: !verbose)
167166
}
168167

169168
public func getExecutableName() -> String {

Sources/Swiftly/Install.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct Install: SwiftlyCommand {
261261

262262
await ctx.print("Installing \(version)")
263263

264-
let tmpFile = fs.mktemp()
264+
let tmpFile = fs.mktemp(ext: ".\(Swiftly.currentPlatform.toolchainFileExtension)")
265265
try await fs.create(file: tmpFile, contents: nil)
266266
return try await fs.withTemporary(files: tmpFile) {
267267
var platformString = config.platform.name

0 commit comments

Comments
 (0)