Tests/OrgAppTests/PublishTests.swift
156 lines · 8202 bytes
1import Foundation
2import Testing
3@testable import OrgApp
4import OrgCore
5
6/// `org-publish` against Emacs: sitemaps and the files published.
7struct PublishTests {
8 static let files: [(String, String, TimeInterval)] = [
9 ("a.org", "#+TITLE: Zeta page\n#+DATE: <2026-01-05 Mon>\n* H\nText [[file:sub/b.org][b]].\n", 1_780_000_000),
10 ("b2.org", "#+TITLE: Alpha\n", 1_780_000_300),
11 ("sub/b.org", "#+TITLE: Beta\n#+TITLE: two\n* B\n", 1_780_000_200),
12 ("sub/deep/c.org", "no title\n", 1_780_000_100),
13 ("other/z.org", "#+TITLE: alpha lower\n", 1_780_000_400),
14 ("draft-x.org", "#+TITLE: Draft\n", 1_780_000_500),
15 ("style.css", "body{}", 1_780_000_600),
16 ("img/logo.png", "png", 1_780_000_700),
17 (".hidden.org", "#+TITLE: Hidden\n", 1_780_000_800),
18 ]
19
20 static func tree(_ folder: URL) throws {
21 for (path, text, time) in files {
22 let url = folder.appendingPathComponent("src").appendingPathComponent(path)
23 try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
24 try text.write(to: url, atomically: true, encoding: .utf8)
25 try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSince1970: time)], ofItemAtPath: url.path)
26 }
27 }
28
29 /// Projects as config.toml writes them and as Lisp, over `folder`.
30 static func projects(_ folder: String) -> (toml: String, lisp: String) {
31 let toml = """
32 fill-column = 80
33
34 [[publish]]
35 name = "site"
36 base-directory = "\(folder)/src"
37 publishing-directory = "\(folder)/out"
38 recursive = true
39 exclude = "^draft-"
40 auto-sitemap = true
41 sitemap-title = "Site"
42
43 [[publish]]
44 name = "list"
45 base-directory = "\(folder)/src"
46 publishing-directory = "\(folder)/out2"
47 recursive = true
48 auto-sitemap = true
49 sitemap-filename = "index.org"
50 sitemap-style = "list"
51 sitemap-sort-folders = "first"
52
53 [[publish]]
54 name = "dated"
55 base-directory = "\(folder)/src"
56 publishing-directory = "\(folder)/out3"
57 include = "sub/b.org"
58 auto-sitemap = true
59 sitemap-filename = "dated.org"
60 sitemap-style = "list"
61 sitemap-sort-files = "anti-chronologically"
62 publishing-function = "org-md-publish-to-md"
63
64 [[publish]]
65 name = "static"
66 base-directory = "\(folder)/src"
67 base-extension = "css\\\\|png"
68 recursive = true
69 publishing-directory = "\(folder)/out"
70 publishing-function = "org-publish-attachment"
71
72 [[publish]]
73 name = "all"
74 components = "site list dated static"
75 """
76 let lisp = """
77 '(("site" :base-directory "\(folder)/src" :publishing-directory "\(folder)/out" :recursive t :exclude "^draft-" :auto-sitemap t :sitemap-title "Site")
78 ("list" :base-directory "\(folder)/src" :publishing-directory "\(folder)/out2" :recursive t :auto-sitemap t :sitemap-filename "index.org" :sitemap-style list :sitemap-sort-folders first)
79 ("dated" :base-directory "\(folder)/src" :publishing-directory "\(folder)/out3" :include ("sub/b.org") :auto-sitemap t :sitemap-filename "dated.org" :sitemap-style list :sitemap-sort-files anti-chronologically :publishing-function org-md-publish-to-md)
80 ("static" :base-directory "\(folder)/src" :base-extension "css\\\\|png" :recursive t :publishing-directory "\(folder)/out" :publishing-function org-publish-attachment)
81 ("all" :components ("site" "list" "dated" "static")))
82 """
83 return (toml, lisp)
84 }
85
86 static func listing(_ folder: URL) -> [String] {
87 let base = folder.path + "/"
88 let enumerator = FileManager.default.enumerator(atPath: folder.path)
89 var out: [String] = []
90 while let path = enumerator?.nextObject() as? String {
91 var isDirectory: ObjCBool = false
92 if FileManager.default.fileExists(atPath: base + path, isDirectory: &isDirectory), !isDirectory.boolValue, !path.hasPrefix("ts/") { out.append(path) }
93 }
94 return out.sorted()
95 }
96
97 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
98 func publishesLikeOrg() throws {
99 let emacsFolder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-e-\(UUID().uuidString)").resolvingSymlinksInPath()
100 let ourFolder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-o-\(UUID().uuidString)").resolvingSymlinksInPath()
101 defer {
102 try? FileManager.default.removeItem(at: emacsFolder)
103 try? FileManager.default.removeItem(at: ourFolder)
104 }
105 try Self.tree(emacsFolder)
106 try Self.tree(ourFolder)
107 let form = """
108 (progn
109 (require 'ox-publish) (require 'ox-md)
110 (setq org-publish-timestamp-directory "\(emacsFolder.path)/ts/"
111 org-publish-project-alist \(Self.projects(emacsFolder.path).lisp))
112 (org-publish "all" t))
113 """
114 let process = Process()
115 process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
116 process.arguments = [ProcessInfo.processInfo.environment["ORGSTAR_EMACS"] ?? "emacs", "-Q", "--batch", "--eval", form]
117 process.standardOutput = FileHandle.nullDevice
118 let errors = Pipe()
119 process.standardError = errors
120 try process.run()
121 let log = errors.fileHandleForReading.readDataToEndOfFile()
122 process.waitUntilExit()
123 #expect(process.terminationStatus == 0, "\(String(decoding: log, as: UTF8.self))")
124
125 let loaded = Publish.projects(fromConfig: Self.projects(ourFolder.path).toml, root: nil)
126 #expect(loaded.problems.isEmpty, "\(loaded.problems)")
127 let read = { (path: String) in try? String(contentsOfFile: path, encoding: .utf8) }
128 let all = loaded.projects.first { $0.name == "all" }!
129 let report = try Publish.publish([all], all: loaded.projects, force: true, read: read) { function, text, _, _ in function + "\n" + text }
130 #expect(!report.published.isEmpty)
131 for name in ["sitemap.org", "index.org", "dated.org"] {
132 let theirs = try String(contentsOf: emacsFolder.appendingPathComponent("src/" + name), encoding: .utf8)
133 let ours = try String(contentsOf: ourFolder.appendingPathComponent("src/" + name), encoding: .utf8)
134 #expect(ours == theirs, "\(name)\nours:\n\(ours)\nemacs:\n\(theirs)")
135 }
136 #expect(Self.listing(ourFolder) == Self.listing(emacsFolder))
137 }
138
139 @Test func skipsUpToDateFilesAndFindsProjects() throws {
140 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-\(UUID().uuidString)").resolvingSymlinksInPath()
141 defer { try? FileManager.default.removeItem(at: folder) }
142 try Self.tree(folder)
143 let loaded = Publish.projects(fromConfig: Self.projects(folder.path).toml, root: nil)
144 let site = loaded.projects.first { $0.name == "site" }!
145 let read = { (path: String) in try? String(contentsOfFile: path, encoding: .utf8) }
146 let first = try Publish.publish([site], all: loaded.projects, read: read) { _, text, _, _ in text }
147 let second = try Publish.publish([site], all: loaded.projects, read: read) { _, text, _, _ in text }
148 #expect(!first.published.isEmpty && second.published.isEmpty && second.skipped.count == first.published.count)
149 let a = folder.path + "/src/a.org"
150 #expect(Publish.projects(containing: a, in: loaded.projects).map(\.name) == ["site", "list", "dated"])
151 let one = try Publish.publish([site], all: loaded.projects, force: true, only: a, read: read) { _, text, _, _ in text }
152 #expect(one.published == [a])
153 #expect(Publish.projects(fromConfig: "[[publish]]\nname = \"x\"\n", root: nil).problems.count == 1)
154 #expect(throws: Publish.Failure.self) { try Publish.expand([PublishProject(name: "m")].map { var p = $0; p.components = ["nope"]; return p }, all: []) }
155 }
156}