Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/Zero/Services/RunProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ class FileBasedRunProfileService: RunProfileService {
return [:]
}

guard let store = try? JSONDecoder().decode(RunProfileStore.self, from: data) else {
do {
let store = try JSONDecoder().decode(RunProfileStore.self, from: data)
return store.commandsByRepository
} catch {
AppLogStore.shared.append(
"RunProfileService decode failed for \(configPath): \(error.localizedDescription)"
)
return [:]
}

return store.commandsByRepository
}

private func writeCommands(_ commands: [String: String]) throws {
Expand Down
22 changes: 22 additions & 0 deletions Tests/ZeroTests/RunProfileServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ final class RunProfileServiceTests: XCTestCase {

override func setUp() {
super.setUp()
AppLogStore.shared.clear()
testConfigPath = "/tmp/test-zero-run-profiles-\(UUID().uuidString).json"
service = FileBasedRunProfileService(configPath: testConfigPath)
}

override func tearDown() {
AppLogStore.shared.clear()
if let testConfigPath {
try? FileManager.default.removeItem(atPath: testConfigPath)
}
Expand Down Expand Up @@ -72,4 +74,24 @@ final class RunProfileServiceTests: XCTestCase {
// Then
XCTAssertEqual(try service.loadCommand(for: repositoryURL), "swift run")
}

func testLoadCommandFromCorruptedStoreAppendsDecodeFailureToAppLogStore() throws {
// Given
let repositoryURL = URL(string: "https://github.com/zero-ide/Zero.git")!
try "{not-valid-json".write(
to: URL(fileURLWithPath: testConfigPath),
atomically: true,
encoding: .utf8
)

// When
let command = try service.loadCommand(for: repositoryURL)

// Then
XCTAssertNil(command)
let logEntries = AppLogStore.shared.recentEntries()
XCTAssertTrue(logEntries.contains { entry in
entry.contains("RunProfileService decode failed") && entry.contains(testConfigPath)
})
}
}