Skip to content

Commit 8b98f2a

Browse files
committed
Convert GitLiveTests
1 parent d255f2a commit 8b98f2a

File tree

1 file changed

+87
-84
lines changed

1 file changed

+87
-84
lines changed

Tests/AppTests/GitLiveTests.swift

Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,115 +12,118 @@
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 in
28+
try await XCTAssertEqualAsync(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 in
34+
try await XCTAssertEqualAsync(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 in
41+
try await XCTAssertEqualAsync(try await Git.lastCommitDate(at: path),
42+
Date(timeIntervalSince1970: 1554248253)) // Sat, 21 March 2015
5143
}
5244
}
53-
}
5445

55-
56-
// Tests
57-
extension GitLiveTests {
58-
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 in
48+
try await XCTAssertEqualAsync(
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+
}
6271
}
6372

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
73+
@Test func hasBranch() async throws {
74+
try await withGitRepository(defaultDependencies) { path in
75+
try await XCTAssertEqualAsync(try await Git.hasBranch(.branch("master"), at: path), true)
76+
try await XCTAssertEqualAsync(try await Git.hasBranch(.branch("main"), at: path), false)
77+
}
6878
}
6979

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
80+
@Test func revisionInfo() async throws {
81+
try await withGitRepository(defaultDependencies) { path in
82+
try await XCTAssertEqualAsync(try await Git.revisionInfo(.tag(0,5,2), at: path),
83+
.init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
84+
date: .init(timeIntervalSince1970: 1554248253)))
85+
try await XCTAssertEqualAsync(try await Git.revisionInfo(.branch("master"), at: path),
86+
.init(commit: "178566b112afe6bef3770678f1bbab6e5c626993",
87+
date: .init(timeIntervalSince1970: 1554248253)))
88+
}
7489
}
7590

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-
)
91+
@Test func shortlog() async throws {
92+
try await withGitRepository(defaultDependencies) { path in
93+
try await XCTAssertEqualAsync(try await Git.shortlog(at: path), """
94+
36\tNeil Pankey
95+
21\tJacob Williams
96+
""")
97+
}
10098
}
10199

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-
}
100+
}
107101

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-
}
117102

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-
""")
103+
private func withGitRepository(
104+
_ updateValuesForOperation: (inout DependencyValues) async throws -> Void = { _ in },
105+
_ test: (_ zipFilePath: String) async throws -> Void
106+
) async throws {
107+
try await withDependencies(updateValuesForOperation) {
108+
try await withTempDir { tempDir in
109+
let fixtureFile = fixturesDirectory().appendingPathComponent("ErrNo.zip").path
110+
try await ShellOut.shellOut(to: .init(command: "unzip", arguments: [fixtureFile]), at: tempDir)
111+
let path = "\(tempDir)/ErrNo"
112+
try await test(path)
113+
}
124114
}
115+
}
116+
125117

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

0 commit comments

Comments
 (0)