Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import Foundationimport ParcelConfigimport Testing
@Suitestruct ConfigDefaultFileTests { @Test func starterHasEditorHintAndUsesModelDefaults() async throws { #expect(ConfigStore.defaultFileContents.hasPrefix("#:schema \(ConfigJSONSchema.publishedURL)\n")) let snapshot = await ConfigStore().load(source: ConfigStore.defaultFileContents) #expect(snapshot.activeConfiguration == .defaults) #expect(snapshot.loadState == .loaded) #expect(snapshot.diagnostics.isEmpty) }
@Test func createsParentDirectoriesAndLoadsTheFile() async throws { try await withDirectory { directory in let url = directory.appendingPathComponent("nested/parcel.toml") let store = ConfigStore(fileURL: url) try await store.createDefaultFileIfMissing() #expect(try String(contentsOf: url, encoding: .utf8) == ConfigStore.defaultFileContents) let snapshot = await store.reload() #expect(snapshot.loadState == .loaded) #expect(snapshot.activeSource == .file(url)) #expect(snapshot.activeConfiguration == .defaults) #expect(snapshot.diagnostics.isEmpty) } }
@Test(arguments: ["", "# My settings\n[layout]\nratio = 0.6\n", "broken = [", "\u{FEFF}# café\n"]) func neverOverwritesAnExistingFile(source: String) async throws { try await withDirectory { directory in let url = directory.appendingPathComponent("parcel.toml") let original = Data(source.utf8) try original.write(to: url) try await ConfigStore(fileURL: url).createDefaultFileIfMissing() #expect(try Data(contentsOf: url) == original) } }
@Test func preservesAnExistingSymlink() async throws { try await withDirectory { directory in let target = directory.appendingPathComponent("actual.toml") let link = directory.appendingPathComponent("parcel.toml") try Data("# Custom\n".utf8).write(to: target) try FileManager.default.createSymbolicLink(at: link, withDestinationURL: target) try await ConfigStore(fileURL: link).createDefaultFileIfMissing() #expect(try FileManager.default.destinationOfSymbolicLink(atPath: link.path) == target.path) #expect(try String(contentsOf: target, encoding: .utf8) == "# Custom\n") } }
@Test func danglingSymlinkIsNotReplaced() async throws { try await withDirectory { directory in let target = directory.appendingPathComponent("missing.toml") let link = directory.appendingPathComponent("parcel.toml") try FileManager.default.createSymbolicLink(at: link, withDestinationURL: target) await #expect(throws: (any Error).self) { try await ConfigStore(fileURL: link).createDefaultFileIfMissing() } #expect(try FileManager.default.destinationOfSymbolicLink(atPath: link.path) == target.path) #expect(!FileManager.default.fileExists(atPath: target.path)) } }
@Test func reportsUnwritablePaths() async throws { try await withDirectory { directory in let parentFile = directory.appendingPathComponent("parent") try Data().write(to: parentFile) await #expect(throws: (any Error).self) { try await ConfigStore(fileURL: parentFile.appendingPathComponent("parcel.toml")) .createDefaultFileIfMissing() } await #expect(throws: ConfigDiagnostic.self) { try await ConfigStore(fileURL: directory).createDefaultFileIfMissing() } } }
@Test func concurrentCreatorsKeepOneCompleteDefaultFile() async throws { try await withDirectory { directory in let url = directory.appendingPathComponent("parcel.toml") async let first: Void = ConfigStore(fileURL: url).createDefaultFileIfMissing() async let second: Void = ConfigStore(fileURL: url).createDefaultFileIfMissing() _ = try await (first, second) #expect(try String(contentsOf: url, encoding: .utf8) == ConfigStore.defaultFileContents) } }
private func withDirectory(_ body: (URL) async throws -> Void) async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) do { try await body(directory) } catch { try FileManager.default.removeItem(at: directory) throw error } try FileManager.default.removeItem(at: directory) }}