Tests/OrgAppTests/FileLintTests.swift

9f1d5f334af860e431f3a5be98720439b45d7d0a
orgstar/Tests/OrgAppTests/FileLintTests.swift

126 lines · 7023 bytes

13 symbols in this file
  1import Foundation
  2import OrgCore
  3@testable import OrgIndex
  4import Testing
  5@testable import OrgApp
  6
  7struct FileLintTests {
  8    @Test func idsResolveThroughTheIndexAndItsOverlay() async throws {
  9        let store = try IndexStore()
 10        try store.write(FileRecord(path: "/notes/a.org", root: "/notes", kind: .org, bytes: Array("* A\n:PROPERTIES:\n:ID: in-a\n:END:\n".utf8), mtime: 1, settings: IndexSettings()))
 11        // An unsaved buffer of b.org, in place of what the index has.
 12        let overlay = ["/notes/b.org": FileRecord(path: "/notes/b.org", root: "/notes", kind: .org, bytes: Array("* B\n:PROPERTIES:\n:ID: in-b\n:END:\n".utf8),
 13                                                  mtime: 2, settings: IndexSettings())]
 14        let text = "* Here\n:PROPERTIES:\n:ID: in-a\n:END:\n[[id:in-a]] [[id:in-b]]\n[[id:nowhere]]\n"
 15        let environment = FileLint.environment(index: store, overlay: overlay)
 16        let findings = await FileLint.lint(text, tree: OrgParser.parse(text), path: "/notes/here.org", environment: environment)
 17        #expect(findings.map(\.checker) == [.duplicateID, .invalidIDLink])
 18        #expect(findings.map(\.line) == [3, 6])
 19        #expect(findings.first?.message == "Duplicate ID \"in-a\", also in a.org")
 20    }
 21
 22    @Test func settingsComeFromThePreferences() {
 23        let defaults = TestDefaults("FileLintTests")
 24        defaults.set("attach/", forKey: "attachIDDir")
 25        defaults.set("t", forKey: "attachUseInheritance")
 26        defaults.set("secret", forKey: "cryptTagMatcher")
 27        let environment = FileLint.environment(index: nil, defaults: defaults)
 28        #expect(environment.attachIDDir == "attach/")
 29        #expect(environment.attachInherit == true)
 30        #expect(environment.cryptTag == "secret")
 31        #expect(environment.idFiles == nil)
 32    }
 33
 34    @Test func appliesToOrgFiles() {
 35        #expect(FileLint.applies(to: "/n/a.org"))
 36        #expect(FileLint.applies(to: "/n/a.org_archive"))
 37        #expect(FileLint.applies(to: "/n/a.org.gpg"))
 38        #expect(!FileLint.applies(to: "/n/a.md"))
 39        #expect(!FileLint.applies(to: nil))
 40    }
 41
 42    @Test func summaries() {
 43        #expect(FileLint.summary([]) == "No problems found")
 44        let finding = LintFinding(offset: 0, line: 1, checker: .priority, message: "x")
 45        #expect(FileLint.summary([finding, finding]) == "2 problems")
 46        #expect(FileLint.severity(finding) == "Error")
 47        #expect(FileLint.severity(LintFinding(offset: 0, line: 1, checker: .timestampSyntax, message: "x")) == "Warning")
 48    }
 49
 50    @Test func allFoldersGroupsByFileAndUsesOpenBuffers() async throws {
 51        let notes = try Folder()
 52        let a = try notes.write("a.org", "* A\n[[*Nowhere]]\n")
 53        let b = try notes.write("b.org", "* B\n")
 54        let c = try notes.write("sub/c.org", "* C\n[fn:1] unused\n")
 55        let paths = [a.path, b.path, c.path]
 56        let files = try await FileLint.lint(paths: paths, texts: [b.path: "* B\n#+BEGIN_SRC\n#+END_SRC\n"], environment: FileLint.environment(index: nil))
 57        #expect(files.map(\.path) == paths)
 58        #expect(files.map { $0.findings.map(\.checker) } == [[.invalidFuzzyLink], [.missingLanguageInSrcBlock], [.unreferencedFootnoteDefinition]])
 59        #expect(files[0].findings.first?.line == 2)
 60    }
 61
 62    @Test func orphanedAttachmentFoldersAreReportedOnce() async throws {
 63        let notes = try Folder()
 64        let store = try IndexStore()
 65        var paths: [String] = []
 66        for name in ["a.org", "b.org", "c.org"] {
 67            let url = try notes.write(name, "* \(name)\n")
 68            try store.write(FileRecord(path: url.path, root: notes.url.path, kind: .org, bytes: Array("* \(name)\n".utf8), mtime: 1, settings: IndexSettings()))
 69            paths.append(url.path)
 70        }
 71        try FileManager.default.createDirectory(at: notes.url.appendingPathComponent("data/ab/cdef"), withIntermediateDirectories: true)
 72        try FileManager.default.createDirectory(at: notes.url.appendingPathComponent("data/12/3456"), withIntermediateDirectories: true)
 73        // Each file alone lists both folders.
 74        let one = await FileLint.lint("* a\n", tree: OrgParser.parse("* a\n"), path: paths[1], environment: FileLint.environment(index: store))
 75        #expect(one.filter { $0.checker == .orphanedAttachmentFolder }.count == 2)
 76        let files = try await FileLint.lint(paths: paths, texts: [:], environment: FileLint.environment(index: store))
 77        #expect(files.map(\.path) == [paths[0]])
 78        #expect(files[0].findings.map(\.message) == ["Attachment folder \"data/12/3456\" belongs to no entry", "Attachment folder \"data/ab/cdef\" belongs to no entry"])
 79    }
 80
 81    @Test func allFoldersLeavesOutEncryptedFiles() {
 82        let files = [
 83            FileEntry(path: "/n/b.org", root: "/n", kind: .org), FileEntry(path: "/n/a.org.gpg", root: "/n", kind: .org),
 84            FileEntry(path: "/n/a.org", root: "/n", kind: .org), FileEntry(path: "/n/old.org_archive", root: "/n", kind: .archive),
 85            FileEntry(path: "/n/a.sync-conflict-1.org", root: "/n", kind: .conflict), FileEntry(path: "/n/x.md", root: "/n", kind: .org),
 86            FileEntry(path: "/n/p.png", root: "/n", kind: .other)
 87        ]
 88        #expect(FileLint.folderPaths(files) == ["/n/a.org", "/n/b.org", "/n/old.org_archive"])
 89    }
 90
 91    @Test func allFoldersNamesFilesBelowTheirFolder() {
 92        let one = [FileEntry(path: "/n/a.org", root: "/n", kind: .org), FileEntry(path: "/n/sub/b.org", root: "/n", kind: .org)]
 93        #expect(FileLint.names(one) == ["/n/a.org": "a.org", "/n/sub/b.org": "sub/b.org"])
 94        let two = one + [FileEntry(path: "/m/a.org", root: "/m", kind: .org)]
 95        #expect(FileLint.names(two) == ["/n/a.org": "n/a.org", "/n/sub/b.org": "n/sub/b.org", "/m/a.org": "m/a.org"])
 96    }
 97
 98    @Test func allFoldersSkipsEncryptedEntries() async throws {
 99        let notes = try Folder()
100        let text = "* Secret :crypt:\n-----BEGIN PGP MESSAGE-----\n\n[[*Nowhere]]\n-----END PGP MESSAGE-----\n* Open\n[[*Nowhere]]\n"
101        let a = try notes.write("a.org", text)
102        let files = try await FileLint.lint(paths: [a.path], texts: [:], environment: FileLint.environment(index: nil))
103        #expect(files.first?.findings.map(\.line) == [7])
104    }
105
106    @Test func allFoldersStopsWhenCancelled() async throws {
107        let notes = try Folder()
108        let paths = try (0..<100).map { try notes.write("n\($0).org", "* N\n[[*Nowhere]]\n").path }
109        let reports = Counter()
110        let task = Task {
111            try await FileLint.lint(paths: paths, texts: [:], environment: FileLint.environment(index: nil)) { _ in
112                await reports.add()
113                try? await Task.sleep(for: .seconds(30))
114            }
115        }
116        try await Task.sleep(for: .milliseconds(100))
117        task.cancel()
118        await #expect(throws: CancellationError.self) { try await task.value }
119        #expect(await reports.count <= 1)
120    }
121}
122
123private actor Counter {
124    var count = 0
125    func add() { count += 1 }
126}
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