Skip to content

Commit 86e8f6a

Browse files
Merge pull request #3698 from SwiftPackageIndex/issue-3655-swift-testing-part-19
Issue 3655 swift testing part 19
2 parents a0c00e3 + cde9f73 commit 86e8f6a

10 files changed

+987
-945
lines changed

Tests/AppTests/PackageCollectionControllerTests.swift

Lines changed: 127 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -12,143 +12,161 @@
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 SnapshotTesting
19-
import XCTVapor
21+
import Testing
2022

2123

22-
class PackageCollectionControllerTests: AppTestCase {
24+
@Suite struct PackageCollectionControllerTests {
2325

24-
func test_owner_request() async throws {
25-
try XCTSkipIf(!isRunningInCI && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil, "Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable")
26+
@Test(
27+
.disabled(
28+
if: !isRunningInCI() && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil,
29+
"Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable"
30+
)
31+
)
32+
func owner_request() async throws {
2633
try await withDependencies {
2734
$0.date.now = .t0
2835
$0.environment.collectionSigningCertificateChain = EnvironmentClient.liveValue.collectionSigningCertificateChain
2936
$0.environment.collectionSigningPrivateKey = EnvironmentClient.liveValue.collectionSigningPrivateKey
3037
} operation: {
31-
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
32-
do {
33-
let v = try Version(id: UUID(),
34-
package: p,
35-
packageName: "P1-main",
36-
reference: .branch("main"),
37-
toolsVersion: "5.0")
38-
try await v.save(on: app.db)
39-
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
40-
.save(on: app.db)
41-
}
42-
do {
43-
let v = try Version(id: UUID(),
44-
package: p,
45-
latest: .release,
46-
packageName: "P1-tag",
47-
reference: .tag(1, 2, 3),
48-
toolsVersion: "5.1")
49-
try await v.save(on: app.db)
50-
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
51-
.save(on: app.db)
52-
try await Build(version: v,
53-
platform: .iOS,
54-
status: .ok,
55-
swiftVersion: .init(5, 6, 0)).save(on: app.db)
56-
try await Target(version: v, name: "t1").save(on: app.db)
57-
}
58-
try await Repository(package: p,
59-
defaultBranch: "main",
60-
license: .mit,
61-
licenseUrl: "https://foo/mit",
62-
owner: "foo",
63-
summary: "summary 1").create(on: app.db)
38+
try await withApp { app in
39+
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
40+
do {
41+
let v = try Version(id: UUID(),
42+
package: p,
43+
packageName: "P1-main",
44+
reference: .branch("main"),
45+
toolsVersion: "5.0")
46+
try await v.save(on: app.db)
47+
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
48+
.save(on: app.db)
49+
}
50+
do {
51+
let v = try Version(id: UUID(),
52+
package: p,
53+
latest: .release,
54+
packageName: "P1-tag",
55+
reference: .tag(1, 2, 3),
56+
toolsVersion: "5.1")
57+
try await v.save(on: app.db)
58+
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
59+
.save(on: app.db)
60+
try await Build(version: v,
61+
platform: .iOS,
62+
status: .ok,
63+
swiftVersion: .init(5, 6, 0)).save(on: app.db)
64+
try await Target(version: v, name: "t1").save(on: app.db)
65+
}
66+
try await Repository(package: p,
67+
defaultBranch: "main",
68+
license: .mit,
69+
licenseUrl: "https://foo/mit",
70+
owner: "foo",
71+
summary: "summary 1").create(on: app.db)
6472

65-
// MUT
66-
let encoder = self.encoder
67-
try await app.test(
68-
.GET,
69-
"foo/collection.json",
70-
afterResponse: { @MainActor res async throws in
71-
// validation
72-
XCTAssertEqual(res.status, .ok)
73-
let json = try res.content.decode(PackageCollection.self)
74-
assertSnapshot(of: json, as: .json(encoder))
75-
})
73+
// MUT
74+
let encoder = self.encoder
75+
try await app.test(
76+
.GET,
77+
"foo/collection.json",
78+
afterResponse: { @MainActor res async throws in
79+
// validation
80+
#expect(res.status == .ok)
81+
let json = try res.content.decode(PackageCollection.self)
82+
assertSnapshot(of: json, as: .json(encoder))
83+
})
84+
}
7685
}
7786
}
7887

79-
func test_custom_request() async throws {
80-
try XCTSkipIf(!isRunningInCI && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil, "Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable")
88+
@Test(
89+
.disabled(
90+
if: !isRunningInCI() && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil,
91+
"Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable"
92+
)
93+
)
94+
func custom_request() async throws {
8195
try await withDependencies {
8296
$0.date.now = .t0
8397
$0.environment.collectionSigningCertificateChain = EnvironmentClient.liveValue.collectionSigningCertificateChain
8498
$0.environment.collectionSigningPrivateKey = EnvironmentClient.liveValue.collectionSigningPrivateKey
8599
} operation: {
86-
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
87-
do {
88-
let v = try Version(id: UUID(),
89-
package: p,
90-
packageName: "P1-main",
91-
reference: .branch("main"),
92-
toolsVersion: "5.0")
93-
try await v.save(on: app.db)
94-
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
95-
.save(on: app.db)
96-
}
97-
do {
98-
let v = try Version(id: UUID(),
99-
package: p,
100-
latest: .release,
101-
packageName: "P1-tag",
102-
reference: .tag(1, 2, 3),
103-
toolsVersion: "5.1")
104-
try await v.save(on: app.db)
105-
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
106-
.save(on: app.db)
107-
try await Build(version: v,
108-
platform: .iOS,
109-
status: .ok,
110-
swiftVersion: .init(5, 6, 0)).save(on: app.db)
111-
try await Target(version: v, name: "t1").save(on: app.db)
112-
}
113-
try await Repository(package: p,
114-
defaultBranch: "main",
115-
license: .mit,
116-
licenseUrl: "https://foo/mit",
117-
owner: "foo",
118-
summary: "summary 1").create(on: app.db)
119-
let collection = CustomCollection(id: .id2, .init(key: "custom-collection",
120-
name: "Custom Collection",
121-
url: "https://github.com/foo/bar/list.json"))
122-
try await collection.save(on: app.db)
123-
try await collection.$packages.attach(p, on: app.db)
100+
try await withApp { app in
101+
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
102+
do {
103+
let v = try Version(id: UUID(),
104+
package: p,
105+
packageName: "P1-main",
106+
reference: .branch("main"),
107+
toolsVersion: "5.0")
108+
try await v.save(on: app.db)
109+
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
110+
.save(on: app.db)
111+
}
112+
do {
113+
let v = try Version(id: UUID(),
114+
package: p,
115+
latest: .release,
116+
packageName: "P1-tag",
117+
reference: .tag(1, 2, 3),
118+
toolsVersion: "5.1")
119+
try await v.save(on: app.db)
120+
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
121+
.save(on: app.db)
122+
try await Build(version: v,
123+
platform: .iOS,
124+
status: .ok,
125+
swiftVersion: .init(5, 6, 0)).save(on: app.db)
126+
try await Target(version: v, name: "t1").save(on: app.db)
127+
}
128+
try await Repository(package: p,
129+
defaultBranch: "main",
130+
license: .mit,
131+
licenseUrl: "https://foo/mit",
132+
owner: "foo",
133+
summary: "summary 1").create(on: app.db)
134+
let collection = CustomCollection(id: .id2, .init(key: "custom-collection",
135+
name: "Custom Collection",
136+
url: "https://github.com/foo/bar/list.json"))
137+
try await collection.save(on: app.db)
138+
try await collection.$packages.attach(p, on: app.db)
124139

125-
// MUT
126-
let encoder = self.encoder
127-
try await app.test(
128-
.GET,
129-
"collections/custom-collection/collection.json",
130-
afterResponse: { @MainActor res async throws in
131-
// validation
132-
XCTAssertEqual(res.status, .ok)
133-
let json = try res.content.decode(PackageCollection.self)
134-
assertSnapshot(of: json, as: .json(encoder))
135-
})
140+
// MUT
141+
let encoder = self.encoder
142+
try await app.test(
143+
.GET,
144+
"collections/custom-collection/collection.json",
145+
afterResponse: { @MainActor res async throws in
146+
// validation
147+
#expect(res.status == .ok)
148+
let json = try res.content.decode(PackageCollection.self)
149+
assertSnapshot(of: json, as: .json(encoder))
150+
})
151+
}
136152
}
137153
}
138154

139-
func test_nonexisting_404() throws {
155+
@Test func nonexisting_404() async throws {
140156
// Ensure a request for a non-existing collection returns a 404
141-
try withDependencies {
157+
try await withDependencies {
142158
$0.environment.dbId = { nil }
143159
} operation: {
144-
// MUT
145-
try app.test(
146-
.GET,
147-
"foo/collection.json",
148-
afterResponse: { res in
149-
// validation
150-
XCTAssertEqual(res.status, .notFound)
151-
})
160+
try await withApp { app in
161+
// MUT
162+
try await app.test(
163+
.GET,
164+
"foo/collection.json",
165+
afterResponse: { res async in
166+
// validation
167+
#expect(res.status == .notFound)
168+
})
169+
}
152170
}
153171
}
154172

0 commit comments

Comments
 (0)