2

Currently there are issues with files not being deleted. I've looked at other posts like: FileManager.default.removeItem not removing file [duplicate] and Error deleting contents in directory - Domain=NSCocoaErrorDomain Code=4 | Domain=NSPOSIXErrorDomain Code=2 “No such file or directory”

I'm getting the same error “No such file or directory”. This is happening with this function when it's being unit tested:

 internal func getCacheFolderPath() -> String {
    var folderPath = ""
    guard let path = self.urls(for:.cachesDirectory , in: .userDomainMask).last?.path else { return folderPath }
    folderPath = path + "/NetworkCache"
    if !self.fileExists(atPath: folderPath) {
        do {
          try  self.createDirectory(atPath: folderPath, withIntermediateDirectories: false, attributes: nil)
        }
        catch {
            print(error.localizedDescription)
        }
}
    return folderPath
}


internal func filePathForFileName(name: String) -> String {
    let cyptoName = name.md5()
    return getCacheFolderPath() + "/" + (cyptoName ?? "")
}



func deleteFile(forFile fileName: String){
        let path = self.filePathForFileName(name: fileName)

        if self.fileExists(atPath: path) {
            do {
                try self.removeItem(atPath: path)
              //  print(path)
            } catch  {
                print(error.localizedDescription)
            }
        }
    }

This is what the unit test looks like:

func test_deleteFile(){

    let path = sut!.filePathForFileName(name: fileName)

    sut?.deleteFile(forFile: fileName)
    print(path)

    if sut!.fileExists(atPath: path){

    let filemanager = FileManager.default

           do {
    try filemanager.removeItem(atPath: path)

           } catch {
            print ("The file could not be removed: \(error)")
        }
    }
}

FYI I am using a mockFileSystem that is subclassed from FileManager. Even when I try and delete the path manually in the do, try, catch block it still returns the error “No such file or directory”.

pacification
  • 5,838
  • 4
  • 29
  • 51
SwiftyJD
  • 5,257
  • 7
  • 41
  • 92

0 Answers0