I understand that when a unit test executes, it is in the sandbox environment of Xcode. In a unit test, I need to write and read data to a file. Since the project resides in a git repository, this must be done completely from within code (i.e. no special manual test setup of directories is possible)
For all my searching and trying today, I cannot find a way to write to a file from within the unit test.
Does anybody know of a method to write to a file under these conditions?
Edit: This is the problematic code:
let dirPath = NSTemporaryDirectory()!
var filePath = dirPath.stringByAppendingPathComponent("unittest.json")
if !NSFileManager.defaultManager().isWritableFileAtPath(filePath) {
return (nil, "Directory missing or write access not granted for \(path)")
}
You should be able to use the temporary directory: NSTemporaryDirectory()
and have it reliably point to a safe place to read and write. I'd suggest making sure you clean up any created files.
let dirPath = NSTemporaryDirectory()
To see whether a file can be created (per the updated question), check the directory for write permissions:
let canCreate = NSFileManager.defaultManager().isWritableFileAtPath(dirPath)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With