Tests/OrgAppTests/ConfigFolderTests.swift

2b4bbb38252d0a3d7b97adde900413f734f0506b
orgstar/Tests/OrgAppTests/ConfigFolderTests.swift

93 lines · 4277 bytes

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