Skip to content

Commit

Permalink
better handling of yaml errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanomondino committed May 1, 2023
1 parent 1d47fff commit 824f714
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Sources/MurrayKit/Coding/YAMLCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ extension YAMLDecoder: Decoder {
case let DecodingError.dataCorrupted(inner):
switch inner.underlyingError ?? error {
case is Errors: throw inner.underlyingError ?? error
default: throw Errors.unparsableContent((inner.underlyingError ?? error).localizedDescription)
default:
if let underlying = inner.underlyingError as? YamlError {
throw Errors.unparsableContent(underlying.description)
}
throw Errors.unparsableContent((inner.underlyingError ?? error).localizedDescription)
}
default: throw Errors.unparsableContent(error.localizedDescription)
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/MurrayKit/Models/CodableFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ public struct CodableFile<Object: Codable & Hashable>: Hashable {
let data = try file.read()
if let ext = file.extension,
let decoder: Decoder = Self.decoder(from: ext) {
try self.init(file: file, object: decoder.decode(data))
do {
try self.init(file: file, object: decoder.decode(data))
} catch {
Logger.log(error.localizedDescription, level: .verbose)
throw Errors.unparsableFile(file.path)
}

} else {
let decoders: [Decoder] = [JSONDecoder(), YAMLDecoder()]
Expand Down
24 changes: 24 additions & 0 deletions Tests/MurrayKitTests/CodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Stefano Mondino on 01/05/23.
//

import Foundation
@testable import MurrayKit
import XCTest
import Yams

class CodableTests: TestCase {
func testYAMLFileProducesProperError() throws {
let scenario = Scenario.wrongMurrayfile
let root = try scenario.make()
XCTAssertThrowsError(try CodableFile<Murrayfile>(in: root).object) { error in
switch error as? Errors ?? .unknown {
case .unparsableFile: return
default: XCTFail("\(error) is not of expected type")
}
}
}
}
3 changes: 3 additions & 0 deletions Tests/MurrayKitTests/Mocks/WrongMurrayfile/Murrayfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
environment:
someProperty: {{name}} but this is probably wrong because is missing surrounding quotes
packages: []
17 changes: 11 additions & 6 deletions Tests/MurrayKitTests/Utilities/Scenario.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ extension Folder {
try Folder(path: Bundle.module.resourcePath ?? Bundle.module.bundlePath)
.subfolder(at: "Mocks/\(name)")
}

static func testFolder() throws -> Folder {
try Folder
.temporary
.createSubfolderIfNeeded(withName: "Murray")
}

static func emptyTestFolder() throws -> Folder {
try? Folder.temporary.subfolder(named: "emptyTest").delete()
return try Folder
Expand All @@ -30,7 +30,7 @@ extension Folder {

struct Scenario {
let name: String

func make() throws -> Folder {
let origin = try Folder.mock(at: name)

Expand All @@ -39,7 +39,7 @@ struct Scenario {
try? destinationParent
.subfolder(named: name)
.delete()

try origin
.copy(to: destinationParent)

Expand All @@ -51,21 +51,26 @@ struct Scenario {
}

extension Scenario {

static var simpleJSON: Scenario {
.init(name: "SimpleJSON")
}

static var simpleYaml: Scenario {
.init(name: "SimpleYaml")
}


static var wrongMurrayfile: Scenario {
.init(name: "WrongMurrayfile")
}

static var cloneOrigin: Scenario {
.init(name: "Skeleton")
}

static var cloneOriginInSubfolder: Scenario {
.init(name: "SkeletonInSubfolder")
}

static func folder() throws -> Folder {
try Folder.testFolder()
}
Expand Down

0 comments on commit 824f714

Please sign in to comment.