Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SiteTests #438

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Package.resolved
/.build
/Packages
Tests/IgniteTesting/TestWebsitePackage/Build
Tests/IgniteTesting/TestWebsitePackage/Content/*.md
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
Expand Down
83 changes: 83 additions & 0 deletions Tests/IgniteTesting/Publishing/Site.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// Site.swift
// Ignite
// https://www.github.com/twostraws/Ignite
// See LICENSE for license information.
//

import Foundation
import Testing

@testable import Ignite

/// Tests for the `Site` type.
@Suite("Site Tests", .serialized)
@MainActor
struct SiteTests {

private let package = TestPackage()

@Test("Site published given there is no Markdown content")
func publishingWithNoMarkdownContent() async throws {
let markdownFileURL = package.contentDirectoryURL.appending(path: "story-with-valid-metadata.md")
let markdownContent = """
---
layout: TestStory
lastModified: 2020-03-30 16:37
---

# Story with valid metadata
"""

try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)

try await TestSitePublisher().publish()

#expect(package.checkIndexFileExists() == true)

try FileManager.default.removeItem(at: markdownFileURL)
try FileManager.default.removeItem(at: package.buildDirectoryURL)
}

@Test("Site published when Markdown content contains invalid lastModified date")
func publishingWithInvalidLastModifiedDate() async throws {
let markdownFileURL = package.contentDirectoryURL.appending(path: "story-with-invalid-lastModified.md")
let markdownContent = """
---
layout: TestStory
lastModified: 2020-03-30 16:37:21
---

# Story with invalid lastModified
"""

try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)

try await TestSitePublisher().publish()

#expect(package.checkIndexFileExists() == true)

try FileManager.default.removeItem(at: markdownFileURL)
try FileManager.default.removeItem(at: package.buildDirectoryURL)
}
}

private struct TestPackage {

let packageBaseURL: URL
let buildDirectoryURL: URL
let contentDirectoryURL: URL

init() {
packageBaseURL = URL(filePath: #filePath, directoryHint: .isDirectory)
.deletingLastPathComponent() // "Site.swift"
.deletingLastPathComponent() // "Publishing/"
.appending(path: "TestWebsitePackage")
buildDirectoryURL = packageBaseURL.appending(path: "Build")
contentDirectoryURL = packageBaseURL.appending(path: "Content")
}

func checkIndexFileExists() -> Bool {
(try? buildDirectoryURL.appending(path: "index.html").checkPromisedItemIsReachable()) ?? false
}
}
1 change: 1 addition & 0 deletions Tests/IgniteTesting/TestWebsitePackage/Content/Content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This place is for generated test Markdown files.
2 changes: 2 additions & 0 deletions Tests/IgniteTesting/TestWebsitePackage/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Empty Package.swift file to stub source build directory in tests
// See Publishing/Site.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct TestSite: Site {
contentCount: 20,
image: .init(url: "path/to/image.png", width: 100, height: 100)
)

var contentLayouts: [any ContentLayout] = [
TestStory()
]

init() {}

Expand All @@ -32,11 +36,30 @@ struct TestSite: Site {
}
}

/// An example page used in tests.
/// An example page used in tests.
struct TestLayout: StaticLayout {
var title = "Home"

var body: some HTML {
Text("Hello, World!")
}
}

/// A test publisher for ``TestSite``.
///
/// It helps to run `TestSite/publish` with a correct path of the file that triggered the build.
@MainActor
struct TestSitePublisher {

let site = TestSite()

func publish() async throws {
try await site.publish()
}
}

struct TestStory: ContentLayout {
var body: some HTML {
EmptyHTML()
}
}