Skip to content

Commit 054ed7c

Browse files
committed
FileManager: Add more tests for contentsEqual(atPath:andPath:)
1 parent faba87e commit 054ed7c

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

TestFoundation/TestFileManager.swift

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ class TestFileManager : XCTestCase {
627627
let tmpParentDirURL = URL(fileURLWithPath: NSTemporaryDirectory() + "test_contentsEqualdir", isDirectory: true)
628628
let testDir1 = tmpParentDirURL.appendingPathComponent("testDir1")
629629
let testDir2 = tmpParentDirURL.appendingPathComponent("testDir2")
630-
let testDir3 = testDir1.appendingPathComponent("subDir")
630+
let testDir3 = testDir1.appendingPathComponent("subDir/anotherDir/extraDir/lastDir")
631631

632632
defer { try? fm.removeItem(atPath: tmpParentDirURL.path) }
633633

@@ -663,6 +663,10 @@ class TestFileManager : XCTestCase {
663663
try Data().write(to: testDir1.appendingPathComponent("empty_file"))
664664
try fm.createSymbolicLink(atPath: symlink, withDestinationPath: testFile1URL.path)
665665

666+
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("thisDir").path, withDestinationPath: ".")
667+
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("parentDir").path, withDestinationPath: "..")
668+
try fm.createSymbolicLink(atPath: testDir1.appendingPathComponent("rootDir").path, withDestinationPath: "/")
669+
666670
// testDir2
667671
try fm.createDirectory(atPath: testDir2.path, withIntermediateDirectories: true)
668672
try fm.createSymbolicLink(atPath: testDir2.appendingPathComponent("bar2").path, withDestinationPath: "foo1")
@@ -696,8 +700,57 @@ class TestFileManager : XCTestCase {
696700
XCTAssertFalse(fm.contentsEqual(atPath: testFile1URL.path, andPath: testFile2URL.path))
697701
XCTAssertFalse(fm.contentsEqual(atPath: testFile3URL.path, andPath: testFile4URL.path))
698702
XCTAssertFalse(fm.contentsEqual(atPath: symlink, andPath: testFile1URL.path))
703+
699704
XCTAssertTrue(fm.contentsEqual(atPath: testDir1.path, andPath: testDir1.path))
700705
XCTAssertTrue(fm.contentsEqual(atPath: testDir2.path, andPath: testDir3.path))
701706
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
707+
708+
// Copy everything in testDir1 to testDir2 to make them equal
709+
do {
710+
for entry in try fm.subpathsOfDirectory(atPath: testDir1.path) {
711+
// Skip entries that already exist
712+
if entry == "bar2" || entry == "unreadable_file" {
713+
continue
714+
}
715+
let srcPath = testDir1.appendingPathComponent(entry).path
716+
let dstPath = testDir2.appendingPathComponent(entry).path
717+
if let attrs = try? fm.attributesOfItem(atPath: srcPath),
718+
let fileType = attrs[.type] as? FileAttributeType, fileType == .typeDirectory {
719+
try fm.createDirectory(atPath: dstPath, withIntermediateDirectories: false, attributes: nil)
720+
} else {
721+
try fm.copyItem(atPath: srcPath, toPath: dstPath)
722+
}
723+
}
724+
} catch {
725+
XCTFail("Failed to copy \(testDir1.path) to \(testDir2.path), \(error)")
726+
return
727+
}
728+
// This will still fail due to unreadable files and a file in testDir2 not in testDir1
729+
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
730+
do {
731+
try fm.copyItem(atPath: testDir2.appendingPathComponent("foo2").path, toPath: testDir1.appendingPathComponent("foo2").path)
732+
try fm.removeItem(atPath: testDir1.appendingPathComponent("unreadable_file").path)
733+
} catch {
734+
XCTFail(String(describing: error))
735+
return
736+
}
737+
XCTAssertTrue(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
738+
739+
let dataFile1 = testDir1.appendingPathComponent("dataFile")
740+
let dataFile2 = testDir2.appendingPathComponent("dataFile")
741+
do {
742+
try Data(count: 100_000).write(to: dataFile1)
743+
try fm.copyItem(atPath: dataFile1.path, toPath: dataFile2.path)
744+
} catch {
745+
XCTFail("Could not create test data files: \(error)")
746+
return
747+
}
748+
XCTAssertTrue(fm.contentsEqual(atPath: dataFile1.path, andPath: dataFile2.path))
749+
XCTAssertTrue(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
750+
var data = Data(count: 100_000)
751+
data[99_999] = 1
752+
try? data.write(to: dataFile1)
753+
XCTAssertFalse(fm.contentsEqual(atPath: dataFile1.path, andPath: dataFile2.path))
754+
XCTAssertFalse(fm.contentsEqual(atPath: testDir1.path, andPath: testDir2.path))
702755
}
703756
}

0 commit comments

Comments
 (0)