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

Increased stability of SiteTests #446

Merged
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
47 changes: 33 additions & 14 deletions Tests/IgniteTesting/Publishing/Site.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@ import Testing
@testable import Ignite

/// Tests for the `Site` type.
///
/// > Warning: Calling `PublishingContext.initialize` as a part of the suite set-up
/// can lead to false positive results because these tests are calling `TestSite/publish`
/// that includes the `PublishingContext.initialize` call.
/// **Workaround:** Run this suite in isolation.
@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")
init() {
try? package.clearBuildFolderAndTestContent()
}

@Test(
"Site published when Markdown content contains invalid lastModified date",
.bug("https://github.com/twostraws/Ignite/issues/445")
)
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
lastModified: 2020-03-30 16:37:21
---

# Story with valid metadata
# Story with invalid lastModified
"""

try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)
Expand All @@ -35,20 +47,19 @@ struct SiteTests {

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

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

@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")
@Test("Site published given Markdown content with valid metadata")
func publishingWithMarkdownContent() async throws {
let markdownFileURL = package.contentDirectoryURL.appending(path: "story-with-valid-metadata.md")
let markdownContent = """
---
layout: TestStory
lastModified: 2020-03-30 16:37:21
lastModified: 2020-03-30 16:37
---

# Story with invalid lastModified
# Story with valid metadata
"""

try markdownContent.write(to: markdownFileURL, atomically: false, encoding: .utf8)
Expand All @@ -57,8 +68,7 @@ struct SiteTests {

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

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

Expand All @@ -80,4 +90,13 @@ private struct TestPackage {
func checkIndexFileExists() -> Bool {
(try? buildDirectoryURL.appending(path: "index.html").checkPromisedItemIsReachable()) ?? false
}

func clearBuildFolderAndTestContent() throws {
try FileManager.default.removeItem(at: buildDirectoryURL)
let enumerator = FileManager.default.enumerator(at: contentDirectoryURL, includingPropertiesForKeys: nil)
while let fileURL = enumerator?.nextObject() as? URL {
guard fileURL.isFileURL, fileURL.pathExtension == "md" else { continue }
try FileManager.default.removeItem(at: fileURL)
}
}
}