fix deleting directory

this used to corrupt the local state (password entities remained in DB
but files/dirs were removed from git and disk)
This commit is contained in:
Lysann Tranvouez 2026-03-09 12:11:37 +01:00
parent 4405b7fe3d
commit 2c8d27df69
4 changed files with 24 additions and 0 deletions

View file

@ -73,6 +73,7 @@
"KeyImportError." = "Cannot import the key.";
"FileNotFoundError." = "File '%@' cannot be read.";
"PasswordDuplicatedError." = "Cannot add the password; password is duplicated.";
"CannotDeleteDirectoryError." = "Cannot delete directories; delete passwords instead.";
"GitResetError." = "Cannot identify the latest synced commit.";
"GitCreateSignatureError." = "Cannot create a valid author/committer signature.";
"GitPushNotSuccessfulError." = "Pushing local changes was not successful. Make sure there are no uncommitted changes on the remote repository.";

View file

@ -15,6 +15,7 @@ public enum AppError: Error, Equatable {
case keyImport
case readingFile(fileName: String)
case passwordDuplicated
case cannotDeleteDirectory
case gitReset
case gitCommit
case gitCreateSignature

View file

@ -273,6 +273,10 @@ public class PasswordStore {
}
public func delete(passwordEntity: PasswordEntity) throws {
if passwordEntity.isDir {
throw AppError.cannotDeleteDirectory
}
let deletedFileURL = passwordEntity.fileURL(in: storeURL)
let deletedFilePath = passwordEntity.path
try gitRm(path: passwordEntity.path)

View file

@ -143,9 +143,27 @@ final class PasswordStoreTest: XCTestCase {
try passwordStore.delete(passwordEntity: entity!)
XCTAssertNil(passwordStore.fetchPasswordEntity(with: "personal/github.com.gpg"))
XCTAssertNil(passwordStore.fetchPasswordEntity(with: "personal"))
XCTAssertFalse(FileManager.default.fileExists(atPath: localRepoURL.appendingPathComponent("personal").path))
waitForExpectations(timeout: 1, handler: nil)
}
func testDeleteDirectoryFails() throws {
try cloneRepository(.withGPGID)
expectation(forNotification: .passwordStoreUpdated, object: nil).isInverted = true
let entity = passwordStore.fetchPasswordEntity(with: "personal")
XCTAssertThrowsError(try passwordStore.delete(passwordEntity: entity!)) { error in
XCTAssertTrue(error is AppError, "Unexpected error type: \(type(of: error))")
XCTAssertEqual(error as? AppError, .cannotDeleteDirectory)
}
XCTAssertNotNil(passwordStore.fetchPasswordEntity(with: "personal/github.com.gpg"))
XCTAssertTrue(FileManager.default.fileExists(atPath: localRepoURL.appendingPathComponent("personal/github.com.gpg").path))
waitForExpectations(timeout: 0.1, handler: nil)
}
func testEditPasswordValue() throws {
try cloneRepository(.withGPGID)
try importSinglePGPKey()