Skip to content

Commit c85b635

Browse files
committed
fix unit tests
1 parent 22ac00a commit c85b635

File tree

10 files changed

+45
-29
lines changed

10 files changed

+45
-29
lines changed

Sources/LinguaLib/Infrastructure/DirectoryOperations/DirectoryOperable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public final class DirectoryOperator: DirectoryOperable {
2727

2828
do {
2929
try fileManagerProvider.manager.createDirectory(at: outputFolder, withIntermediateDirectories: true, attributes: nil)
30+
} catch let error as DirectoryOperationError {
31+
throw error
3032
} catch {
3133
throw DirectoryOperationError.folderCreationFailed(error.localizedDescription)
3234
}
@@ -42,6 +44,8 @@ public final class DirectoryOperator: DirectoryOperable {
4244
for fileURL in fileURLs where fileURL.lastPathComponent.hasPrefix(prefix) {
4345
do {
4446
try fileManager.removeItem(at: fileURL)
47+
} catch let error as DirectoryOperationError {
48+
throw error
4549
} catch {
4650
throw DirectoryOperationError.removeItemFailed(error.localizedDescription)
4751
}

Sources/LinguaLib/Infrastructure/DirectoryOperations/DirectoryOperationError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
public enum DirectoryOperationError: LocalizedError {
3+
public enum DirectoryOperationError: LocalizedError, Equatable {
44
case folderCreationFailed(String)
55
case removeItemFailed(String)
66

Tests/LinguaTests/Application/Processor/LocalizationProcessorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class LocalizationProcessorTests: XCTestCase {
3535
}
3636

3737
func test_process_throwsErrorWhenModuleLocalizationFails() async throws {
38-
let localizationModule = MockLocalizationModule(shouldThrow: true)
38+
let localizationModule = MockLocalizationModule(shouldThrow: "Error_message")
3939
let (sut, actors) = makeSUT(localizationModule: localizationModule)
4040
let tempDirectoryURL = try createTemporaryDirectoryURL()
4141
let configPath = try createTemporaryConfigFile(data: createConfigData(in: tempDirectoryURL), tempDirectoryURL: tempDirectoryURL)
@@ -47,7 +47,7 @@ final class LocalizationProcessorTests: XCTestCase {
4747
XCTAssertEqual(actors.logger.messages, [.message(message: "Loading configuration file...", level: .info),
4848
.message(message: "Initializing localization module...", level: .info),
4949
.message(message: "Starting localization...", level: .info),
50-
.message(message: DirectoryOperationError.folderCreationFailed.localizedDescription, level: .error)])
50+
.message(message: DirectoryOperationError.folderCreationFailed("Error_message").localizedDescription, level: .error)])
5151
XCTAssertEqual(actors.mockLocalizationModule.messages, [])
5252
}
5353
}
@@ -85,7 +85,7 @@ private extension LocalizationProcessorTests {
8585
let mockLocalizationModule: MockLocalizationModule
8686
}
8787

88-
func makeSUT(localizationModule: MockLocalizationModule = MockLocalizationModule(),
88+
func makeSUT(localizationModule: MockLocalizationModule = MockLocalizationModule(shouldThrow: .none),
8989
configFileGenerator: ConfigInitialFileGenerating = ConfigInitialFileGenerator.make()) -> (sut: LocalizationProcessor, actors: Actors) {
9090
let argumentParser = CommandLineParser()
9191
let logger = MockLogger()

Tests/LinguaTests/Application/Processor/Mock/MockLocalizationModule.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ final class MockLocalizationModule: ModuleLocalizing {
66
case localize(LocalizationPlatform)
77
}
88
private(set) var messages = [Message]()
9-
private let shouldThrow: Bool
9+
private let shouldThrow: String?
1010

11-
init(shouldThrow: Bool = false) {
11+
init(shouldThrow: String?) {
1212
self.shouldThrow = shouldThrow
1313
}
1414

1515
func localize(for platform: LocalizationPlatform) async throws {
16-
if shouldThrow {
17-
throw DirectoryOperationError.folderCreationFailed
16+
if let shouldThrow {
17+
throw DirectoryOperationError.folderCreationFailed(shouldThrow)
1818
}
1919
messages.append(.localize(platform))
2020
}

Tests/LinguaTests/Infrastructure/DirectoryOperations/DirectoryOperatorTests.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ final class DirectoryOperatorTests: XCTestCase {
1313

1414
func test_createDirectory_throwsError_onFailure() {
1515
let errorFileManager = MockFileManager()
16-
errorFileManager.shouldThrowErrorOnCreateDirectory = true
16+
errorFileManager.shouldThrowErrorOnCreateDirectory = "Create_failed"
1717
let sut = makeSUT(fileManager: errorFileManager)
1818
let outputDirectory = NSTemporaryDirectory()
1919
let directoryName = "TestDirectory"
2020

2121
XCTAssertThrowsError(try sut.createDirectory(named: directoryName, in: outputDirectory)) { error in
22-
XCTAssertEqual(error as? DirectoryOperationError, DirectoryOperationError.folderCreationFailed)
22+
XCTAssertEqual((error as? DirectoryOperationError), DirectoryOperationError.folderCreationFailed("Create_failed"))
23+
}
24+
}
25+
26+
func test_createDirectory_throwsError_onEmptyDirectory() {
27+
let errorFileManager = MockFileManager()
28+
errorFileManager.shouldThrowErrorOnCreateDirectory = "Directory name is empty."
29+
let sut = makeSUT(fileManager: errorFileManager)
30+
let outputDirectory = NSTemporaryDirectory()
31+
let directoryName = ""
32+
33+
XCTAssertThrowsError(try sut.createDirectory(named: directoryName, in: outputDirectory)) { error in
34+
XCTAssertEqual((error as? DirectoryOperationError)?.errorDescription, DirectoryOperationError.folderCreationFailed("Directory name is empty.").errorDescription)
2335
}
2436
}
2537

@@ -47,7 +59,7 @@ final class DirectoryOperatorTests: XCTestCase {
4759

4860
func test_removeFiles_throwsError_onRemoveItem() throws {
4961
let errorFileManager = MockFileManager()
50-
errorFileManager.shouldThrowErrorOnRemoveItem = true
62+
errorFileManager.shouldThrowErrorOnRemoveItem = "Remove_failed"
5163
let sut = makeSUT(fileManager: errorFileManager)
5264
let outputDirectory = NSTemporaryDirectory()
5365
let directoryName = "TestDirectory"
@@ -64,7 +76,7 @@ final class DirectoryOperatorTests: XCTestCase {
6476
// Test the error case
6577
XCTAssertThrowsError(try sut.removeFiles(withPrefix: .packageName, in: createdDirectoryURL)) { error in
6678
XCTAssertEqual((error as? DirectoryOperationError)?.localizedDescription,
67-
DirectoryOperationError.removeItemFailed.localizedDescription)
79+
DirectoryOperationError.removeItemFailed("Remove_failed").localizedDescription)
6880
}
6981
}
7082
}

Tests/LinguaTests/Infrastructure/DirectoryOperations/Mock/MockFileManager.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import Foundation
33

44
final class MockFileManager: FileManager {
55
let files: [String]
6-
var shouldThrowErrorOnRemoveItem = false
7-
var shouldThrowErrorOnCreateDirectory = false
6+
var shouldThrowErrorOnRemoveItem: String?
7+
var shouldThrowErrorOnCreateDirectory: String?
88

99
init(files: [String] = []) {
1010
self.files = files
@@ -17,16 +17,16 @@ final class MockFileManager: FileManager {
1717
override func createDirectory(at url: URL,
1818
withIntermediateDirectories createIntermediates: Bool,
1919
attributes: [FileAttributeKey : Any]? = nil) throws {
20-
if shouldThrowErrorOnCreateDirectory {
21-
throw DirectoryOperationError.folderCreationFailed
20+
if let shouldThrowErrorOnCreateDirectory {
21+
throw DirectoryOperationError.folderCreationFailed(shouldThrowErrorOnCreateDirectory)
2222
} else {
2323
try super.createDirectory(at: url, withIntermediateDirectories: createIntermediates, attributes: attributes)
2424
}
2525
}
2626

2727
override func removeItem(at URL: URL) throws {
28-
if shouldThrowErrorOnRemoveItem {
29-
throw NSError(domain: NSCocoaErrorDomain, code: NSFileWriteUnknownError, userInfo: nil)
28+
if let shouldThrowErrorOnRemoveItem {
29+
throw DirectoryOperationError.removeItemFailed(shouldThrowErrorOnRemoveItem)
3030
} else {
3131
try super.removeItem(at: URL)
3232
}

Tests/LinguaTests/Infrastructure/LocalizationGenerator/Generator/ConfigInitialFileGeneratorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class ConfigInitialFileGeneratorTests: XCTestCase {
1616
}
1717

1818
func test_generate_throwsErrorOnFailure() {
19-
let (sut, _) = makeSUT(shouldFail: true)
19+
let (sut, _) = makeSUT(shouldFail: "error")
2020

2121
XCTAssertThrowsError(try sut.generate())
2222
}
@@ -38,7 +38,7 @@ private extension ConfigInitialFileGeneratorTests {
3838
let fileName: String
3939
}
4040

41-
func makeSUT(shouldFail: Bool = false, encoder: JSONEncoding? = nil) -> (sut: ConfigInitialFileGenerator, actors: Actors) {
41+
func makeSUT(shouldFail: String? = .none, encoder: JSONEncoding? = nil) -> (sut: ConfigInitialFileGenerator, actors: Actors) {
4242
let contentFilesCreator = MockContentFilesCreator()
4343
contentFilesCreator.shouldThrowError = shouldFail
4444
let config = Config.createTemplateConfig()

Tests/LinguaTests/Infrastructure/LocalizationGenerator/Generator/LocalizedPlatformFilesGeneratorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XCTest
33

44
final class LocalizedPlatformFilesGeneratorTests: XCTestCase {
55
func test_createPlatformFiles_createsFilesSuccessfully() throws {
6-
let (contentGenerator, filesCreator) = makeContentGeneratorAndCreator()
6+
let (contentGenerator, filesCreator) = makeContentGeneratorAndCreator(shouldFilesCreatorThrowError: .none)
77
let fileExtension = "txt"
88
let fileNameGenerator = MockPlatformFilesNameGenerator(fileExtension: fileExtension)
99
let sut = makeSUT(contentGenerator: contentGenerator, filesCreator: filesCreator, fileNameGenerator: fileNameGenerator)
@@ -18,7 +18,7 @@ final class LocalizedPlatformFilesGeneratorTests: XCTestCase {
1818
}
1919

2020
func test_createPlatformFiles_throwsErrorAndLogsMessage_onCreateFilesFailure() {
21-
let (contentGenerator, filesCreator) = makeContentGeneratorAndCreator(shouldFilesCreatorThrowError: true)
21+
let (contentGenerator, filesCreator) = makeContentGeneratorAndCreator(shouldFilesCreatorThrowError: "Error_message")
2222
let sut = makeSUT(contentGenerator: contentGenerator, filesCreator: filesCreator)
2323
let entries: [LocalizationEntry] = [.create(plural: true)]
2424
let outputFolder = URL(fileURLWithPath: NSTemporaryDirectory())
@@ -27,12 +27,12 @@ final class LocalizedPlatformFilesGeneratorTests: XCTestCase {
2727
sectionName: "SectionName",
2828
outputFolder: outputFolder,
2929
language: "en")) { error in
30-
XCTAssertEqual(error as? DirectoryOperationError, DirectoryOperationError.folderCreationFailed)
30+
XCTAssertEqual(error as? DirectoryOperationError, DirectoryOperationError.folderCreationFailed("Error_message"))
3131
}
3232
}
3333

3434
func makeContentGeneratorAndCreator(
35-
shouldFilesCreatorThrowError: Bool = false
35+
shouldFilesCreatorThrowError: String?
3636
) -> (contentGenerator: MockLocalizedContentGenerator, filesCreator: MockContentFilesCreator) {
3737
let contentGenerator = MockLocalizedContentGenerator()
3838
contentGenerator.content = ("stringsContent", "stringsDictContent")

Tests/LinguaTests/Infrastructure/LocalizationGenerator/Generator/Mock/MockContentFilesCreator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import Foundation
33

44
class MockContentFilesCreator: ContentFileCreatable {
55
var writtenContent: [String: String] = [:]
6-
var shouldThrowError = false
6+
var shouldThrowError: String?
77

88
func createFiles(with content: String, fileName: String, outputFolder: URL) throws {
99
try createFilesInCurrentDirectory(with: content, fileName: fileName)
1010
}
1111

1212
func createFilesInCurrentDirectory(with content: String, fileName: String) throws {
13-
if shouldThrowError {
14-
throw DirectoryOperationError.folderCreationFailed
13+
if let shouldThrowError {
14+
throw DirectoryOperationError.folderCreationFailed(shouldThrowError)
1515
}
1616
writtenContent[fileName] = content
1717
}

Tests/LinguaTests/Infrastructure/SwiftLocalizeGenerator/Generator/SwiftLocalizedCodeFileGeneratorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ final class SwiftLocalizedCodeFileGeneratorTests: XCTestCase {
1818
func test_generate_printsErrorOnCreateFilesFailure() {
1919
let mockLogger = MockLogger()
2020
let mockContentFilesCreator = MockContentFilesCreator()
21-
mockContentFilesCreator.shouldThrowError = true
21+
mockContentFilesCreator.shouldThrowError = "Error_message"
2222
let sut = makeSUT(mockLogger: mockLogger,
2323
mockContentFilesCreator: mockContentFilesCreator)
2424

2525
sut.generate(from: "inputPath", outputPath: "outputPath")
2626

2727
XCTAssertEqual(mockLogger.messages,
28-
[.message(message: DirectoryOperationError.folderCreationFailed.errorDescription ?? "", level: .error)])
28+
[.message(message: DirectoryOperationError.folderCreationFailed("Error_message").localizedDescription, level: .error)])
2929
}
3030
}
3131

0 commit comments

Comments
 (0)