Skip to content

Commit d789b43

Browse files
Merge pull request #3693 from SwiftPackageIndex/issue-3655-swift-testing-part-14
Issue 3655 swift testing part 14
2 parents d255f2a + 7e4cf9c commit d789b43

File tree

4 files changed

+174
-163
lines changed

4 files changed

+174
-163
lines changed

Sources/App/Core/Dependencies/LoggerClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ extension LoggerClient {
4747
set(to: logger)
4848
}
4949
}
50+
51+
static var noop: Self {
52+
.init(log: { _, _ in }, set: { _ in })
53+
}
5054
}
5155
#endif
5256

Tests/AppTests/GitLiveTests.swift

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,115 +12,119 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import XCTest
15+
import Foundation
1616

1717
@testable import App
1818

1919
import Dependencies
2020
import ShellOut
21+
import Testing
2122

2223

23-
// setup
24-
class GitLiveTests: XCTestCase {
25-
static let tempDir = NSTemporaryDirectory().appending("spi-test-\(UUID())")
26-
static let sampleGitRepoName = "ErrNo"
27-
static let sampleGitRepoZipFile = fixturesDirectory()
28-
.appendingPathComponent("\(sampleGitRepoName).zip").path
24+
@Suite struct GitLiveTests {
2925

30-
var path: String { "\(Self.tempDir)/\(Self.sampleGitRepoName)" }
31-
nonisolated(unsafe) static var hasRunSetup = false
32-
33-
override func setUp() async throws {
34-
// Simulate a class setUp (which does not exist as an async function)
35-
if Self.hasRunSetup { return }
36-
Self.hasRunSetup = true
37-
try! Foundation.FileManager.default.createDirectory(atPath: Self.tempDir, withIntermediateDirectories: false, attributes: nil)
38-
try! await ShellOut.shellOut(to: .init(command: "unzip", arguments: [Self.sampleGitRepoZipFile]), at: Self.tempDir)
26+
@Test func commitCount() async throws {
27+
try await withGitRepository(defaultDependencies) { path throws in
28+
#expect(try await Git.commitCount(at: path) == 57)
29+
}
3930
}
4031

41-
override class func tearDown() {
42-
try? Foundation.FileManager.default.removeItem(atPath: tempDir)
32+
@Test func firstCommitDate() async throws {
33+
try await withGitRepository(defaultDependencies) { path throws in
34+
#expect(try await Git.firstCommitDate(at: path)
35+
== Date(timeIntervalSince1970: 1426918070)) // Sat, 21 March 2015
36+
}
4337
}
4438

45-
override func invokeTest() {
46-
withDependencies {
47-
$0.logger.log = { @Sendable _, _ in }
48-
$0.shell = .liveValue
49-
} operation: {
50-
super.invokeTest()
39+
@Test func lastCommitDate() async throws {
40+
try await withGitRepository(defaultDependencies) { path throws in
41+
#expect(try await Git.lastCommitDate(at: path)
42+
== Date(timeIntervalSince1970: 1554248253)) // Sat, 21 March 2015
5143
}
5244
}
53-
}
54-
55-
56-
// Tests
57-
extension GitLiveTests {
5845

59-
func test_commitCount() async throws {
60-
let path = self.path
61-
try await XCTAssertEqualAsync(try await Git.commitCount(at: path), 57)
46+
@Test func getTags() async throws {
47+
try await withGitRepository(defaultDependencies) { path throws in
48+
#expect(
49+
try await Git.getTags(at: path) == [
50+
.tag(0,2,0),
51+
.tag(0,2,1),
52+
.tag(0,2,2),
53+
.tag(0,2,3),
54+
.tag(0,2,4),
55+
.tag(0,2,5),
56+
.tag(0,3,0),
57+
.tag(0,4,0),
58+
.tag(0,4,1),
59+
.tag(0,4,2),
60+
.tag(0,5,0),
61+
.tag(0,5,1),
62+
.tag(0,5,2),
63+
.tag(.init(0,0,1), "v0.0.1"),
64+
.tag(.init(0,0,2), "v0.0.2"),
65+
.tag(.init(0,0,3), "v0.0.3"),
66+
.tag(.init(0,0,4), "v0.0.4"),
67+
.tag(.init(0,0,5), "v0.0.5"),
68+
.tag(.init(0,1,0), "v0.1.0")
69+
]
70+
)
71+
}
6272
}
6373

64-
func test_firstCommitDate() async throws {
65-
let path = self.path
66-
try await XCTAssertEqualAsync(try await Git.firstCommitDate(at: path),
67-
Date(timeIntervalSince1970: 1426918070)) // Sat, 21 March 2015
74+
@Test func hasBranch() async throws {
75+
try await withGitRepository(defaultDependencies) { path throws in
76+
#expect(try await Git.hasBranch(.branch("master"), at: path) == true)
77+
#expect(try await Git.hasBranch(.branch("main"), at: path) == false)
78+
}
6879
}
6980

70-
func test_lastCommitDate() async throws {
71-
let path = self.path
72-
try await XCTAssertEqualAsync(try await Git.lastCommitDate(at: path),
73-
Date(timeIntervalSince1970: 1554248253)) // Sat, 21 March 2015
81+
@Test func revisionInfo() async throws {
82+
try await withGitRepository(defaultDependencies) { path throws in
83+
#expect(try await Git.revisionInfo(.tag(0,5,2), at: path)
84+
== .init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
85+
date: .init(timeIntervalSince1970: 1554248253)))
86+
#expect(try await Git.revisionInfo(.branch("master"), at: path)
87+
== .init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
88+
date: .init(timeIntervalSince1970: 1554248253)))
89+
}
7490
}
7591

76-
func test_getTags() async throws {
77-
let path = self.path
78-
try await XCTAssertEqualAsync(
79-
try await Git.getTags(at: path),
80-
[.tag(0,2,0),
81-
.tag(0,2,1),
82-
.tag(0,2,2),
83-
.tag(0,2,3),
84-
.tag(0,2,4),
85-
.tag(0,2,5),
86-
.tag(0,3,0),
87-
.tag(0,4,0),
88-
.tag(0,4,1),
89-
.tag(0,4,2),
90-
.tag(0,5,0),
91-
.tag(0,5,1),
92-
.tag(0,5,2),
93-
.tag(.init(0,0,1), "v0.0.1"),
94-
.tag(.init(0,0,2), "v0.0.2"),
95-
.tag(.init(0,0,3), "v0.0.3"),
96-
.tag(.init(0,0,4), "v0.0.4"),
97-
.tag(.init(0,0,5), "v0.0.5"),
98-
.tag(.init(0,1,0), "v0.1.0")]
99-
)
92+
@Test func shortlog() async throws {
93+
try await withGitRepository(defaultDependencies) { path throws in
94+
#expect(try await Git.shortlog(at: path) == """
95+
36\tNeil Pankey
96+
21\tJacob Williams
97+
""")
98+
}
10099
}
101100

102-
func test_hasBranch() async throws {
103-
let path = self.path
104-
try await XCTAssertEqualAsync(try await Git.hasBranch(.branch("master"), at: path), true)
105-
try await XCTAssertEqualAsync(try await Git.hasBranch(.branch("main"), at: path), false)
106-
}
101+
}
107102

108-
func test_revisionInfo() async throws {
109-
let path = self.path
110-
try await XCTAssertEqualAsync(try await Git.revisionInfo(.tag(0,5,2), at: path),
111-
.init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
112-
date: .init(timeIntervalSince1970: 1554248253)))
113-
try await XCTAssertEqualAsync(try await Git.revisionInfo(.branch("master"), at: path),
114-
.init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
115-
date: .init(timeIntervalSince1970: 1554248253)))
116-
}
117103

118-
func test_shortlog() async throws {
119-
let path = self.path
120-
try await XCTAssertEqualAsync(try await Git.shortlog(at: path), """
121-
36\tNeil Pankey
122-
21\tJacob Williams
123-
""")
104+
private func withGitRepository(
105+
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void = { _ in },
106+
_ test: (_ zipFilePath: String) async throws -> Void
107+
) async throws {
108+
try await withDependencies(updateValuesForOperation) {
109+
try await withTempDir { tempDir in
110+
let fixtureFile = fixturesDirectory().appendingPathComponent("ErrNo.zip").path
111+
try await ShellOut.shellOut(to: .init(command: "unzip", arguments: [fixtureFile]), at: tempDir)
112+
let path = "\(tempDir)/ErrNo"
113+
try await test(path)
114+
}
124115
}
116+
}
125117

118+
119+
extension GitLiveTests {
120+
#if compiler(>=6.1)
121+
#warning("Move this into a trait on @Test")
122+
// See https://forums.swift.org/t/converting-xctest-invoketest-to-swift-testing/77692/4 for details
123+
#endif
124+
var defaultDependencies: (inout DependencyValues) async throws -> Void {
125+
{
126+
$0.logger = .noop
127+
$0.shell = .liveValue
128+
}
129+
}
126130
}

Tests/AppTests/GitTests.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Foundation
16+
1517
@testable import App
1618

1719
import Dependencies
1820
import ShellOut
19-
import XCTVapor
20-
21+
import Testing
2122

22-
class GitTests: XCTestCase {
2323

24+
@Suite struct GitTests {
2425

25-
func test_tag() async throws {
26+
@Test func tag() async throws {
2627
try await withDependencies {
2728
$0.shell.run = mock(for: "git tag", """
2829
test
@@ -32,33 +33,32 @@ class GitTests: XCTestCase {
3233
1.0.2
3334
"""
3435
)
35-
} operation: {
36-
try await XCTAssertEqualAsync(
37-
try await Git.getTags(at: "ignored"), [
38-
.tag(.init(1, 0, 0, "pre")),
39-
.tag(.init(1, 0, 0)),
40-
.tag(.init(1, 0, 1)),
41-
.tag(.init(1, 0, 2)),
42-
])
36+
} operation: { () async throws in
37+
#expect(try await Git.getTags(at: "ignored") == [
38+
.tag(.init(1, 0, 0, "pre")),
39+
.tag(.init(1, 0, 0)),
40+
.tag(.init(1, 0, 1)),
41+
.tag(.init(1, 0, 2)),
42+
])
4343
}
4444
}
4545

46-
func test_revInfo() async throws {
46+
@Test func revInfo() async throws {
4747
try await withDependencies {
4848
$0.shell.run = { @Sendable cmd, _ in
4949
if cmd.description == #"git log -n1 --format=tformat:"%H-%ct" 2.2.1"# {
5050
return "63c973f3c2e632a340936c285e94d59f9ffb01d5-1536799579"
5151
}
5252
throw TestError.unknownCommand
5353
}
54-
} operation: {
55-
try await XCTAssertEqualAsync(try await Git.revisionInfo(.tag(.init(2, 2, 1)), at: "ignored"),
56-
.init(commit: "63c973f3c2e632a340936c285e94d59f9ffb01d5",
57-
date: Date(timeIntervalSince1970: 1536799579)))
54+
} operation: { () async throws in
55+
#expect(try await Git.revisionInfo(.tag(2, 2, 1), at: "ignored")
56+
== .init(commit: "63c973f3c2e632a340936c285e94d59f9ffb01d5",
57+
date: Date(timeIntervalSince1970: 1536799579)))
5858
}
5959
}
6060

61-
func test_revInfo_tagName() async throws {
61+
@Test func revInfo_tagName() async throws {
6262
// Ensure we look up by tag name and not semver
6363
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/139
6464
try await withDependencies {
@@ -68,10 +68,10 @@ class GitTests: XCTestCase {
6868
}
6969
throw TestError.unknownCommand
7070
}
71-
} operation: {
72-
try await XCTAssertEqualAsync(try await Git.revisionInfo(.tag(.init(2, 2, 1), "v2.2.1"), at: "ignored"),
73-
.init(commit: "63c973f3c2e632a340936c285e94d59f9ffb01d5",
74-
date: Date(timeIntervalSince1970: 1536799579)))
71+
} operation: { () async throws in
72+
#expect(try await Git.revisionInfo(.tag(.init(2, 2, 1), "v2.2.1"), at: "ignored")
73+
== .init(commit: "63c973f3c2e632a340936c285e94d59f9ffb01d5",
74+
date: Date(timeIntervalSince1970: 1536799579)))
7575
}
7676
}
7777

0 commit comments

Comments
 (0)