|
| 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 | +} |
0 commit comments