Tests/OrgCoreTests/SetupFileTests.swift

9879b1b41ae2b5d87a110fc372f6f4b88f846096
orgstar/Tests/OrgCoreTests/SetupFileTests.swift

91 lines · 4878 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// `#+SETUPFILE` keywords, nested setup files included, as Org reads them.
 6struct SetupFileTests {
 7    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 8
 9    static let common = """
10        #+TODO: NEXT WAIT | DONE CANCELLED
11        #+PRIORITIES: A E C
12        #+TAGS: work home
13        #+FILETAGS: :shared:
14        #+PROPERTY: owner alice
15        #+PROPERTY: header-args :results silent
16        #+TITLE: Setup Title
17        #+OPTIONS: toc:nil num:nil
18        #+MACRO: who Alice
19        #+SETUPFILE: ../deep.org
20        #+SETUPFILE: ../loop.org
21        """ + "\n"
22    static let deep = "#+TYP_TODO: BUG | FIXED\n#+SETUPFILE: setup/common.org\n"
23    static let main = """
24        #+SETUPFILE: setup/common.org
25
26        * NEXT [#E] Task :local:
27        {{{who}}}
28        * BUG Other
29        """ + "\n"
30
31    @Test func collectsNestedKeywordLines() {
32        let files = ["/w/setup/common.org": Self.common, "/w/deep.org": Self.deep]
33        let lines = SetupFile.keywordLines(in: Self.main, directory: "/w") { files[$0] }
34        #expect(lines.hasPrefix("#+TODO: NEXT WAIT | DONE CANCELLED\n"))
35        #expect(lines.contains("#+TYP_TODO: BUG | FIXED\n#+TITLE") == false)
36        #expect(lines.contains("#+TYP_TODO: BUG | FIXED\n"))
37        #expect(lines.components(separatedBy: "#+TODO:").count == 2)
38        #expect(SetupFile.keywordLines(in: "* x\n", directory: "/w") { files[$0] } == "")
39        #expect(SetupFile.keywordLines(in: "#+SETUPFILE: https://example.com/a.org\n", directory: "/w") { _ in "#+TODO: X\n" } == "")
40    }
41
42    @Test(.enabled(if: oracle))
43    func matchesOrg() throws {
44        let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-setup-\(UUID().uuidString)")
45        try FileManager.default.createDirectory(at: folder.appendingPathComponent("setup"), withIntermediateDirectories: true)
46        defer { try? FileManager.default.removeItem(at: folder) }
47        try Self.common.write(to: folder.appendingPathComponent("setup/common.org"), atomically: true, encoding: .utf8)
48        try Self.deep.write(to: folder.appendingPathComponent("deep.org"), atomically: true, encoding: .utf8)
49        let path = folder.appendingPathComponent("main.org").path
50        try Self.main.write(toFile: path, atomically: true, encoding: .utf8)
51
52        let emacs = try EmacsOracle.evaluate("", """
53            (with-current-buffer (find-file-noselect \(lispString(path)))
54              (require 'ox)
55              (goto-char (point-min))
56              (re-search-forward "^\\\\* NEXT")
57              (let ((env (org-export-get-environment)))
58                (list (mapconcat #'identity org-todo-keywords-1 " ")
59                      (format "%c %c %c" org-priority-highest org-priority-lowest org-priority-default)
60                      (mapconcat #'identity (org-get-tags) " ")
61                      (or (cdr (assoc "owner" org-keyword-properties)) "")
62                      (mapconcat #'identity (delq nil (mapcar (lambda (x) (and (stringp (car x)) (car x))) org-current-tag-alist)) " ")
63                      (org-element-interpret-data (plist-get env :title))
64                      (format "%s" (plist-get env :with-toc))
65                      (progn (org-macro-initialize-templates) (or (cdr (assoc "who" org-macro-templates)) "")))))
66            """)
67
68        let settings = OrgSettings.default.resolvingSetupFiles(in: Self.main, path: path, read: OrgSettings.readFile)
69        let tree = OrgParser.parse(Self.main, defaults: settings)
70        let model = DocumentModel(tree: tree)
71        let task = try #require(model.headings.firstIndex { $0.title.contains("Task") })
72        let priorities = tree.settings.priorities
73        let tagNames = (FastSelection.tagTable(settings.setupKeywords + Self.main) ?? []).compactMap { item -> String? in
74            if case .option(_, let name) = item { name } else { nil }
75        }
76        let options = ExportOptions.parse(settings.setupKeywords + Self.main)
77        let page = HTMLExport.page(Self.main, fallbackTitle: "main", settings: settings, path: path)
78        let ours = [
79            tree.settings.todoSequences.flatMap { ($0.active + $0.done).map(\.name) }.joined(separator: " "),
80            "\(priorities.highest) \(priorities.lowest) \(priorities.default)",
81            model.tags(of: task).map(\.value).joined(separator: " "),
82            model.property("owner", of: nil)?.value ?? "",
83            tagNames.joined(separator: " "),
84            HTMLExport.metadata(Self.main, settings: settings).title ?? "",
85            options.toc == nil ? "nil" : "\(options.toc!)",
86            page.contains("Alice") ? "Alice" : "",
87        ]
88        #expect(ours == emacs)
89        #expect(page.contains("<h1 class=\"title\">Setup Title</h1>") && !page.contains("id=\"table-of-contents\""))
90    }
91}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form