-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FileManager: Implement contentsEqual(atPath:andPath:) #1510
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
06add84
FileManager: Implement contentsEqual(atPath:andPath:)
spevans c8c2a7b
FileManager contentsEqual(atPath:andPath:) updates.
spevans faba87e
FileManager: Use st_blksize as a buffer size when comparing files.
spevans 054ed7c
FileManager: Add more tests for contentsEqual(atPath:andPath:)
spevans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,8 @@ class TestFileManager : XCTestCase { | |
("test_homedirectoryForUser", test_homedirectoryForUser), | ||
("test_temporaryDirectoryForUser", test_temporaryDirectoryForUser), | ||
("test_creatingDirectoryWithShortIntermediatePath", test_creatingDirectoryWithShortIntermediatePath), | ||
("test_mountedVolumeURLs", test_mountedVolumeURLs) | ||
("test_mountedVolumeURLs", test_mountedVolumeURLs), | ||
("test_contentsEqual", test_contentsEqual) | ||
] | ||
} | ||
|
||
|
@@ -620,4 +621,83 @@ class TestFileManager : XCTestCase { | |
XCTAssertTrue(visibleVolumes.count < volumes.count) | ||
#endif | ||
} | ||
|
||
func test_contentsEqual() { | ||
let fm = FileManager.default | ||
let tmpParentDirURL = URL(fileURLWithPath: NSTemporaryDirectory() + "test_contentsEqualdir", isDirectory: true) | ||
let testDir1 = tmpParentDirURL.appendingPathComponent("testDir1") | ||
let testDir2 = tmpParentDirURL.appendingPathComponent("testDir2") | ||
let testDir3 = testDir1.appendingPathComponent("subDir") | ||
|
||
defer { try? fm.removeItem(atPath: tmpParentDirURL.path) } | ||
|
||
func testFileURL(_ name: String, _ ext: String) -> URL? { | ||
guard let url = testBundle().url(forResource: name, withExtension: ext) else { | ||
XCTFail("Cant open \(name).\(ext)") | ||
return nil | ||
} | ||
return url | ||
} | ||
|
||
guard let testFile1URL = testFileURL("NSStringTestData", "txt") else { return } | ||
guard let testFile2URL = testFileURL("NSURLTestData", "plist") else { return } | ||
guard let testFile3URL = testFileURL("NSString-UTF32-BE-data", "txt") else { return } | ||
guard let testFile4URL = testFileURL("NSString-UTF32-LE-data", "txt") else { return } | ||
let symlink = testDir1.appendingPathComponent("testlink").path | ||
|
||
// Setup test directories | ||
do { | ||
// Clean out and leftover test data | ||
try? fm.removeItem(atPath: tmpParentDirURL.path) | ||
|
||
// testDir1 | ||
try fm.createDirectory(atPath: testDir1.path, withIntermediateDirectories: true) | ||
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("null1").path, withDestinationPath: "/dev/null") | ||
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("zero1").path, withDestinationPath: "/dev/zero") | ||
try "foo".write(toFile: testDir1.appendingPathComponent("foo.txt").path, atomically: false, encoding: .ascii) | ||
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("foo1").path, withDestinationPath: "foo.txt") | ||
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("bar2").path, withDestinationPath: "foo1") | ||
let unreadable = testDir1.appendingPathComponent("unreadable_file").path | ||
try "unreadable".write(toFile: unreadable, atomically: false, encoding: .ascii) | ||
try fm.setAttributes([.posixPermissions: NSNumber(value: 0)], ofItemAtPath: unreadable) | ||
try Data().write(to: testDir1.appendingPathComponent("empty_file")) | ||
try fm.createSymbolicLink(atPath: symlink, withDestinationPath: testFile1URL.path) | ||
|
||
// testDir2 | ||
try fm.createDirectory(atPath: testDir2.path, withIntermediateDirectories: true) | ||
try fm.createSymbolicLink(atPath: testDir2.appendingPathComponent("bar2").path, withDestinationPath: "foo1") | ||
try fm.createSymbolicLink(atPath: testDir2.appendingPathComponent("foo2").path, withDestinationPath: "../testDir1/foo.txt") | ||
|
||
// testDir3 | ||
try fm.createDirectory(atPath: testDir3.path, withIntermediateDirectories: true) | ||
try fm.createSymbolicLink(atPath: testDir3.appendingPathComponent("bar2").path, withDestinationPath: "foo1") | ||
try fm.createSymbolicLink(atPath: testDir3.appendingPathComponent("foo2").path, withDestinationPath: "../testDir1/foo.txt") | ||
} catch { | ||
XCTFail(String(describing: error)) | ||
} | ||
|
||
XCTAssertTrue(fm.contentsEqual(atPath: "/dev/null", andPath: "/dev/null")) | ||
XCTAssertTrue(fm.contentsEqual(atPath: "/dev/urandom", andPath: "/dev/urandom")) | ||
XCTAssertFalse(fm.contentsEqual(atPath: "/dev/null", andPath: "/dev/zero")) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.appendingPathComponent("null1").path, andPath: "/dev/null")) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.appendingPathComponent("zero").path, andPath: "/dev/zero")) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.appendingPathComponent("foo.txt").path, andPath: testDir1.appendingPathComponent("foo1").path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.appendingPathComponent("foo.txt").path, andPath: testDir1.appendingPathComponent("foo2").path)) | ||
XCTAssertTrue(fm.contentsEqual(atPath: testDir1.appendingPathComponent("bar2").path, andPath: testDir2.appendingPathComponent("bar2").path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.appendingPathComponent("foo1").path, andPath: testDir2.appendingPathComponent("foo2").path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: "/non_existant_file", andPath: "/non_existant_file")) | ||
|
||
let emptyFile = testDir1.appendingPathComponent("empty_file") | ||
XCTAssertFalse(fm.contentsEqual(atPath: emptyFile.path, andPath: "/dev/null")) | ||
XCTAssertFalse(fm.contentsEqual(atPath: emptyFile.path, andPath: testDir1.appendingPathComponent("null1").path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: emptyFile.path, andPath: testDir1.appendingPathComponent("unreadable_file").path)) | ||
|
||
XCTAssertTrue(fm.contentsEqual(atPath: testFile1URL.path, andPath: testFile1URL.path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testFile1URL.path, andPath: testFile2URL.path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testFile3URL.path, andPath: testFile4URL.path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: symlink, andPath: testFile1URL.path)) | ||
XCTAssertTrue(fm.contentsEqual(atPath: testDir1.path, andPath: testDir1.path)) | ||
XCTAssertTrue(fm.contentsEqual(atPath: testDir2.path, andPath: testDir3.path)) | ||
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path)) | ||
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. A test case that verifies recursive (in)equality is needed. |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'd expect this to recurse for each item in the two directories. If the contents of 'A/C' doesn't match 'B/C', then this is supposed to fail.