Skip to content

Commit e048289

Browse files
Merge pull request #3712 from SwiftPackageIndex/issue-3655-swift-testing-part-27
Issue 3655 swift testing part 27
2 parents da115a6 + f23bd0a commit e048289

File tree

52 files changed

+356
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+356
-344
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Dependencies
16+
import DependenciesTestSupport
17+
import Testing
18+
19+
20+
struct DependenciesProvider {
21+
init(updateValues: @escaping @Sendable (inout DependencyValues) -> Void) {
22+
self.updateValues = updateValues
23+
}
24+
25+
var updateValues: @Sendable (inout DependencyValues) -> Void
26+
}
27+
28+
29+
extension Trait where Self == _DependenciesTrait {
30+
static func dependencies(
31+
_ provider: DependenciesProvider
32+
) -> Self {
33+
.dependencies(provider.updateValues)
34+
}
35+
}

Tests/AppTests/ValidateSPIManifestControllerTests.swift

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

15-
import XCTest
16-
1715
@testable import App
1816

1917
import SPIManifest
18+
import Testing
2019

2120

22-
final class ValidateSPIManifestControllerTests: XCTestCase {
21+
@Suite struct ValidateSPIManifestControllerTests {
2322

24-
func test_validationResult_basic() throws {
23+
@Test func validationResult_basic() throws {
2524
let yml = ValidateSPIManifest.Model.placeholderManifest
2625

2726
// MUT
2827
let res = ValidateSPIManifestController.validationResult(manifest: yml)
2928

3029
// validate
31-
XCTAssertEqual(res, .valid(try SPIManifest.Manifest(yml: yml)))
30+
#expect(try res == .valid(SPIManifest.Manifest(yml: yml)))
3231
}
3332

34-
func test_validationResult_decodingError() throws {
33+
@Test func validationResult_decodingError() throws {
3534
let yml = """
3635
broken:
3736
"""
@@ -40,24 +39,24 @@ final class ValidateSPIManifestControllerTests: XCTestCase {
4039
let res = ValidateSPIManifestController.validationResult(manifest: yml)
4140

4241
// validate
43-
XCTAssertEqual(res, .invalid("Decoding failed: Key not found: 'version'."))
42+
#expect(res == .invalid("Decoding failed: Key not found: 'version'."))
4443
}
4544

46-
func test_validationResult_tooLarge() throws {
45+
@Test func validationResult_tooLarge() throws {
4746
let targets = (0..<200).map { "Target_\($0)" }.joined(separator: ", ")
4847
let yml = """
4948
version: 1
5049
builder:
5150
configs:
5251
- documentation_targets: [\(targets)]
5352
"""
54-
XCTAssert(yml.count > SPIManifest.Manifest.maxByteSize)
53+
#expect(yml.count > SPIManifest.Manifest.maxByteSize)
5554

5655
// MUT
5756
let res = ValidateSPIManifestController.validationResult(manifest: yml)
5857

5958
// validate
60-
XCTAssertEqual(res, .invalid("File must not exceed \(SPIManifest.Manifest.maxByteSize) bytes. File size: \(yml.count) bytes."))
59+
#expect(res == .invalid("File must not exceed \(SPIManifest.Manifest.maxByteSize) bytes. File size: \(yml.count) bytes."))
6160
}
6261

6362
}

Tests/AppTests/VersionDiffTests.swift

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
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

17-
import XCTest
19+
import Testing
1820

1921

20-
class VersionImmutableReferenceDiffTests: XCTestCase {
21-
// Tests different version diffing scenarios for lower level
22-
// [Version.ImmutableReference] interface.
23-
// Scenarios:
24-
// 1) branch changes commit hash
25-
// 2) new tag is added
26-
// 3) tag is removed
27-
// 4) branch is removed
28-
// 5) tag is moved
22+
// Tests different version diffing scenarios for lower level
23+
// [Version.ImmutableReference] interface.
24+
// Scenarios:
25+
// 1) branch changes commit hash
26+
// 2) new tag is added
27+
// 3) tag is removed
28+
// 4) branch is removed
29+
// 5) tag is moved
30+
@Suite struct VersionDiffTests {
2931

30-
func test_diff_1() throws {
32+
@Test func ImmutableReference_diff_1() throws {
3133
// Branch changes commit hash
3234
// setup
3335
let saved: [Version.ImmutableReference] = [
@@ -42,15 +44,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
4244
])
4345

4446
// validate
45-
XCTAssertEqual(res.toAdd,
46-
[.init(reference: .branch("main"), commit: "hash3")])
47-
XCTAssertEqual(res.toDelete,
48-
[.init(reference: .branch("main"), commit: "hash1")])
49-
XCTAssertEqual(res.toKeep,
50-
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
47+
#expect(res.toAdd == [.init(reference: .branch("main"), commit: "hash3")])
48+
#expect(res.toDelete == [.init(reference: .branch("main"), commit: "hash1")])
49+
#expect(res.toKeep == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
5150
}
5251

53-
func test_diff_2() throws {
52+
@Test func ImmutableReference_diff_2() throws {
5453
// New tag is incoming
5554
// setup
5655
let saved: [Version.ImmutableReference] = [
@@ -66,15 +65,13 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
6665
])
6766

6867
// validate
69-
XCTAssertEqual(res.toAdd,
70-
[.init(reference: .tag(2, 0, 0), commit: "hash4")])
71-
XCTAssertEqual(res.toDelete, [])
72-
XCTAssertEqual(res.toKeep,
73-
[.init(reference: .branch("main"), commit: "hash1"),
68+
#expect(res.toAdd == [.init(reference: .tag(2, 0, 0), commit: "hash4")])
69+
#expect(res.toDelete == [])
70+
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1"),
7471
.init(reference: .tag(1, 2, 3), commit: "hash2")])
7572
}
7673

77-
func test_diff_3() throws {
74+
@Test func ImmutableReference_diff_3() throws {
7875
// Tag was deleted upstream
7976
// setup
8077
let saved: [Version.ImmutableReference] = [
@@ -88,14 +85,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
8885
])
8986

9087
// validate
91-
XCTAssertEqual(res.toAdd, [])
92-
XCTAssertEqual(res.toDelete,
93-
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
94-
XCTAssertEqual(res.toKeep,
95-
[.init(reference: .branch("main"), commit: "hash1")])
88+
#expect(res.toAdd == [])
89+
#expect(res.toDelete == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
90+
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1")])
9691
}
9792

98-
func test_diff_4() throws {
93+
@Test func ImmutableReference_diff_4() throws {
9994
// Branch was deleted upstream
10095
// setup
10196
let saved: [Version.ImmutableReference] = [
@@ -109,14 +104,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
109104
])
110105

111106
// validate
112-
XCTAssertEqual(res.toAdd, [])
113-
XCTAssertEqual(res.toDelete,
114-
[.init(reference: .branch("main"), commit: "hash1")])
115-
XCTAssertEqual(res.toKeep,
116-
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
107+
#expect(res.toAdd == [])
108+
#expect(res.toDelete == [.init(reference: .branch("main"), commit: "hash1")])
109+
#expect(res.toKeep == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
117110
}
118111

119-
func test_diff_5() throws {
112+
@Test func ImmutableReference_diff_5() throws {
120113
// Tag was changed - retagging a release
121114
// setup
122115
let saved: [Version.ImmutableReference] = [
@@ -131,47 +124,40 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
131124
])
132125

133126
// validate
134-
XCTAssertEqual(res.toAdd, [.init(reference: .tag(1, 2, 3), commit: "hash3")])
135-
XCTAssertEqual(res.toDelete, [.init(reference: .tag(1, 2, 3), commit: "hash2")])
136-
XCTAssertEqual(res.toKeep,
137-
[.init(reference: .branch("main"), commit: "hash1")])
127+
#expect(res.toAdd == [.init(reference: .tag(1, 2, 3), commit: "hash3")])
128+
#expect(res.toDelete == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
129+
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1")])
138130
}
139131

140-
}
141-
142-
143-
class VersionDiffTests: AppTestCase {
144-
// Test [Version] based diff (higher level interface)
145-
// Just run an integration scenario, the details are covered in the test above
146-
147-
func test_diff_1() async throws {
148-
// Branch changes commit hash
149-
// setup
150-
let pkg = try await savePackage(on: app.db, "1")
151-
let keptId = UUID()
152-
let saved: [Version] = [
153-
try .init(package: pkg, commit: "hash1", reference: .branch("main")),
154-
try .init(id: keptId,
155-
package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
156-
]
157-
try await saved.save(on: app.db)
158-
159-
// MUT
160-
let res = Version.diff(local: saved, incoming: [
161-
try .init(package: pkg, commit: "hash3", reference: .branch("main")),
162-
try .init(package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
163-
try .init(package: pkg, commit: "hash4", reference: .tag(2, 0, 0)),
164-
])
165-
166-
// validate
167-
XCTAssertEqual(res.toAdd.map(\.immutableReference),
168-
[.init(reference: .branch("main"), commit: "hash3"),
169-
.init(reference: .tag(2, 0, 0), commit: "hash4")])
170-
XCTAssertEqual(res.toDelete.map(\.immutableReference),
171-
[.init(reference: .branch("main"), commit: "hash1")])
172-
XCTAssertEqual(res.toKeep.map(\.immutableReference),
173-
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
174-
XCTAssertEqual(res.toKeep.map(\.id), [keptId])
132+
@Test func Version_diff_1() async throws {
133+
// Test [Version] based diff (higher level interface)
134+
// Just run an integration scenario, the details are covered in the test above
135+
try await withApp { app in
136+
// Branch changes commit hash
137+
// setup
138+
let pkg = try await savePackage(on: app.db, "1")
139+
let keptId = UUID()
140+
let saved: [Version] = [
141+
try .init(package: pkg, commit: "hash1", reference: .branch("main")),
142+
try .init(id: keptId,
143+
package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
144+
]
145+
try await saved.save(on: app.db)
146+
147+
// MUT
148+
let res = Version.diff(local: saved, incoming: [
149+
try .init(package: pkg, commit: "hash3", reference: .branch("main")),
150+
try .init(package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
151+
try .init(package: pkg, commit: "hash4", reference: .tag(2, 0, 0)),
152+
])
153+
154+
// validate
155+
#expect(res.toAdd.map(\.immutableReference) == [.init(reference: .branch("main"), commit: "hash3"),
156+
.init(reference: .tag(2, 0, 0), commit: "hash4")])
157+
#expect(res.toDelete.map(\.immutableReference) == [.init(reference: .branch("main"), commit: "hash1")])
158+
#expect(res.toKeep.map(\.immutableReference) == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
159+
#expect(res.toKeep.map(\.id) == [keptId])
160+
}
175161
}
176162

177163
}

0 commit comments

Comments
 (0)