diff --git a/Tests/AppTests/AuthorControllerTests.swift b/Tests/AppTests/AuthorControllerTests.swift index 8a77c7915..123f5d4b4 100644 --- a/Tests/AppTests/AuthorControllerTests.swift +++ b/Tests/AppTests/AuthorControllerTests.swift @@ -12,89 +12,98 @@ // See the License for the specific language governing permissions and // limitations under the License. -import XCTest - @testable import App import Dependencies +import Testing import Vapor -class AuthorControllerTests: AppTestCase { - - func test_query() async throws { - // setup - let p = try await savePackage(on: app.db, "1") - try await Repository(package: p, owner: "owner").save(on: app.db) - try await Version(package: p, latest: .defaultBranch).save(on: app.db) +@Suite struct AuthorControllerTests { - // MUT - let pkg = try await AuthorController.query(on: app.db, owner: "owner") - - // validate - XCTAssertEqual(pkg.map(\.model.id), [p.id]) - } + @Test func query() async throws { + try await withApp { app in + // setup + let p = try await savePackage(on: app.db, "1") + try await Repository(package: p, owner: "owner").save(on: app.db) + try await Version(package: p, latest: .defaultBranch).save(on: app.db) - func test_query_no_version() async throws { - // setup - let p = try await savePackage(on: app.db, "1") - try await Repository(package: p, owner: "owner").save(on: app.db) + // MUT + let pkg = try await AuthorController.query(on: app.db, owner: "owner") - // MUT - do { - _ = try await AuthorController.query(on: app.db, owner: "owner") - XCTFail("Expected Abort.notFound") - } catch let error as Abort { // validate - XCTAssertEqual(error.status, .notFound) - } catch { - XCTFail("Unexpected error: \(error)") + #expect(pkg.map(\.model.id) == [p.id]) } } - func test_query_sort_alphabetically() async throws { - // setup - for packageName in ["gamma", "alpha", "beta"] { - let p = Package(url: "\(packageName)".url) - try await p.save(on: app.db) + @Test func query_no_version() async throws { + try await withApp { app in + // setup + let p = try await savePackage(on: app.db, "1") try await Repository(package: p, owner: "owner").save(on: app.db) - try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db) + + // MUT + do { + _ = try await AuthorController.query(on: app.db, owner: "owner") + Issue.record("Expected Abort.notFound") + } catch let error as Abort { + // validate + #expect(error.status == .notFound) + } catch { + Issue.record("Unexpected error: \(error)") + } } + } + + @Test func query_sort_alphabetically() async throws { + try await withApp { app in + // setup + for packageName in ["gamma", "alpha", "beta"] { + let p = Package(url: "\(packageName)".url) + try await p.save(on: app.db) + try await Repository(package: p, owner: "owner").save(on: app.db) + try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db) + } - // MUT - let pkg = try await AuthorController.query(on: app.db, owner: "owner") + // MUT + let pkg = try await AuthorController.query(on: app.db, owner: "owner") - // validate - XCTAssertEqual(pkg.map(\.model.url), ["alpha", "beta", "gamma"]) + // validate + #expect(pkg.map(\.model.url) == ["alpha", "beta", "gamma"]) + } } - func test_show_owner() async throws { + @Test func show_owner() async throws { try await withDependencies { $0.environment.dbId = { nil } } operation: { - let p = try await savePackage(on: app.db, "1") - try await Repository(package: p, owner: "owner").save(on: app.db) - try await Version(package: p, latest: .defaultBranch).save(on: app.db) - - // MUT - try await app.test(.GET, "/owner", afterResponse: { response async in - XCTAssertEqual(response.status, .ok) - }) + try await withApp { app in + let p = try await savePackage(on: app.db, "1") + try await Repository(package: p, owner: "owner").save(on: app.db) + try await Version(package: p, latest: .defaultBranch).save(on: app.db) + + // MUT + try await app.test(.GET, "/owner", afterResponse: { response async in + #expect(response.status == .ok) + }) + } } } - func test_show_owner_empty() async throws { + @Test func show_owner_empty() async throws { try await withDependencies { $0.environment.dbId = { nil } } operation: { - let p = try await savePackage(on: app.db, "1") - try await Repository(package: p, owner: "owner").save(on: app.db) - try await Version(package: p, latest: .defaultBranch).save(on: app.db) - - // MUT - try await app.test(.GET, "/fake-owner", afterResponse: { response async in - XCTAssertEqual(response.status, .notFound) - }) + try await withApp { app in + let p = try await savePackage(on: app.db, "1") + try await Repository(package: p, owner: "owner").save(on: app.db) + try await Version(package: p, latest: .defaultBranch).save(on: app.db) + + // MUT + try await app.test(.GET, "/fake-owner", afterResponse: { response async in + #expect(response.status == .notFound) + }) + } } } diff --git a/Tests/AppTests/BadgeTests.swift b/Tests/AppTests/BadgeTests.swift index 031252b58..fd99e1c2a 100644 --- a/Tests/AppTests/BadgeTests.swift +++ b/Tests/AppTests/BadgeTests.swift @@ -14,20 +14,19 @@ @testable import App -import XCTest +import Testing -class BadgeTests: AppTestCase { +@Suite struct BadgeTests { - func test_badgeMessage_swiftVersions() throws { - XCTAssertEqual(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]), "6.0 | 5.10 | 5.9 | 5.8") - XCTAssertNil(Badge.badgeMessage(swiftVersions: [])) + @Test func badgeMessage_swiftVersions() throws { + #expect(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]) == "6.0 | 5.10 | 5.9 | 5.8") + #expect(Badge.badgeMessage(swiftVersions: []) == nil) } - func test_badgeMessage_platforms() throws { - XCTAssertEqual(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]), - "iOS | macOS | Linux") - XCTAssertNil(Badge.badgeMessage(platforms: [])) + @Test func badgeMessage_platforms() throws { + #expect(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]) == "iOS | macOS | Linux") + #expect(Badge.badgeMessage(platforms: []) == nil) } } diff --git a/Tests/AppTests/BlogActionsModelTests.swift b/Tests/AppTests/BlogActionsModelTests.swift index 506d4ea37..4b8064407 100644 --- a/Tests/AppTests/BlogActionsModelTests.swift +++ b/Tests/AppTests/BlogActionsModelTests.swift @@ -12,16 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -import XCTest +import Foundation @testable import App import Dependencies +import Testing -class BlogActionsModelTests: AppTestCase { +@Suite struct BlogActionsModelTests { - func test_init_loadSummaries() async throws { + @Test func init_loadSummaries() async throws { try withDependencies { $0.fileManager.contents = { @Sendable _ in """ @@ -45,19 +46,21 @@ class BlogActionsModelTests: AppTestCase { // MUT let summaries = try BlogActions.Model().summaries - XCTAssertEqual(summaries.count, 2) - XCTAssertEqual(summaries.map(\.slug), ["post-2", "post-1"]) - XCTAssertEqual(summaries.map(\.published), [false, true]) + #expect(summaries.count == 2) + #expect(summaries.map(\.slug) == ["post-2", "post-1"]) + #expect(summaries.map(\.published) == [false, true]) - let firstSummary = try XCTUnwrap(summaries).first + let firstSummary = try #require(summaries).first // Note that we are testing that the first item in this list is the *last* item in the source YAML // as the init should reverse the order of posts so that they display in reverse chronological order - XCTAssertEqual(firstSummary, BlogActions.Model.PostSummary(slug: "post-2", - title: "Blog post title two", - summary: "Summary of blog post two", - publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!, - published: false)) + #expect(firstSummary == BlogActions.Model.PostSummary( + slug: "post-2", + title: "Blog post title two", + summary: "Summary of blog post two", + publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!, + published: false) + ) } try withDependencies { // Ensure prod shows only published summaries @@ -67,13 +70,13 @@ class BlogActionsModelTests: AppTestCase { let summaries = try BlogActions.Model().summaries // validate - XCTAssertEqual(summaries.map(\.slug), ["post-1"]) - XCTAssertEqual(summaries.map(\.published), [true]) + #expect(summaries.map(\.slug) == ["post-1"]) + #expect(summaries.map(\.published) == [true]) } } } - func test_postSummary_postMarkdown_load() async throws { + @Test func postSummary_postMarkdown_load() async throws { withDependencies { $0.fileManager.contents = { @Sendable _ in """ @@ -86,11 +89,11 @@ class BlogActionsModelTests: AppTestCase { // MUT let markdown = summary.postMarkdown - XCTAssertEqual(markdown, "
This is some Markdown with a link and some formatting.
") + #expect(markdown == "This is some Markdown with a link and some formatting.
") } } - func test_decode_posts_yml() async throws { + @Test func decode_posts_yml() async throws { // setup try withDependencies { $0.fileManager.contents = FileManagerClient.liveValue.contents(atPath:) @@ -98,9 +101,9 @@ class BlogActionsModelTests: AppTestCase { } operation: { // MUT let summaries = try BlogActions.Model.allSummaries() - + // validate - XCTAssert(summaries.count > 0) + #expect(summaries.count > 0) } } diff --git a/Tests/AppTests/BuildIndexModelTests.swift b/Tests/AppTests/BuildIndexModelTests.swift index b9153f193..f3e317853 100644 --- a/Tests/AppTests/BuildIndexModelTests.swift +++ b/Tests/AppTests/BuildIndexModelTests.swift @@ -12,38 +12,42 @@ // See the License for the specific language governing permissions and // limitations under the License. +import Foundation + @testable import App import Plot -import XCTVapor +import Testing -class BuildIndexModelTests: AppTestCase { +@Suite struct BuildIndexModelTests { - func test_init_no_name() async throws { + @Test func init_no_name() async throws { // Tests behaviour when we're lacking data - // setup package without package name - let pkg = try await 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 await Version(package: pkg, latest: .defaultBranch).save(on: app.db) - 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) + try await withApp { app in + // setup package without package name + let pkg = try await 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 await Version(package: pkg, latest: .defaultBranch).save(on: app.db) + 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 + #expect(m != nil) + } } - func test_completedBuildCount() throws { + @Test func 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: @@ -58,15 +62,15 @@ class BuildIndexModelTests: AppTestCase { // -> 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) + #expect(m.completedBuildCount == 43) } - func test_packageURL() throws { + @Test func packageURL() throws { let m = BuildIndex.Model.mock - XCTAssertEqual(m.packageURL, "/foo/foobar") + #expect(m.packageURL == "/foo/foobar") } - func test_buildMatrix() throws { + @Test func buildMatrix() throws { // setup let id = UUID() let stable: [BuildInfo] = [ @@ -92,31 +96,26 @@ class BuildIndexModelTests: AppTestCase { let matrix = model.buildMatrix // validate - XCTAssertEqual(matrix.values.keys.count, 27) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.column.label), - ["1.2.3", "2.0.0-b1", "main"] + #expect(matrix.values.keys.count == 27) + #expect( + matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.column.label) == ["1.2.3", "2.0.0-b1", "main"] ) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.value?.status), - .some([.ok, nil, nil]) + #expect( + matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.value?.status) == .some([.ok, nil, nil]) ) - XCTAssertEqual( + #expect( matrix.values[.init(swiftVersion: .v2, - platform: .macosXcodebuild)]?.map(\.value?.status), - [.ok, nil, nil] + platform: .macosXcodebuild)]?.map(\.value?.status) == [.ok, nil, nil] ) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v2, platform: .macosSpm)]?.map(\.value?.status), - [nil, nil, .failed] + #expect( + matrix.values[.init(swiftVersion: .v2, platform: .macosSpm)]?.map(\.value?.status) == [nil, nil, .failed] ) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v1, platform: .tvOS)]?.map(\.value?.status), - [.ok, nil, .ok] + #expect( + matrix.values[.init(swiftVersion: .v1, platform: .tvOS)]?.map(\.value?.status) == [.ok, nil, .ok] ) } - func test_buildMatrix_no_beta() throws { + @Test func buildMatrix_no_beta() throws { // Test BuildMatrix mapping, in particular absence of a beta version // setup let id = UUID() @@ -142,48 +141,43 @@ class BuildIndexModelTests: AppTestCase { let matrix = model.buildMatrix // validate - XCTAssertEqual(matrix.values.keys.count, 27) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.column.label), - ["1.2.3", "main"] + #expect(matrix.values.keys.count == 27) + #expect( + matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.column.label) == ["1.2.3", "main"] ) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.value?.status), - [.ok, nil] + #expect( + matrix.values[.init(swiftVersion: .v3, platform: .iOS)]?.map(\.value?.status) == [.ok, nil] ) - XCTAssertEqual( + #expect( matrix.values[.init(swiftVersion: .v2, - platform: .macosXcodebuild)]?.map(\.value?.status), - [.ok, nil] + platform: .macosXcodebuild)]?.map(\.value?.status) == [.ok, nil] ) - XCTAssertEqual( + #expect( matrix.values[.init(swiftVersion: .v2, - platform: .macosSpm)]?.map(\.value?.status), - [nil, .failed] + platform: .macosSpm)]?.map(\.value?.status) == [nil, .failed] ) - XCTAssertEqual( - matrix.values[.init(swiftVersion: .v1, platform: .tvOS)]?.map(\.value?.status), - [.ok, .ok] + #expect( + matrix.values[.init(swiftVersion: .v1, platform: .tvOS)]?.map(\.value?.status) == [.ok, .ok] ) } - func test_BuildCell() throws { + @Test func render_BuildCell() throws { let id = UUID() - XCTAssertEqual(BuildCell("1.2.3", .release, id, .ok, docStatus: nil).node.render(), """ + #expect(BuildCell("1.2.3", .release, id, .ok, docStatus: nil).node.render() == """ """) - XCTAssertEqual(BuildCell("1.2.3", .release, id, .ok, docStatus: .ok).node.render(), """ + #expect(BuildCell("1.2.3", .release, id, .ok, docStatus: .ok).node.render() == """ """) - XCTAssertEqual(BuildCell("1.2.3", .release, id, .failed, docStatus: nil).node.render(), """ + #expect(BuildCell("1.2.3", .release, id, .failed, docStatus: nil).node.render() == """ """) - XCTAssertEqual(BuildCell("1.2.3", .release).node.render(), """ + #expect(BuildCell("1.2.3", .release).node.render() == """