Tests/OrgAppTests/ConfigFolderTests.swift

6e76718b67275264fce9c9041e50ec01592edace
orgstar/Tests/OrgAppTests/ConfigFolderTests.swift

113 lines · 5491 bytes

  1import Foundation
  2import OrgCore
  3import Testing
  4@testable import OrgApp
  5
  6@MainActor
  7struct ConfigFolderTests {
  8    func defaults() -> UserDefaults { TestDefaults("orgstar-config-folder") }
  9
 10    func folder() throws -> URL {
 11        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-config-folder-\(UUID().uuidString)")
 12        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
 13        return url
 14    }
 15
 16    @Test func chosenFolderResolves() throws {
 17        let store = defaults()
 18        let url = try folder()
 19        defer { try? FileManager.default.removeItem(at: url) }
 20        #expect(ConfigFolder.resolve(defaults: store) == nil)
 21        ConfigFolder.set(url, defaults: store)
 22        #expect(store.data(forKey: ConfigFolder.bookmarkKey) != nil)
 23        let resolved = try #require(ConfigFolder.resolve(defaults: store))
 24        #expect(resolved.resolvingSymlinksInPath().path == url.resolvingSymlinksInPath().path)
 25        ConfigFolder.clear(defaults: store)
 26        #expect(ConfigFolder.resolve(defaults: store) == nil)
 27    }
 28
 29    @Test func aFolderWithoutABookmarkIsNotKept() {
 30        let store = defaults()
 31        #expect(!ConfigFolder.set(URL(fileURLWithPath: "/nowhere-\(UUID().uuidString)"), defaults: store))
 32        #expect(store.data(forKey: ConfigFolder.bookmarkKey) == nil)
 33    }
 34
 35    /// iOS keeps its folder under the key it always used.
 36    @Test func bookmarkKeyIsUnchanged() {
 37        #expect(ConfigFolder.bookmarkKey == "configFolderBookmark")
 38    }
 39
 40    @Test func suggestedIsInTheRealHome() {
 41        #expect(ConfigFolder.suggested.path == OrgPaths.home + "/.config/orgstar")
 42    }
 43
 44    @Test func movingAppliesTheNewFolder() throws {
 45        let first = try folder()
 46        let second = try folder()
 47        defer {
 48            try? FileManager.default.removeItem(at: first)
 49            try? FileManager.default.removeItem(at: second)
 50        }
 51        try "fill-column = 64\n".write(to: first.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
 52        try "fill-column = 72\n".write(to: second.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
 53        let store = defaults()
 54        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: store)
 55        sync.start(watch: false)
 56        #expect(store.integer(forKey: "fillColumn") == 64)
 57        sync.move(to: second.appendingPathComponent("config.toml"))
 58        #expect(store.integer(forKey: "fillColumn") == 72)
 59        #expect(sync.changedFile?.name == "keymap.toml")
 60        // Changes made in the app go into the new file only.
 61        store.set(80, forKey: "fillColumn")
 62        sync.writeBack()
 63        #expect(try String(contentsOf: second.appendingPathComponent("config.toml"), encoding: .utf8).contains("fill-column = 80"))
 64        let old = try String(contentsOf: first.appendingPathComponent("config.toml"), encoding: .utf8)
 65        #expect(old.contains("fill-column = 64") && !old.contains("fill-column = 80"))
 66    }
 67
 68    @Test func movingToAnEmptyFolderWritesTheCurrentSettings() throws {
 69        let first = try folder()
 70        let second = try folder()
 71        defer {
 72            try? FileManager.default.removeItem(at: first)
 73            try? FileManager.default.removeItem(at: second)
 74        }
 75        try "fill-column = 64\n".write(to: first.appendingPathComponent("config.toml"), atomically: true, encoding: .utf8)
 76        let store = defaults()
 77        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: store)
 78        sync.start(watch: false)
 79        sync.move(to: second.appendingPathComponent("config.toml"))
 80        #expect(try String(contentsOf: second.appendingPathComponent("config.toml"), encoding: .utf8).contains("fill-column = 64"))
 81    }
 82
 83    @Test func readsSnippetsBesideTheFile() throws {
 84        let first = try folder()
 85        let second = try folder()
 86        defer {
 87            try? FileManager.default.removeItem(at: first)
 88            try? FileManager.default.removeItem(at: second)
 89        }
 90        try "[[snippet]]\ntrigger = \"d\"\nbody = \"%t\"\n".write(to: first.appendingPathComponent("snippets.toml"), atomically: true, encoding: .utf8)
 91        try "[[snippet]]\ntrigger = \"d\n".write(to: second.appendingPathComponent("snippets.toml"), atomically: true, encoding: .utf8)
 92        let library = SnippetLibrary()
 93        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: defaults(), snippets: library)
 94        sync.start(watch: false)
 95        #expect(library.snippets.map(\.trigger) == ["d"] && sync.problems.isEmpty)
 96        // A broken file is reported, and the snippets read before stay.
 97        sync.move(to: second.appendingPathComponent("config.toml"))
 98        #expect(library.snippets.map(\.trigger) == ["d"])
 99        #expect(sync.problems == ["snippets.toml line 2: unclosed string"])
100    }
101
102    @Test func movingBeforeStartDoesNothing() throws {
103        let first = try folder()
104        let second = try folder()
105        defer {
106            try? FileManager.default.removeItem(at: first)
107            try? FileManager.default.removeItem(at: second)
108        }
109        let sync = ConfigSync(url: first.appendingPathComponent("config.toml"), defaults: defaults())
110        sync.move(to: second.appendingPathComponent("config.toml"))
111        #expect(!FileManager.default.fileExists(atPath: second.appendingPathComponent("config.toml").path))
112    }
113}
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