Skip to content

Commit

Permalink
Merge pull request #438 from erikbasargin/fix-site-publish-when-conte…
Browse files Browse the repository at this point in the history
…nt-with-invalid-lastModified

Add SiteTests
  • Loading branch information
JPToroDev authored Jan 30, 2025
2 parents 5ce9e12 + d123cfa commit 88ce42f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
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()
}
}

0 comments on commit 88ce42f

Please sign in to comment.