|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import XCTest |
| 15 | +import Foundation |
16 | 16 |
|
17 | 17 | @testable import App |
18 | 18 |
|
19 | 19 | import Dependencies |
20 | 20 | import ShellOut |
| 21 | +import Testing |
21 | 22 |
|
22 | 23 |
|
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 { |
29 | 25 |
|
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 | + } |
39 | 30 | } |
40 | 31 |
|
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 | + } |
43 | 37 | } |
44 | 38 |
|
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 |
51 | 43 | } |
52 | 44 | } |
53 | | -} |
54 | 45 |
|
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 | + } |
62 | 71 | } |
63 | 72 |
|
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 | + } |
68 | 78 | } |
69 | 79 |
|
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 | + } |
74 | 89 | } |
75 | 90 |
|
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 | + } |
100 | 98 | } |
101 | 99 |
|
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 | +} |
107 | 101 |
|
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 | | - } |
117 | 102 |
|
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 | + } |
124 | 114 | } |
| 115 | +} |
| 116 | + |
125 | 117 |
|
| 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 | + } |
126 | 129 | } |
0 commit comments