Skip to content

Commit 312e6ae

Browse files
committed
Introduce test scoping traits to remove many of the with... closures needed for mocking
1 parent 76ba540 commit 312e6ae

File tree

8 files changed

+586
-646
lines changed

8 files changed

+586
-646
lines changed

Tests/SwiftlyTests/InitTests.swift

Lines changed: 88 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,123 +4,117 @@ import Foundation
44
import Testing
55

66
@Suite struct InitTests {
7-
@Test func initFresh() async throws {
8-
try await SwiftlyTests.withTestHome {
9-
// GIVEN: a fresh user account without Swiftly installed
10-
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
11-
12-
// AND: the user is using the bash shell
13-
let shell = "/bin/bash"
14-
var ctx = SwiftlyTests.ctx
15-
ctx.mockedShell = shell
16-
17-
try await SwiftlyTests.$ctx.withValue(ctx) {
18-
let envScript: URL?
19-
if shell.hasSuffix("bash") || shell.hasSuffix("zsh") {
20-
envScript = Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("env.sh")
21-
} else if shell.hasSuffix("fish") {
22-
envScript = Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("env.fish")
23-
} else {
24-
envScript = nil
25-
}
7+
@Test(.testHome) func initFresh() async throws {
8+
// GIVEN: a fresh user account without Swiftly installed
9+
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
10+
11+
// AND: the user is using the bash shell
12+
let shell = "/bin/bash"
13+
var ctx = SwiftlyTests.ctx
14+
ctx.mockedShell = shell
15+
16+
try await SwiftlyTests.$ctx.withValue(ctx) {
17+
let envScript: URL?
18+
if shell.hasSuffix("bash") || shell.hasSuffix("zsh") {
19+
envScript = Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("env.sh")
20+
} else if shell.hasSuffix("fish") {
21+
envScript = Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("env.fish")
22+
} else {
23+
envScript = nil
24+
}
2625

27-
if let envScript {
28-
#expect(!envScript.fileExists())
29-
}
26+
if let envScript {
27+
#expect(!envScript.fileExists())
28+
}
3029

31-
// WHEN: swiftly is invoked to init the user account and finish swiftly installation
32-
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
33-
34-
// THEN: it creates a valid configuration at the correct version
35-
let config = try Config.load()
36-
#expect(SwiftlyCore.version == config.version)
37-
38-
// AND: it creates an environment script suited for the type of shell
39-
if let envScript {
40-
#expect(envScript.fileExists())
41-
if let scriptContents = try? String(contentsOf: envScript) {
42-
#expect(scriptContents.contains("SWIFTLY_HOME_DIR"))
43-
#expect(scriptContents.contains("SWIFTLY_BIN_DIR"))
44-
#expect(scriptContents.contains(Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).path))
45-
#expect(scriptContents.contains(Swiftly.currentPlatform.swiftlyBinDir(SwiftlyTests.ctx).path))
46-
}
30+
// WHEN: swiftly is invoked to init the user account and finish swiftly installation
31+
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
32+
33+
// THEN: it creates a valid configuration at the correct version
34+
let config = try Config.load()
35+
#expect(SwiftlyCore.version == config.version)
36+
37+
// AND: it creates an environment script suited for the type of shell
38+
if let envScript {
39+
#expect(envScript.fileExists())
40+
if let scriptContents = try? String(contentsOf: envScript) {
41+
#expect(scriptContents.contains("SWIFTLY_HOME_DIR"))
42+
#expect(scriptContents.contains("SWIFTLY_BIN_DIR"))
43+
#expect(scriptContents.contains(Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).path))
44+
#expect(scriptContents.contains(Swiftly.currentPlatform.swiftlyBinDir(SwiftlyTests.ctx).path))
4745
}
46+
}
4847

49-
// AND: it sources the script from the user profile
50-
if let envScript {
51-
var foundSourceLine = false
52-
for p in [".profile", ".zprofile", ".bash_profile", ".bash_login", ".config/fish/conf.d/swiftly.fish"] {
53-
let profile = SwiftlyTests.ctx.mockedHomeDir!.appendingPathComponent(p)
54-
if profile.fileExists() {
55-
if let profileContents = try? String(contentsOf: profile), profileContents.contains(envScript.path) {
56-
foundSourceLine = true
57-
break
58-
}
48+
// AND: it sources the script from the user profile
49+
if let envScript {
50+
var foundSourceLine = false
51+
for p in [".profile", ".zprofile", ".bash_profile", ".bash_login", ".config/fish/conf.d/swiftly.fish"] {
52+
let profile = SwiftlyTests.ctx.mockedHomeDir!.appendingPathComponent(p)
53+
if profile.fileExists() {
54+
if let profileContents = try? String(contentsOf: profile), profileContents.contains(envScript.path) {
55+
foundSourceLine = true
56+
break
5957
}
6058
}
61-
#expect(foundSourceLine)
6259
}
60+
#expect(foundSourceLine)
6361
}
6462
}
6563
}
6664

67-
@Test func initOverwrite() async throws {
68-
try await SwiftlyTests.withTestHome {
69-
// GIVEN: a user account with swiftly already installed
70-
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
65+
@Test(.testHome) func initOverwrite() async throws {
66+
// GIVEN: a user account with swiftly already installed
67+
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
7168

72-
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
69+
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
7370

74-
// Add some customizations to files and directories
75-
var config = try Config.load()
76-
config.version = try SwiftlyVersion(parsing: "100.0.0")
77-
try config.save()
71+
// Add some customizations to files and directories
72+
var config = try Config.load()
73+
config.version = try SwiftlyVersion(parsing: "100.0.0")
74+
try config.save()
7875

79-
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
80-
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
76+
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
77+
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
8178

82-
// WHEN: swiftly is initialized with overwrite enabled
83-
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install", "--overwrite"])
79+
// WHEN: swiftly is initialized with overwrite enabled
80+
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install", "--overwrite"])
8481

85-
// THEN: everything is overwritten in initialization
86-
config = try Config.load()
87-
#expect(SwiftlyCore.version == config.version)
88-
#expect(!Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
89-
#expect(!Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
90-
}
82+
// THEN: everything is overwritten in initialization
83+
config = try Config.load()
84+
#expect(SwiftlyCore.version == config.version)
85+
#expect(!Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
86+
#expect(!Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
9187
}
9288

93-
@Test func initTwice() async throws {
94-
try await SwiftlyTests.withTestHome {
95-
// GIVEN: a user account with swiftly already installed
96-
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
89+
@Test(.testHome) func initTwice() async throws {
90+
// GIVEN: a user account with swiftly already installed
91+
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
9792

98-
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
93+
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
9994

100-
// Add some customizations to files and directories
101-
var config = try Config.load()
102-
config.version = try SwiftlyVersion(parsing: "100.0.0")
103-
try config.save()
95+
// Add some customizations to files and directories
96+
var config = try Config.load()
97+
config.version = try SwiftlyVersion(parsing: "100.0.0")
98+
try config.save()
10499

105-
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
106-
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
100+
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
101+
try Data("".utf8).append(to: Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt"))
107102

108-
// WHEN: swiftly init is invoked a second time
109-
var threw = false
110-
do {
111-
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
112-
} catch {
113-
threw = true
114-
}
103+
// WHEN: swiftly init is invoked a second time
104+
var threw = false
105+
do {
106+
try await SwiftlyTests.runCommand(Init.self, ["init", "--assume-yes", "--skip-install"])
107+
} catch {
108+
threw = true
109+
}
115110

116-
// THEN: init fails
117-
#expect(threw)
111+
// THEN: init fails
112+
#expect(threw)
118113

119-
// AND: files were left intact
120-
config = try Config.load()
121-
#expect(try SwiftlyVersion(parsing: "100.0.0") == config.version)
122-
#expect(Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
123-
#expect(Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
124-
}
114+
// AND: files were left intact
115+
config = try Config.load()
116+
#expect(try SwiftlyVersion(parsing: "100.0.0") == config.version)
117+
#expect(Swiftly.currentPlatform.swiftlyHomeDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
118+
#expect(Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
125119
}
126120
}

0 commit comments

Comments
 (0)