Skip to content

Commit 065b61d

Browse files
authored
Merge pull request #2866 from SwiftPackageIndex/extend-tests
Extend tests
2 parents fea47c1 + c5e5d3c commit 065b61d

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

Sources/App/Controllers/BlogController.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ import Plot
1818
enum BlogController {
1919

2020
static func index(req: Request) async throws -> HTML {
21-
try BlogActions.Index.View(path: req.url.path,
22-
model: BlogActions.Model()).document()
21+
BlogActions.Index.View(path: req.url.path, model: BlogActions.Model()).document()
2322
}
2423

2524
static func indexFeed(req: Request) async throws -> RSS {
26-
let model = try BlogActions.Model()
25+
let model = BlogActions.Model()
2726
let items = model.summaries.map { summary -> Node <RSS.ChannelContext> in
2827
.item(
2928
.guid(
@@ -59,7 +58,7 @@ enum BlogController {
5958
guard let slug = req.parameters.get("slug")
6059
else { throw Abort(.notFound) }
6160

62-
let model = try BlogActions.Model()
61+
let model = BlogActions.Model()
6362
if let summary = model.summaries.first(where: { $0.slug == slug }) {
6463
return BlogActions.Show.View(path: req.url.path,
6564
model: summary).document()

Sources/App/Views/Blog/BlogActions+Model.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ enum BlogActions {
2121

2222
struct Model {
2323

24+
static var blogIndexYmlPath: String {
25+
Current.fileManager.workingDirectory().appending("Resources/Blog/posts.yml")
26+
}
27+
2428
struct PostSummary: Equatable {
2529
var slug: String
2630
var title: String
@@ -32,16 +36,8 @@ enum BlogActions {
3236
var summaries: [PostSummary]
3337

3438
init() {
35-
let blogIndexYmlPath = Current.fileManager.workingDirectory()
36-
.appending("Resources/Blog/posts.yml")
3739
do {
38-
let allSummaries = if let ymlData = Current.fileManager.contents(atPath: blogIndexYmlPath),
39-
let ymlString = String(data: ymlData, encoding: .utf8) {
40-
// Post order should be as exists in the file, but reversed.
41-
try Array(YAMLDecoder().decode([PostSummary].self, from: ymlString).reversed())
42-
} else {
43-
Array<PostSummary>()
44-
}
40+
let allSummaries = try Self.allSummaries()
4541

4642
summaries = if Current.environment() == .production {
4743
// Only "published" posts show in production.
@@ -66,6 +62,17 @@ enum BlogActions {
6662
latest features, our efforts in the community, and any other updates that affect the site.
6763
"""
6864
}
65+
66+
static func allSummaries() throws -> [PostSummary] {
67+
guard let data = Current.fileManager.contents(atPath: Self.blogIndexYmlPath) else {
68+
throw AppError.genericError(nil, "failed to read posts.yml")
69+
}
70+
71+
// Post order should be as exists in the file, but reversed.
72+
return try YAMLDecoder().decode([PostSummary].self, from: String(decoding: data, as: UTF8.self))
73+
.reversed()
74+
}
75+
6976
}
7077
}
7178

Tests/AppTests/BlogActionsModelTests.swift

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,37 @@ class BlogActionsModelTests: AppTestCase {
3434
""".data(using: .utf8)
3535
}
3636

37-
// MUT
38-
let summaries = try BlogActions.Model().summaries
39-
40-
XCTAssertEqual(summaries.count, 2)
41-
let firstSummary = try XCTUnwrap(summaries).first
42-
43-
// Note that we are testing that the first item in this list is the *last* item in the source YAML
44-
// as the init should reverse the order of posts so that they display in reverse chronological order
45-
XCTAssertEqual(firstSummary, BlogActions.Model.PostSummary(slug: "post-2",
46-
title: "Blog post title two",
47-
summary: "Summary of blog post two",
48-
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
49-
published: false))
37+
do { // Ensure dev shows all summaries
38+
Current.environment = { .development }
39+
40+
// MUT
41+
let summaries = BlogActions.Model().summaries
42+
43+
XCTAssertEqual(summaries.count, 2)
44+
XCTAssertEqual(summaries.map(\.slug), ["post-2", "post-1"])
45+
XCTAssertEqual(summaries.map(\.published), [false, true])
46+
47+
let firstSummary = try XCTUnwrap(summaries).first
48+
49+
// Note that we are testing that the first item in this list is the *last* item in the source YAML
50+
// as the init should reverse the order of posts so that they display in reverse chronological order
51+
XCTAssertEqual(firstSummary, BlogActions.Model.PostSummary(slug: "post-2",
52+
title: "Blog post title two",
53+
summary: "Summary of blog post two",
54+
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
55+
published: false))
56+
}
57+
58+
do { // Ensure prod shows only published summaries
59+
Current.environment = { .production }
60+
61+
// MUT
62+
let summaries = BlogActions.Model().summaries
63+
64+
// validate
65+
XCTAssertEqual(summaries.map(\.slug), ["post-1"])
66+
XCTAssertEqual(summaries.map(\.published), [true])
67+
}
5068
}
5169

5270
func test_postSummary_postMarkdown_load() async throws {
@@ -63,4 +81,15 @@ class BlogActionsModelTests: AppTestCase {
6381
XCTAssertEqual(markdown, "<p>This is some Markdown with <a href=\"https://example.com\">a link</a> and some <em>formatting</em>.</p>")
6482
}
6583

84+
func test_decode_posts_yml() async throws {
85+
// setup
86+
Current.fileManager = .live
87+
88+
// MUT
89+
let summaries = try BlogActions.Model.allSummaries()
90+
91+
// validate
92+
XCTAssert(summaries.count > 0)
93+
}
94+
6695
}

0 commit comments

Comments
 (0)