New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isDeletableFile Implementation + Tests #1670
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ class TestFileManager : XCTestCase { | |
("test_moveFile", test_moveFile), | ||
("test_fileSystemRepresentation", test_fileSystemRepresentation), | ||
("test_fileExists", test_fileExists), | ||
("test_isReadableFile", test_isReadableFile), | ||
("test_isWritableFile", test_isWritableFile), | ||
("test_isExecutableFile", test_isExecutableFile), | ||
("test_isDeletableFile", test_isDeletableFile), | ||
("test_fileAttributes", test_fileAttributes), | ||
("test_fileSystemAttributes", test_fileSystemAttributes), | ||
("test_setFileAttributes", test_setFileAttributes), | ||
|
@@ -202,6 +206,62 @@ class TestFileManager : XCTestCase { | |
ignoreError { try fm.removeItem(atPath: tmpDir.path) } | ||
} | ||
|
||
func test_isReadableFile() { | ||
let fm = FileManager.default | ||
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)" | ||
|
||
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil)) | ||
|
||
do { | ||
let attrs = try fm.attributesOfItem(atPath: path) | ||
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16 | ||
let fileIsReadableFile = (permissions & S_IRUSR == S_IRUSR) | ||
let fmIsReadableFile = fm.isReadableFile(atPath: path) | ||
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile) | ||
} catch let e { | ||
XCTFail("\(e)") | ||
} | ||
} | ||
|
||
func test_isWritableFile() { | ||
let fm = FileManager.default | ||
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)" | ||
|
||
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil)) | ||
|
||
do { | ||
let attrs = try fm.attributesOfItem(atPath: path) | ||
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16 | ||
let fileIsReadableFile = (permissions & S_IWUSR == S_IWUSR) | ||
let fmIsReadableFile = fm.isReadableFile(atPath: path) | ||
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile) | ||
} catch let e { | ||
XCTFail("\(e)") | ||
} | ||
} | ||
|
||
func test_isExecutableFile() { | ||
let fm = FileManager.default | ||
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)" | ||
|
||
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil)) | ||
|
||
do { | ||
let attrs = try fm.attributesOfItem(atPath: path) | ||
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16 | ||
let fileIsReadableFile = (permissions & S_IXUSR == S_IXUSR) | ||
let fmIsReadableFile = fm.isReadableFile(atPath: path) | ||
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile) | ||
} catch let e { | ||
rauhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTFail("\(e)") | ||
} | ||
} | ||
|
||
func test_isDeletableFile() { | ||
// TODO: Implement test | ||
// how to test? | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could create a file and check its is deletable. Also check a file that should fail eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm, we would expect the created file to be deletable, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thats correct. You should be able to create a file that fails by putting it into a subdirectory with no permissions: eg
|
||
|
||
func test_fileAttributes() { | ||
let fm = FileManager.default | ||
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty close to the Darwin implementation! That implementation also special-cases the empty string to mean the current directory, but it's not behavior that's documented, so I don't think we need to port it.