Skip to content

Commit d123cfa

Browse files
committed
Add SiteTests
1 parent 490ba14 commit d123cfa

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Package.resolved
33
/.build
44
/Packages
5+
Tests/IgniteTesting/TestWebsitePackage/Build
6+
Tests/IgniteTesting/TestWebsitePackage/Content/*.md
57
xcuserdata/
68
DerivedData/
79
.swiftpm/configuration/registries.json
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Site.swift
3+
// Ignite
4+
// https://www.github.com/twostraws/Ignite
5+
// See LICENSE for license information.
6+
//
7+
8+
import Foundation
9+
import Testing
10+
11+
@testable import Ignite
12+
13+
/// Tests for the `Site` type.
14+
@Suite("Site Tests", .serialized)
15+
@MainActor
16+
struct SiteTests {
17+
18+
private let package = TestPackage()
19+
20+
@Test("Site published given there is no Markdown content")
21+
func publishingWithNoMarkdownContent() async throws {
22+
let markdownFileURL = package.contentDirectoryURL.appending(path: "story-with-valid-metadata.md")
23+
let markdownContent = """
24+
---
25+
layout: TestStory
26+
lastModified: 2020-03-30 16:37
27+
---
28+
29+
# Story with valid metadata
30+
"""
31+
32+
try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)
33+
34+
try await TestSitePublisher().publish()
35+
36+
#expect(package.checkIndexFileExists() == true)
37+
38+
try FileManager.default.removeItem(at: markdownFileURL)
39+
try FileManager.default.removeItem(at: package.buildDirectoryURL)
40+
}
41+
42+
@Test("Site published when Markdown content contains invalid lastModified date")
43+
func publishingWithInvalidLastModifiedDate() async throws {
44+
let markdownFileURL = package.contentDirectoryURL.appending(path: "story-with-invalid-lastModified.md")
45+
let markdownContent = """
46+
---
47+
layout: TestStory
48+
lastModified: 2020-03-30 16:37:21
49+
---
50+
51+
# Story with invalid lastModified
52+
"""
53+
54+
try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)
55+
56+
try await TestSitePublisher().publish()
57+
58+
#expect(package.checkIndexFileExists() == true)
59+
60+
try FileManager.default.removeItem(at: markdownFileURL)
61+
try FileManager.default.removeItem(at: package.buildDirectoryURL)
62+
}
63+
}
64+
65+
private struct TestPackage {
66+
67+
let packageBaseURL: URL
68+
let buildDirectoryURL: URL
69+
let contentDirectoryURL: URL
70+
71+
init() {
72+
packageBaseURL = URL(filePath: #filePath, directoryHint: .isDirectory)
73+
.deletingLastPathComponent() // "Site.swift"
74+
.deletingLastPathComponent() // "Publishing/"
75+
.appending(path: "TestWebsitePackage")
76+
buildDirectoryURL = packageBaseURL.appending(path: "Build")
77+
contentDirectoryURL = packageBaseURL.appending(path: "Content")
78+
}
79+
80+
func checkIndexFileExists() -> Bool {
81+
(try? buildDirectoryURL.appending(path: "index.html").checkPromisedItemIsReachable()) ?? false
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This place is for generated test Markdown files.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Empty Package.swift file to stub source build directory in tests
2+
// See Publishing/Site.swift

Tests/IgniteTesting/TestSite.swift Tests/IgniteTesting/TestWebsitePackage/Sources/TestSite.swift

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct TestSite: Site {
2424
contentCount: 20,
2525
image: .init(url: "path/to/image.png", width: 100, height: 100)
2626
)
27+
28+
var contentLayouts: [any ContentLayout] = [
29+
TestStory()
30+
]
2731

2832
init() {}
2933

@@ -32,11 +36,30 @@ struct TestSite: Site {
3236
}
3337
}
3438

35-
/// An example page used in tests.
39+
/// An example page used in tests.
3640
struct TestLayout: StaticLayout {
3741
var title = "Home"
3842

3943
var body: some HTML {
4044
Text("Hello, World!")
4145
}
4246
}
47+
48+
/// A test publisher for ``TestSite``.
49+
///
50+
/// It helps to run `TestSite/publish` with a correct path of the file that triggered the build.
51+
@MainActor
52+
struct TestSitePublisher {
53+
54+
let site = TestSite()
55+
56+
func publish() async throws {
57+
try await site.publish()
58+
}
59+
}
60+
61+
struct TestStory: ContentLayout {
62+
var body: some HTML {
63+
EmptyHTML()
64+
}
65+
}

0 commit comments

Comments
 (0)