diff --git a/Sources/Fishee.swift b/Sources/Fishee.swift index 4e5d7eb..d120d80 100644 --- a/Sources/Fishee.swift +++ b/Sources/Fishee.swift @@ -86,7 +86,6 @@ struct Fishee: ParsableCommand { let result = writeFishHistory( to: writePath, history: finalHistory, - historyFileLocation: fishHistoryLocation?.path, backup: backup ) if result { diff --git a/Sources/Parser.swift b/Sources/Parser.swift index 60efe51..d1b5bf7 100644 --- a/Sources/Parser.swift +++ b/Sources/Parser.swift @@ -35,25 +35,25 @@ func backupHistory(_ path: String) -> Bool { let newFileURL = directory.appendingPathComponent(newFileName).appendingPathExtension(fileExtension) do { + try? fileManager.removeItem(at: newFileURL) try fileManager.copyItem(at: fileURL, to: newFileURL) print("File duplicated successfully to: \(newFileURL.path)") return true } catch { + print("error making a backup of \(path), got error: \(error)") return false } } -func writeFishHistory(to path: String, history: [FishHistoryEntry], historyFileLocation: String?, backup: Bool = true) -> Bool { +func writeFishHistory(to path: String, history: [FishHistoryEntry], backup: Bool = true) -> Bool { var output = "" if backup { - if let backupFile = historyFileLocation { - let result = backupHistory(backupFile) - if !result { - print("Failed to backup \(backupFile) so aborting!") - return false - } + let result = backupHistory(path) + if !result { + print("Failed to backup \(path) so aborting!") + return false } } diff --git a/Tests/ParserTests.swift b/Tests/ParserTests.swift index 87f3891..533b811 100644 --- a/Tests/ParserTests.swift +++ b/Tests/ParserTests.swift @@ -27,7 +27,7 @@ final class ParserTests { "myfile.txt" ) let filePathforFileBackupTest = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)[0].appendingPathComponent( - "myfile.txt_copy" + "myfile_copy.txt" ) deinit { @@ -51,7 +51,7 @@ final class ParserTests { let written = writeFishHistory( to: filePathforWriteTest.path, history: [historyItem], - historyFileLocation: fishHistoryFile + backup: false ) #expect(written) @@ -64,6 +64,14 @@ final class ParserTests { """ #expect(fileContent == expectedEntry) + + // confirm backup functionality is working + let write_again = writeFishHistory( + to: filePathforWriteTest.path, + history: [historyItem], + backup: true + ) + #expect(write_again) #expect(FileManager.default.fileExists(atPath: filePathforFileBackupTest.path)) }