forked from swiftlang/swiftly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUninstallTests.swift
More file actions
317 lines (279 loc) · 13.5 KB
/
Copy pathUninstallTests.swift
File metadata and controls
317 lines (279 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import Foundation
@testable import Swiftly
@testable import SwiftlyCore
import XCTest
final class UninstallTests: SwiftlyTests {
static let homeName = "uninstallTests"
/// Tests that `swiftly uninstall` successfully handles being invoked when no toolchains have been installed yet.
func testUninstallNoInstalledToolchains() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: []) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "1.2.3"])
_ = try await uninstall.runWithMockedIO(input: ["y"])
try await self.validateInstalledToolchains(
[],
description: "remove not-installed toolchain"
)
}
}
/// Tests that `swiftly uninstall latest` successfully uninstalls the latest stable release of Swift.
func testUninstallLatest() async throws {
let toolchains = Self.allToolchains.filter { $0.asStableRelease != nil }
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains) {
var installed = toolchains
for i in 0..<toolchains.count {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "latest"])
_ = try await uninstall.runWithMockedIO(input: ["y"])
installed.remove(installed.max()!)
try await self.validateInstalledToolchains(
installed,
description: "remove latest \(i)"
)
}
// Ensure that uninstalling when no toolchains are installed is handled gracefully.
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "latest"])
try await uninstall.run()
}
}
/// Tests that a fully-qualified stable release version can be supplied to `swiftly uninstall`.
func testUninstallStableRelease() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: Self.allToolchains) {
var installed = Self.allToolchains
for toolchain in Self.allToolchains.filter({ $0.isStableRelease() }) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", toolchain.name])
_ = try await uninstall.runWithMockedIO(input: ["y"])
installed.remove(toolchain)
try await self.validateInstalledToolchains(
installed,
description: "remove \(toolchain)"
)
}
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "1.2.3"])
_ = try await uninstall.runWithMockedIO(input: ["y"])
try await self.validateInstalledToolchains(
installed,
description: "remove not-installed toolchain"
)
}
}
/// Tests that a fully-qualified snapshot version can be supplied to `swiftly uninstall`.
func testUninstallSnapshot() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: Self.allToolchains) {
var installed = Self.allToolchains
for toolchain in Self.allToolchains.filter({ $0.isSnapshot() }) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", toolchain.name])
_ = try await uninstall.runWithMockedIO(input: ["y"])
installed.remove(toolchain)
try await self.validateInstalledToolchains(
installed,
description: "remove \(toolchain)"
)
}
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "main-snapshot-2022-01-01"])
_ = try await uninstall.runWithMockedIO(input: ["y"])
try await self.validateInstalledToolchains(
installed,
description: "remove not-installed toolchain"
)
}
}
/// Tests that multiple toolchains can be installed at once.
func testBulkUninstall() async throws {
let toolchains = Set(
[
"main-snapshot-2022-01-03",
"main-snapshot-2022-05-02",
"main-snapshot-2022-02-23",
"5.8-snapshot-2022-01-03",
"5.8-snapshot-2022-05-02",
"5.8-snapshot-2022-02-23",
"5.7-snapshot-2022-01-03",
"1.1.3",
"1.1.0",
"1.5.54",
].map { try! ToolchainVersion(parsing: $0) }
)
func bulkUninstallTest(
installed: inout Set<ToolchainVersion>,
argument: String,
uninstalled: Set<ToolchainVersion>
) async throws {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", argument])
let output = try await uninstall.runWithMockedIO(input: ["y"])
installed.subtract(uninstalled)
try await self.validateInstalledToolchains(
installed,
description: "uninstall \(argument)"
)
// Ensure that swiftly checks for confirmation before uninstalling all toolchains.
let outputToolchains = output.compactMap {
try? ToolchainVersion(parsing: $0.trimmingCharacters(in: .whitespaces))
}
XCTAssertEqual(Set(outputToolchains), uninstalled)
}
try await self.withMockedHome(homeName: Self.homeName, toolchains: Set(toolchains)) {
var installed = toolchains
let mainSnapshots = installed.filter { toolchain in
guard case .main = toolchain.asSnapshot?.branch else {
return false
}
return true
}
try await bulkUninstallTest(
installed: &installed,
argument: "main-snapshot",
uninstalled: mainSnapshots
)
let releaseSnapshots = installed.filter { toolchain in
guard case .release(major: 5, minor: 8) = toolchain.asSnapshot?.branch else {
return false
}
return true
}
try await bulkUninstallTest(
installed: &installed,
argument: "5.8-snapshot",
uninstalled: releaseSnapshots
)
let releases = installed.filter { toolchain in
guard let release = toolchain.asStableRelease else {
return false
}
return release.major == 1 && release.minor == 1
}
try await bulkUninstallTest(
installed: &installed,
argument: "1.1",
uninstalled: releases
)
}
}
/// Tests that uninstalling the toolchain that is currently "in use" has the expected behavior.
func testUninstallInUse() async throws {
let toolchains: Set<ToolchainVersion> = [
Self.oldStable,
Self.oldStableNewPatch,
Self.newStable,
Self.oldMainSnapshot,
Self.newMainSnapshot,
Self.oldReleaseSnapshot,
Self.newReleaseSnapshot,
]
func uninstallInUseTest(
_ installed: inout Set<ToolchainVersion>,
toRemove: ToolchainVersion,
expectedInUse: ToolchainVersion?
) async throws {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", toRemove.name])
let output = try await uninstall.runWithMockedIO(input: ["y"])
installed.remove(toRemove)
try await self.validateInstalledToolchains(
installed,
description: "remove \(toRemove)"
)
// Ensure the latest installed toolchain was used when the in-use one was uninstalled.
try await self.validateInUse(expected: expectedInUse)
if let expectedInUse {
// Ensure that something was printed indicating the latest toolchain was marked in use.
XCTAssert(
output.contains(where: { $0.contains(String(describing: expectedInUse)) }),
"output did not contain \(expectedInUse)"
)
}
}
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains, inUse: Self.oldStable) {
var installed = toolchains
try await uninstallInUseTest(&installed, toRemove: Self.oldStable, expectedInUse: Self.oldStableNewPatch)
try await uninstallInUseTest(&installed, toRemove: Self.oldStableNewPatch, expectedInUse: Self.newStable)
try await uninstallInUseTest(&installed, toRemove: Self.newStable, expectedInUse: Self.newMainSnapshot)
// Switch to the old main snapshot to ensure uninstalling it selects the new one.
var use = try self.parseCommand(Use.self, ["use", Self.oldMainSnapshot.name])
try await use.run()
try await uninstallInUseTest(&installed, toRemove: Self.oldMainSnapshot, expectedInUse: Self.newMainSnapshot)
try await uninstallInUseTest(
&installed,
toRemove: Self.newMainSnapshot,
expectedInUse: Self.newReleaseSnapshot
)
// Switch to the old release snapshot to ensure uninstalling it selects the new one.
use = try self.parseCommand(Use.self, ["use", Self.oldReleaseSnapshot.name])
try await use.run()
try await uninstallInUseTest(
&installed,
toRemove: Self.oldReleaseSnapshot,
expectedInUse: Self.newReleaseSnapshot
)
try await uninstallInUseTest(
&installed,
toRemove: Self.newReleaseSnapshot,
expectedInUse: nil
)
}
}
/// Tests that uninstalling the last toolchain is handled properly and cleans up any symlinks.
func testUninstallLastToolchain() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: [Self.oldStable], inUse: Self.oldStable) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", Self.oldStable.name])
_ = try await uninstall.runWithMockedIO(input: ["y"])
let config = try Config.load()
XCTAssertEqual(config.inUse, nil)
// Ensure all symlinks have been cleaned up.
let symlinks = try FileManager.default.contentsOfDirectory(
atPath: Swiftly.currentPlatform.swiftlyBinDir.path
)
XCTAssertEqual(symlinks, [])
}
}
/// Tests that aborting an uninstall works correctly.
func testUninstallAbort() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: Self.allToolchains, inUse: Self.oldStable) {
let preConfig = try Config.load()
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", Self.oldStable.name])
_ = try await uninstall.runWithMockedIO(input: ["n"])
try await self.validateInstalledToolchains(
Self.allToolchains,
description: "abort uninstall"
)
// Ensure config did not change.
XCTAssertEqual(try Config.load(), preConfig)
}
}
/// Tests that providing the `-y` argument skips the confirmation prompt.
func testUninstallAssumeYes() async throws {
try await self.withMockedHome(homeName: Self.homeName, toolchains: [Self.oldStable, Self.newStable]) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "-y", Self.oldStable.name])
_ = try await uninstall.run()
try await self.validateInstalledToolchains(
[Self.newStable],
description: "uninstall did not succeed even with -y provided"
)
}
}
/// Tests that providing "all" as an argument to uninstall will uninstall all toolchains.
func testUninstallAll() async throws {
let toolchains = Set([Self.oldStable, Self.newStable, Self.newMainSnapshot, Self.oldReleaseSnapshot])
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains, inUse: Self.newMainSnapshot) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "-y", "all"])
_ = try await uninstall.run()
try await self.validateInstalledToolchains(
[],
description: "uninstall did not uninstall all toolchains"
)
}
}
/// Tests that uninstalling a toolchain that is the global default, but is not in the list of installed toolchains.
func testUninstallNotInstalled() async throws {
let toolchains = Set([Self.oldStable, Self.newStable, Self.newMainSnapshot, Self.oldReleaseSnapshot])
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains, inUse: Self.newMainSnapshot) {
var config = try await Config.load()
config.inUse = Self.newMainSnapshot
config.installedToolchains.remove(Self.newMainSnapshot)
try await config.save()
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "-y", Self.newMainSnapshot.name])
_ = try await uninstall.run()
try await self.validateInstalledToolchains(
[Self.oldStable, Self.newStable, Self.oldReleaseSnapshot],
description: "uninstall did not uninstall all toolchains"
)
}
}
}