-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
BuildIndexModelTests.swift
260 lines (236 loc) · 10.7 KB
/
BuildIndexModelTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2020-2021 Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
@testable import App
import Plot
import XCTVapor
class BuildIndexModelTests: AppTestCase {
func test_init_no_name() async throws {
// Tests behaviour when we're lacking data
// setup package without package name
let pkg = try savePackage(on: app.db, "1".url)
try await Repository(package: pkg,
defaultBranch: "main",
forks: 42,
license: .mit,
name: "bar",
owner: "foo",
stars: 17,
summary: "summary").save(on: app.db)
try Version(package: pkg, latest: .defaultBranch).save(on: app.db).wait()
let (pkgInfo, buildInfo) = try await PackageController.BuildsRoute
.query(on: app.db, owner: "foo", repository: "bar")
// MUT
let m = BuildIndex.Model(packageInfo: pkgInfo, buildInfo: buildInfo)
// validate
XCTAssertNotNil(m)
}
func test_completedBuildCount() throws {
let m = BuildIndex.Model.mock
// mock contains build for three Swift versions, 5.3, 5.4, 5.5
// each has the same default setup:
// - 4 x .ok
// - 1 x .failed
// - 1 x .triggered
// -> 5 completed per Swift version (4 x .ok + .failed)
// -> 15 completed per package version
// (there are 3 versions, default branch, release, and beta)
// -> 45 completed overall
// -> 44 minus the linux/5.5 build to test .none
// -> 44 the tvos/5.5 build to test .timeout does not change the completed tally
// -> 43 minus the watchos/5.5 build to test .infrastructureError
// -> 43 completed in total
XCTAssertEqual(m.completedBuildCount, 43)
}
func test_packageURL() throws {
let m = BuildIndex.Model.mock
XCTAssertEqual(m.packageURL, "/foo/foobar")
}
func test_buildMatrix() throws {
// setup
let id = UUID()
let stable: [BuildInfo] = [
.init(id: id, swiftVersion: .init(5, 6, 0), platform: .ios, status: .ok, generatedDocs: true),
.init(id: id, swiftVersion: .init(5, 5, 0), platform: .macosXcodebuild, status: .ok, generatedDocs: false),
.init(id: id, swiftVersion: .init(5, 4, 0), platform: .tvos, status: .ok, generatedDocs: false),
]
let latest: [BuildInfo] = [
.init(id: id, swiftVersion: .init(5, 5, 0), platform: .macosSpm, status: .failed, generatedDocs: false),
.init(id: id, swiftVersion: .init(5, 4, 0), platform: .tvos, status: .ok, generatedDocs: false),
]
let model = BuildIndex.Model.init(owner: "foo",
ownerName: "Foo",
repositoryName: "bar",
packageName: "bar",
buildGroups: [
.init(name: "1.2.3", kind: .release, builds: stable),
.init(name: "2.0.0-b1", kind: .preRelease, builds: []),
.init(name: "main", kind: .defaultBranch, builds: latest),
])
// MUT
let matrix = model.buildMatrix
// validate
XCTAssertEqual(matrix.values.keys.count, 24)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_6, platform: .ios)]?.map(\.column.label),
["1.2.3", "2.0.0-b1", "main"]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_6, platform: .ios)]?.map(\.value?.status),
.some([.ok, nil, nil])
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_5,
platform: .macosXcodebuild)]?.map(\.value?.status),
[.ok, nil, nil]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_5, platform: .macosSpm)]?.map(\.value?.status),
[nil, nil, .failed]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_4, platform: .tvos)]?.map(\.value?.status),
[.ok, nil, .ok]
)
}
func test_buildMatrix_no_beta() throws {
// Test BuildMatrix mapping, in particular absence of a beta version
// setup
let id = UUID()
let stable: [BuildInfo] = [
.init(id: id, swiftVersion: .init(5, 6, 0), platform: .ios, status: .ok, generatedDocs: false),
.init(id: id, swiftVersion: .init(5, 5, 0), platform: .macosXcodebuild, status: .ok, generatedDocs: false),
.init(id: id, swiftVersion: .init(5, 4, 0), platform: .tvos, status: .ok, generatedDocs: false),
]
let latest: [BuildInfo] = [
.init(id: id, swiftVersion: .init(5, 5, 0), platform: .macosSpm, status: .failed, generatedDocs: false),
.init(id: id, swiftVersion: .init(5, 4, 0), platform: .tvos, status: .ok, generatedDocs: false),
]
let model = BuildIndex.Model.init(owner: "foo",
ownerName: "Foo",
repositoryName: "bar",
packageName: "bar",
buildGroups: [
.init(name: "1.2.3", kind: .release, builds: stable),
.init(name: "main", kind: .defaultBranch, builds: latest),
])
// MUT
let matrix = model.buildMatrix
// validate
XCTAssertEqual(matrix.values.keys.count, 24)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_6, platform: .ios)]?.map(\.column.label),
["1.2.3", "main"]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_6, platform: .ios)]?.map(\.value?.status),
[.ok, nil]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_5,
platform: .macosXcodebuild)]?.map(\.value?.status),
[.ok, nil]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_5,
platform: .macosSpm)]?.map(\.value?.status),
[nil, .failed]
)
XCTAssertEqual(
matrix.values[.init(swiftVersion: .v5_4, platform: .tvos)]?.map(\.value?.status),
[.ok, .ok]
)
}
func test_BuildCell() throws {
let id = UUID()
XCTAssertEqual(BuildCell("1.2.3", .release, id, .ok, generatedDocs: false).node.render(), """
<div class="succeeded"><a href="/builds/\(id.uuidString)">Build Succeeded</a></div>
""")
XCTAssertEqual(BuildCell("1.2.3", .release, id, .ok, generatedDocs: true).node.render(), """
<div class="succeeded"><a href="/builds/\(id.uuidString)">Build Succeeded</a><span class="generated-docs" title="If successful, this build generated package documentation."></span></div>
""")
XCTAssertEqual(BuildCell("1.2.3", .release, id, .failed, generatedDocs: false).node.render(), """
<div class="failed"><a href="/builds/\(id.uuidString)">Build Failed</a></div>
""")
XCTAssertEqual(BuildCell("1.2.3", .release).node.render(), """
<div><span>Build Pending</span></div>
""")
}
func test_BuildItem() throws {
// setup
let id = UUID()
let bi = BuildItem(index: .init(swiftVersion: .v5_7, platform: .ios),
values: [.init("1.2.3", .release, id, .ok, generatedDocs: false),
.init("2.0.0-b1", .preRelease),
.init("develop", .defaultBranch, id, .failed, generatedDocs: false)])
// MUT - altogether now
let node = bi.node
let expectation: Node<HTML.ListContext> = .li(
.class("row"),
.div(
.class("row-labels"),
.strong("iOS")
),
.div(
.class("column-labels"),
.div(.span(.class("stable"), .text("1.2.3"))),
.div(.span(.class("beta"), .text("2.0.0-b1"))),
.div(.span(.class("branch"), .text("develop")))
),
.div(
.class("results"),
.div(.class("succeeded"), .a(.href("/builds/\(id.uuidString)"), .text("Build Succeeded"))),
.div(.span(.text("Build Pending"))),
.div(.class("failed"), .a(.href("/builds/\(id.uuidString)"), .text("Build Failed")))
)
)
XCTAssertEqual(node.render(), expectation.render())
}
func test_BuildItem_generatedDocs() throws {
// setup
let id = UUID()
let bi = BuildItem(index: .init(swiftVersion: .v5_7, platform: .ios),
values: [ .init("main", .defaultBranch, id, .ok, generatedDocs: true) ])
// MUT
let node = bi.node
let expectation: Node<HTML.ListContext> = .li(
.class("row"),
.div(
.class("row-labels"),
.strong("iOS")
),
.div(
.class("column-labels"),
.div(.span(.class("branch"), .text("main")))
),
.div(
.class("results"),
.div(
.class("succeeded"),
.a(
.href("/builds/\(id.uuidString)"),
.text("Build Succeeded")
),
.span(
.class("generated-docs"),
.title("If successful, this build generated package documentation.")
)
)
)
)
XCTAssertEqual(node.render(), expectation.render())
}
}
fileprivate typealias BuildCell = BuildIndex.Model.BuildCell
fileprivate typealias BuildInfo = BuildIndex.Model.BuildInfo
fileprivate typealias BuildItem = BuildIndex.Model.BuildItem