Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import ArgumentParserimport Foundationimport ParcelConfigimport Synchronizationimport Testing
@testable import prcl
struct ConfigCheckCommandTests { @Test func parsesLocalValidationCommand() throws { #expect( try CLIParsing.parse(["config", "check"]) == .configCheck(path: ConfigStore.defaultFileURL.path, json: false)) #expect( try CLIParsing.parse(["config", "check", "--path", "custom.toml", "--json"]) == .configCheck(path: "custom.toml", json: true)) }
@Test func inheritedDisplacementIsAValidWarningAndExplicitDuplicatesAreErrors() throws { let warning = ConfigCheckReport.validate("[keybindings]\nlayout-tree = 'ctrl-alt-t'\n") #expect(warning.valid) #expect(warning.diagnostics.first?.severity == "warning") #expect(try warning.format(json: false).contains("Unbound because keybindings.layout-tree uses this key")) let error = ConfigCheckReport.validate( "[keybindings]\nlayout-tree = 'ctrl-alt-t'\ntoggle-automatic-tiling = 'ctrl-alt-t'\n") #expect(!error.valid) #expect(error.diagnostics.first?.severity == "error") #expect(!ConfigCheckReport.validate("[broken").valid) }
@Test func validatesWithoutConnectingAndKeepsWarningsSuccessful() async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent( "parcel-config-check-\(UUID().uuidString)") try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } let file = directory.appendingPathComponent("parcel.toml") try "[keybindings]\nlayout-tree = 'ctrl-alt-t'\n".write(to: file, atomically: true, encoding: .utf8) let output = Mutex("") try await CLIRunner.execute(.configCheck(path: file.path, json: true), socketPath: "not-an-absolute-path") { text in output.withLock { $0 += text } } let report = try JSONDecoder().decode(Report.self, from: Data(output.withLock { $0 }.utf8)) #expect(report.valid) #expect(report.diagnostics.first?.message.contains("Unbound because") == true) try "[general]\nshortcuts-enabled = 'no'\n".write(to: file, atomically: true, encoding: .utf8) await #expect(throws: ExitCode.self) { try await CLIRunner.execute(.configCheck(path: file.path, json: false), socketPath: "not-an-absolute-path") { _ in } } }
@Test(arguments: [false, true]) func missingConfigValidatesDefaultsOfflineWithoutCreatingAFile(json: Bool) async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent( "parcel-config-missing-\(UUID().uuidString)") try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } let file = directory.appendingPathComponent("absent/parcel.toml") let output = Mutex("") try await CLIRunner.execute(.configCheck(path: file.path, json: json), socketPath: "not-an-absolute-path") { text in output.withLock { $0 += text } } #expect(try output.withLock { $0 } == ConfigCheckReport.validate("").format(json: json)) #expect(!FileManager.default.fileExists(atPath: file.path)) #expect(!FileManager.default.fileExists(atPath: file.deletingLastPathComponent().path)) }
@Test(arguments: ["directory", "unreadable", "invalid-utf8", "not-directory"]) func realFileFailuresDoNotValidateDefaults(failure: String) async throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent( "parcel-config-failure-\(UUID().uuidString)") try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } let file = directory.appendingPathComponent("parcel.toml") var path = file.path switch failure { case "directory": path = directory.path case "unreadable": try Data().write(to: file) try FileManager.default.setAttributes([.posixPermissions: 0], ofItemAtPath: file.path) case "invalid-utf8": try Data([0xFF]).write(to: file) case "not-directory": try Data().write(to: file) path = file.appendingPathComponent("parcel.toml").path default: preconditionFailure("Unknown file failure") } let output = Mutex("") do { try await CLIRunner.execute(.configCheck(path: path, json: true), socketPath: "not-an-absolute-path") { text in output.withLock { $0 += text } } Issue.record("A real file failure must not validate defaults") } catch { #expect(ParcelCLI.exitCode(for: error).rawValue != 0) #expect(!ParcelCLI.fullMessage(for: error).isEmpty) } #expect(output.withLock { $0 }.isEmpty) }
private struct Report: Decodable { struct Diagnostic: Decodable { let message: String } let valid: Bool let diagnostics: [Diagnostic] }}