Skip to content

Commit d97e203

Browse files
committed
Make test scoping traits more versatile and fewer by adding functions to Trait instead of var
1 parent 312e6ae commit d97e203

File tree

9 files changed

+123
-136
lines changed

9 files changed

+123
-136
lines changed

Tests/SwiftlyTests/InitTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import Testing
55

66
@Suite struct InitTests {
7-
@Test(.testHome) func initFresh() async throws {
7+
@Test(.testHome()) func initFresh() async throws {
88
// GIVEN: a fresh user account without Swiftly installed
99
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
1010

@@ -62,7 +62,7 @@ import Testing
6262
}
6363
}
6464

65-
@Test(.testHome) func initOverwrite() async throws {
65+
@Test(.testHome()) func initOverwrite() async throws {
6666
// GIVEN: a user account with swiftly already installed
6767
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
6868

@@ -86,7 +86,7 @@ import Testing
8686
#expect(!Swiftly.currentPlatform.swiftlyToolchainsDir(SwiftlyTests.ctx).appendingPathComponent("foo.txt").fileExists())
8787
}
8888

89-
@Test(.testHome) func initTwice() async throws {
89+
@Test(.testHome()) func initTwice() async throws {
9090
// GIVEN: a user account with swiftly already installed
9191
try? FileManager.default.removeItem(at: Swiftly.currentPlatform.swiftlyConfigFile(SwiftlyTests.ctx))
9292

Tests/SwiftlyTests/InstallTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Testing
99
/// It stops short of verifying that it actually installs the _most_ recently released version, which is the intended
1010
/// behavior, since determining which version is the latest is non-trivial and would require duplicating code
1111
/// from within swiftly itself.
12-
@Test(.testHomeMockedToolchain) func installLatest() async throws {
12+
@Test(.testHomeMockedToolchain()) func installLatest() async throws {
1313
try await SwiftlyTests.runCommand(Install.self, ["install", "latest", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
1414

1515
let config = try Config.load()
@@ -33,7 +33,7 @@ import Testing
3333
}
3434

3535
/// Tests that `swiftly install a.b` installs the latest patch version of Swift a.b.
36-
@Test(.testHomeMockedToolchain) func installLatestPatchVersion() async throws {
36+
@Test(.testHomeMockedToolchain()) func installLatestPatchVersion() async throws {
3737
try await SwiftlyTests.runCommand(Install.self, ["install", "5.7", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
3838

3939
let config = try Config.load()
@@ -57,7 +57,7 @@ import Testing
5757
}
5858

5959
/// Tests that swiftly can install different stable release versions by their full a.b.c versions.
60-
@Test(.testHomeMockedToolchain) func installReleases() async throws {
60+
@Test(.testHomeMockedToolchain()) func installReleases() async throws {
6161
var installedToolchains: Set<ToolchainVersion> = []
6262

6363
try await SwiftlyTests.runCommand(Install.self, ["install", "5.7.0", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
@@ -78,7 +78,7 @@ import Testing
7878
}
7979

8080
/// Tests that swiftly can install main and release snapshots by their full snapshot names.
81-
@Test(.testHomeMockedToolchain) func installSnapshots() async throws {
81+
@Test(.testHomeMockedToolchain()) func installSnapshots() async throws {
8282
var installedToolchains: Set<ToolchainVersion> = []
8383

8484
try await SwiftlyTests.runCommand(Install.self, ["install", "main-snapshot-2023-04-01", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
@@ -100,7 +100,7 @@ import Testing
100100
}
101101

102102
/// Tests that `swiftly install main-snapshot` installs the latest available main snapshot.
103-
@Test(.testHomeMockedToolchain) func installLatestMainSnapshot() async throws {
103+
@Test(.testHomeMockedToolchain()) func installLatestMainSnapshot() async throws {
104104
try await SwiftlyTests.runCommand(Install.self, ["install", "main-snapshot", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
105105

106106
let config = try Config.load()
@@ -127,7 +127,7 @@ import Testing
127127
}
128128

129129
/// Tests that `swiftly install a.b-snapshot` installs the latest available a.b release snapshot.
130-
@Test(.testHomeMockedToolchain) func installLatestReleaseSnapshot() async throws {
130+
@Test(.testHomeMockedToolchain()) func installLatestReleaseSnapshot() async throws {
131131
try await SwiftlyTests.runCommand(Install.self, ["install", "6.0-snapshot", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
132132

133133
let config = try Config.load()
@@ -154,7 +154,7 @@ import Testing
154154
}
155155

156156
/// Tests that swiftly can install both stable release toolchains and snapshot toolchains.
157-
@Test(.testHomeMockedToolchain) func installReleaseAndSnapshots() async throws {
157+
@Test(.testHomeMockedToolchain()) func installReleaseAndSnapshots() async throws {
158158
try await SwiftlyTests.runCommand(Install.self, ["install", "main-snapshot-2023-04-01", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
159159

160160
try await SwiftlyTests.runCommand(Install.self, ["install", "5.9-snapshot-2023-03-28", "--post-install-file=\(Swiftly.currentPlatform.getTempFilePath().path)"])
@@ -209,7 +209,7 @@ import Testing
209209
}
210210

211211
/// Verify that the installed toolchain will be used if no toolchains currently are installed.
212-
@Test(.testHomeMockedToolchain) func installUsesFirstToolchain() async throws {
212+
@Test(.testHomeMockedToolchain()) func installUsesFirstToolchain() async throws {
213213
let config = try Config.load()
214214
#expect(config.inUse == nil)
215215
try await SwiftlyTests.validateInUse(expected: nil)
@@ -225,7 +225,7 @@ import Testing
225225
}
226226

227227
/// Verify that the installed toolchain will be marked as in-use if the --use flag is specified.
228-
@Test(.testHomeMockedToolchain) func installUseFlag() async throws {
228+
@Test(.testHomeMockedToolchain()) func installUseFlag() async throws {
229229
try await SwiftlyTests.installMockedToolchain(toolchain: SwiftlyTests.oldStable)
230230
try await SwiftlyTests.runCommand(Use.self, ["use", SwiftlyTests.oldStable.name])
231231
try await SwiftlyTests.validateInUse(expected: SwiftlyTests.oldStable)

Tests/SwiftlyTests/ListTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ import Testing
154154
}
155155

156156
/// Tests that `list` properly handles the case where no toolchains have been installed yet.
157-
@Test(.testHome) func listEmpty() async throws {
157+
@Test(.testHome(Self.homeName)) func listEmpty() async throws {
158158
var toolchains = try await self.runList(selector: nil)
159159
#expect(toolchains == [])
160160

Tests/SwiftlyTests/PlatformTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Testing
1717
return (mockedToolchainFile, version)
1818
}
1919

20-
@Test(.testHome) func install() async throws {
20+
@Test(.testHome()) func install() async throws {
2121
// GIVEN: a toolchain has been downloaded
2222
var (mockedToolchainFile, version) = try await self.mockToolchainDownload(version: "5.7.1")
2323
// WHEN: the platform installs the toolchain
@@ -43,7 +43,7 @@ import Testing
4343
#expect(2 == toolchains.count)
4444
}
4545

46-
@Test(.testHome) func uninstall() async throws {
46+
@Test(.testHome()) func uninstall() async throws {
4747
// GIVEN: toolchains have been downloaded, and installed
4848
var (mockedToolchainFile, version) = try await self.mockToolchainDownload(version: "5.8.0")
4949
try Swiftly.currentPlatform.install(SwiftlyTests.ctx, from: mockedToolchainFile, version: version, verbose: true)

Tests/SwiftlyTests/RunTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Testing
77
static let homeName = "runTests"
88

99
/// Tests that the `run` command can switch between installed toolchains.
10-
@Test(.mockHomeAllToolchains) func runSelection() async throws {
10+
@Test(.mockHomeToolchains()) func runSelection() async throws {
1111
// GIVEN: a set of installed toolchains
1212
// WHEN: invoking the run command with a selector argument for that toolchain
1313
var output = try await SwiftlyTests.runWithMockedIO(Run.self, ["run", "swift", "--version", "+\(SwiftlyTests.newStable.name)"])
@@ -34,7 +34,7 @@ import Testing
3434
}
3535

3636
/// Tests the `run` command verifying that the environment is as expected
37-
@Test(.mockHomeAllToolchains) func runEnvironment() async throws {
37+
@Test(.mockHomeToolchains()) func runEnvironment() async throws {
3838
// The toolchains directory should be the fist entry on the path
3939
let output = try await SwiftlyTests.runWithMockedIO(Run.self, ["run", try await Swiftly.currentPlatform.getShell(), "-c", "echo $PATH"])
4040
#expect(output.count == 1)

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,47 +71,50 @@ extension SwiftlyCoreContext {
7171
// Convenience test scoping traits
7272

7373
struct TestHomeTrait: TestTrait, TestScoping {
74+
var name: String = "testHome"
75+
76+
init(_ name: String) { self.name = name }
77+
7478
func provideScope(for _: Test, testCase _: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
75-
try await SwiftlyTests.withTestHome {
79+
try await SwiftlyTests.withTestHome(name: self.name) {
7680
try await function()
7781
}
7882
}
7983
}
8084

8185
extension Trait where Self == TestHomeTrait {
8286
/// Run the test with a test home directory.
83-
static var testHome: Self { Self() }
87+
static func testHome(_ name: String = "testHome") -> Self { Self(name) }
8488
}
8589

86-
struct MockHomeAllToolchainsTrait: TestTrait, TestScoping {
87-
func provideScope(for _: Test, testCase _: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
88-
try await SwiftlyTests.withMockedHome(homeName: "testHome", toolchains: SwiftlyTests.allToolchains) {
89-
try await function()
90-
}
91-
}
92-
}
90+
struct MockHomeToolchainsTrait: TestTrait, TestScoping {
91+
var name: String = "testHome"
92+
var toolchains: Set<ToolchainVersion> = SwiftlyTests.allToolchains
9393

94-
extension Trait where Self == MockHomeAllToolchainsTrait {
95-
/// Run the test with this trait to get a mocked home directory with a predefined collection of toolchains already installed.
96-
static var mockHomeAllToolchains: Self { Self() }
97-
}
94+
init(_ name: String, toolchains: Set<ToolchainVersion>) {
95+
self.name = name
96+
self.toolchains = toolchains
97+
}
9898

99-
struct MockHomeNoToolchainsTrait: TestTrait, TestScoping {
10099
func provideScope(for _: Test, testCase _: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
101-
try await SwiftlyTests.withMockedHome(homeName: UseTests.homeName, toolchains: []) {
100+
try await SwiftlyTests.withMockedHome(homeName: self.name, toolchains: self.toolchains) {
102101
try await function()
103102
}
104103
}
105104
}
106105

107-
extension Trait where Self == MockHomeNoToolchainsTrait {
108-
/// Run the test with this trait to get a mocked home directory without any toolchains installed there yet.
109-
static var mockHomeNoToolchains: Self { Self() }
106+
extension Trait where Self == MockHomeToolchainsTrait {
107+
/// Run the test with this trait to get a mocked home directory with a predefined collection of toolchains already installed.
108+
static func mockHomeToolchains(_ homeName: String = "testHome", toolchains: Set<ToolchainVersion> = SwiftlyTests.allToolchains) -> Self { Self(homeName, toolchains: toolchains) }
110109
}
111110

112111
struct TestHomeMockedToolchainTrait: TestTrait, TestScoping {
112+
var name: String = "testHome"
113+
114+
init(_ name: String) { self.name = name }
115+
113116
func provideScope(for _: Test, testCase _: Test.Case?, performing function: @Sendable () async throws -> Void) async throws {
114-
try await SwiftlyTests.withTestHome {
117+
try await SwiftlyTests.withTestHome(name: self.name) {
115118
try await SwiftlyTests.withMockedToolchain {
116119
try await function()
117120
}
@@ -122,7 +125,7 @@ struct TestHomeMockedToolchainTrait: TestTrait, TestScoping {
122125
extension Trait where Self == TestHomeMockedToolchainTrait {
123126
/// Run the test with this trait to get a test home directory and a mocked
124127
/// toolchain can be installed by request, at any version.
125-
static var testHomeMockedToolchain: Self { Self() }
128+
static func testHomeMockedToolchain(_ name: String = "testHome") -> Self { Self(name) }
126129
}
127130

128131
public enum SwiftlyTests {

0 commit comments

Comments
 (0)