diff --git a/Sources/Parsers/StringsFileParser.swift b/Sources/Parsers/StringsFileParser.swift index 8904208..97d99ae 100644 --- a/Sources/Parsers/StringsFileParser.swift +++ b/Sources/Parsers/StringsFileParser.swift @@ -8,12 +8,15 @@ import Foundation import PathKit public enum StringsFileParserError: Error, CustomStringConvertible { + case duplicateTable(name: String) case failureOnLoading(path: String) case invalidFormat case invalidPlaceholder(previous: StringsFileParser.PlaceholderType, new: StringsFileParser.PlaceholderType) public var description: String { switch self { + case .duplicateTable(let name): + return "Table \"\(name)\" already loaded, cannot add it again" case .failureOnLoading(let path): return "Failed to load a file at \"\(path)\"" case .invalidFormat: @@ -31,6 +34,11 @@ public final class StringsFileParser { // Localizable.strings files are generally UTF16, not UTF8! public func parseFile(at path: Path) throws { + let name = path.lastComponentWithoutExtension + + guard tables[name] == nil else { + throw StringsFileParserError.duplicateTable(name: name) + } guard let data = try? path.read() else { throw StringsFileParserError.failureOnLoading(path: path.string) } @@ -40,7 +48,6 @@ public final class StringsFileParser { throw StringsFileParserError.invalidFormat } - let name = path.lastComponentWithoutExtension tables[name] = try dict.map { key, translation in try Entry(key: key, translation: translation) } diff --git a/Tests/SwiftGenKitTests/StringsTests.swift b/Tests/SwiftGenKitTests/StringsTests.swift index 7d8fec7..30734a2 100644 --- a/Tests/SwiftGenKitTests/StringsTests.swift +++ b/Tests/SwiftGenKitTests/StringsTests.swift @@ -61,4 +61,18 @@ class StringsTests: XCTestCase { let result = parser.stencilContext() XCTDiffContexts(result, expected: "multiple.plist", sub: .strings) } + + func testMultipleFilesDuplicate() throws { + let parser = StringsFileParser() + try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings)) + + do { + try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings)) + XCTFail("Code did parse file successfully while it was expected to fail for duplicate file") + } catch StringsFileParserError.duplicateTable { + // That's the expected exception we want to happen + } catch let error { + XCTFail("Unexpected error occured while parsing: \(error)") + } + } }