Skip to content

Commit ae3abd5

Browse files
committed
implement isDeletableFile test, update is*File tests
1 parent f311ec7 commit ae3abd5

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

TestFoundation/TestFileManager.swift

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ class TestFileManager : XCTestCase {
208208

209209
func test_isReadableFile() {
210210
let fm = FileManager.default
211-
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
211+
let path = NSTemporaryDirectory() + "test_isReadableFile\(NSUUID().uuidString)"
212212

213-
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
213+
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))
214214

215215
do {
216216
let attrs = try fm.attributesOfItem(atPath: path)
@@ -225,41 +225,61 @@ class TestFileManager : XCTestCase {
225225

226226
func test_isWritableFile() {
227227
let fm = FileManager.default
228-
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
228+
let path = NSTemporaryDirectory() + "test_isWritableFile\(NSUUID().uuidString)"
229229

230-
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
230+
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))
231231

232232
do {
233233
let attrs = try fm.attributesOfItem(atPath: path)
234234
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16
235-
let fileIsReadableFile = (permissions & S_IWUSR == S_IWUSR)
236-
let fmIsReadableFile = fm.isReadableFile(atPath: path)
237-
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile)
235+
let fileIsWritableFile = (permissions & S_IWUSR == S_IWUSR)
236+
let fmIsWritableFile = fm.isWritableFile(atPath: path)
237+
XCTAssertTrue(fileIsWritableFile == fmIsWritableFile)
238238
} catch let e {
239239
XCTFail("\(e)")
240240
}
241241
}
242242

243243
func test_isExecutableFile() {
244244
let fm = FileManager.default
245-
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
245+
let path = NSTemporaryDirectory() + "test_isExecutableFile\(NSUUID().uuidString)"
246246

247-
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
247+
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))
248248

249249
do {
250250
let attrs = try fm.attributesOfItem(atPath: path)
251251
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16
252-
let fileIsReadableFile = (permissions & S_IXUSR == S_IXUSR)
253-
let fmIsReadableFile = fm.isReadableFile(atPath: path)
254-
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile)
252+
let fileIsExecutableFile = (permissions & S_IXUSR == S_IXUSR)
253+
let fmIsExecutableFile = fm.isExecutableFile(atPath: path)
254+
XCTAssertTrue(fileIsExecutableFile == fmIsExecutableFile)
255255
} catch let e {
256256
XCTFail("\(e)")
257257
}
258258
}
259259

260260
func test_isDeletableFile() {
261-
// TODO: Implement test
262-
// how to test?
261+
let fm = FileManager.default
262+
263+
do {
264+
let dir_path = NSTemporaryDirectory() + "/test_isDeletableFile_dir/"
265+
let file_path = dir_path + "test_isDeletableFile\(NSUUID().uuidString)"
266+
// create test directory
267+
try fm.createDirectory(atPath: dir_path, withIntermediateDirectories: true)
268+
// create test file
269+
XCTAssertTrue(fm.createFile(atPath: file_path, contents: Data()))
270+
271+
// test undeletable if parent directory has no permissions
272+
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: dir_path)
273+
XCTAssertFalse(fm.isDeletableFile(atPath: file_path))
274+
275+
// test deletable if parent directory has all necessary permissions
276+
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0755))], ofItemAtPath: dir_path)
277+
XCTAssertTrue(fm.isDeletableFile(atPath: file_path))
278+
}
279+
catch { XCTFail("\(error)") }
280+
281+
// test against known undeletable file
282+
XCTAssertFalse(fm.isDeletableFile(atPath: "/dev/null"))
263283
}
264284

265285
func test_fileAttributes() {

0 commit comments

Comments
 (0)