1

I have some code that expects to be able to write to a file and I want to test that a couple of error scenarios are handled correctly. One of these is that the target file is locked.

I thought that something like this should provoke the error (in Swift, but that should not matter):

var someByte: UInt8 = 0b101010

let os = OutputStream(toFileAtPath: "myfile", append: false)!
os.open()
os.write(&someByte, maxLength: 1)

// Inlining methodThatWrites(toFileAt: "myfile")
let os2 = OutputStream(toFileAtPath: "myfile", append: false)!
os2.open()
os2.write(&someByte, maxLength: 1)
os2.close()
// end of inlining

os.write(&someByte, maxLength: 1)
os.close()

However, no error happens -- the same process may write concurrently to the same file as it pleases. (Which makes complete sense from the OS' perspective, but is really weird from an application perspective.)

I would try to start an external process and open the file for writing from there, but you can not do that when building for iOS (Process, NSTask are not available).

Is it possible to simulate a locked file in a unit test built for iOS?


I saw this question, but the answers don't apply here (no Windows, test is automated).

Community
  • 1
  • 1
Raphael
  • 9,779
  • 5
  • 63
  • 94

0 Answers0