Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
The Trash tests move on a real exFAT disk
Țugui Dragoș4ee94683aace
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashMoverTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashMoverTests.swiftindex bb978f1d..825388ae 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashMoverTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashMoverTests.swift@@ -5,26 +5,31 @@ import Foundationimport Testing/// Moves into the Trash, and back out of it, never replace what is already at the new name. exFAT cannot refuse to-/// replace by itself: `RENAME_EXCL` answers `ENOTSUP` there for a free name and `EEXIST` for a taken one, and these-/// tests stand in for it.+/// replace by itself: `RENAME_EXCL` answers `ENOTSUP` there for a free name and `EEXIST` for a taken one. So these+/// tests run on an exFAT disk made for them.struct TrashMoverTests {- private func likeExFAT(_ item: OpenItem, _ name: String, _ directory: DirectoryHandle) -> Result<Void, POSIXError> {- var info = stat()- let isTaken = name.withCString { fstatat(directory.descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) } == 0- return .failure(POSIXError(isTaken ? .EEXIST : .ENOTSUP))+ private func contents(of url: URL) throws -> [String] {+ try FileManager.default.contentsOfDirectory(atPath: url.path(percentEncoded: false)).filter { !$0.hasPrefix(".") }.sorted()}- private func contents(of url: URL) throws -> [String] {- try FileManager.default.contentsOfDirectory(atPath: url.path(percentEncoded: false)).sorted()+ private func write(_ text: String, at url: URL) throws {+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data(text.utf8).write(to: url)+ }++ private func folder(_ url: URL) throws -> DirectoryHandle {+ try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)+ return try DirectoryHandle.at(url.path(percentEncoded: false)).get()}@Test func aFileMovesWhereTheDiskCannotRefuseToReplace() throws {let directory = try TemporaryDirectory()- let file = try directory.file("folder/notes.txt", contents: Data("mine".utf8))- let trash = try directory.directory("Trash")- let item = try OpenItem.at(file.path(percentEncoded: false)).get()+ let disk = try ScratchVolume(fileSystem: "ExFAT", mountedAt: directory.url.appending(path: "Stick", directoryHint: .isDirectory))+ let file = disk.url.appending(path: "folder/notes.txt")+ try write("mine", at: file)+ let trash = disk.url.appending(path: "Trash", directoryHint: .isDirectory)- let landed = try TrashMover.move(item, into: try DirectoryHandle.at(trash.path(percentEncoded: false)).get(), exclusively: likeExFAT).get()+ let landed = try TrashMover.move(try OpenItem.at(file.path(percentEncoded: false)).get(), into: try folder(trash)).get()#expect(URL(filePath: landed).lastPathComponent == "notes.txt")#expect(try String(contentsOfFile: landed, encoding: .utf8) == "mine")@@ -34,12 +39,14 @@ struct TrashMoverTests {@Test func whatIsAlreadyThereIsNeverReplaced() throws {let directory = try TemporaryDirectory()- let file = try directory.file("folder/notes.txt", contents: Data("mine".utf8))- let earlier = try directory.file("Trash/notes.txt", contents: Data("in the Trash already".utf8))- let item = try OpenItem.at(file.path(percentEncoded: false)).get()+ let disk = try ScratchVolume(fileSystem: "ExFAT", mountedAt: directory.url.appending(path: "Stick", directoryHint: .isDirectory))+ let file = disk.url.appending(path: "folder/notes.txt")+ let earlier = disk.url.appending(path: "Trash/notes.txt")+ try write("mine", at: file)+ try write("in the Trash already", at: earlier)- let trash = try DirectoryHandle.at(earlier.deletingLastPathComponent().path(percentEncoded: false)).get()- let landed = try TrashMover.move(item, into: trash, exclusively: likeExFAT).get()+ let item = try OpenItem.at(file.path(percentEncoded: false)).get()+ let landed = try TrashMover.move(item, into: try folder(earlier.deletingLastPathComponent())).get()#expect(URL(filePath: landed).lastPathComponent == "notes 2.txt")#expect(try String(contentsOf: earlier, encoding: .utf8) == "in the Trash already")@@ -48,55 +55,65 @@ struct TrashMoverTests {@Test func aFolderMovesWithEverythingInIt() throws {let directory = try TemporaryDirectory()- try directory.file("folder/Project/one.txt", contents: Data("1".utf8))- try directory.file("folder/Project/deeper/two.txt", contents: Data("2".utf8))- let trash = try directory.directory("Trash")- let item = try OpenItem.at(directory.url.appending(path: "folder/Project").path(percentEncoded: false)).get()+ let disk = try ScratchVolume(fileSystem: "ExFAT", mountedAt: directory.url.appending(path: "Stick", directoryHint: .isDirectory))+ try write("1", at: disk.url.appending(path: "folder/Project/one.txt"))+ try write("2", at: disk.url.appending(path: "folder/Project/deeper/two.txt"))+ let trash = disk.url.appending(path: "Trash", directoryHint: .isDirectory)- let landed = URL(filePath: try TrashMover.move(item, into: try DirectoryHandle.at(trash.path(percentEncoded: false)).get(), exclusively: likeExFAT).get())+ let item = try OpenItem.at(disk.url.appending(path: "folder/Project").path(percentEncoded: false)).get()+ let landed = URL(filePath: try TrashMover.move(item, into: try folder(trash)).get())#expect(try String(contentsOf: landed.appending(path: "one.txt"), encoding: .utf8) == "1")#expect(try String(contentsOf: landed.appending(path: "deeper/two.txt"), encoding: .utf8) == "2")#expect(try contents(of: trash) == ["Project"])}- /// A move that fails once the new name is taken for it gives the name back, so neither the Trash nor the place an- /// item was being put back to is left holding an empty stand-in.+ @Test func putBackNeverReplacesWhatCameBackInTheMeantime() throws {+ let directory = try TemporaryDirectory()+ let disk = try ScratchVolume(fileSystem: "ExFAT", mountedAt: directory.url.appending(path: "Stick", directoryHint: .isDirectory))+ let trashed = disk.url.appending(path: "Trash/notes.txt")+ let another = disk.url.appending(path: "Trash/other.txt")+ try write("from the Trash", at: trashed)+ try write("another", at: another)+ let place = disk.url.appending(path: "folder", directoryHint: .isDirectory)+ let destination = try folder(place)++ try TrashMover.rename(try OpenItem.at(trashed.path(percentEncoded: false)).get(), to: "notes.txt", in: destination).get()+ #expect(try String(contentsOf: place.appending(path: "notes.txt"), encoding: .utf8) == "from the Trash")++ let second = try OpenItem.at(another.path(percentEncoded: false)).get()+ #expect(throws: POSIXError(.EEXIST)) { try TrashMover.rename(second, to: "notes.txt", in: destination).get() }+ #expect(try String(contentsOf: place.appending(path: "notes.txt"), encoding: .utf8) == "from the Trash")+ #expect(another.isThere)+ }++ /// A move that fails once its new name is taken gives the name back, so neither the Trash nor the place an item+ /// was being put back to keeps an empty stand-in. exFAT refuses no permission, so no rename there can be made to+ /// fail at that moment: here the startup disk plays exFAT, answering `ENOTSUP` for a free name, and a folder+ /// that cannot be changed makes the rename fail.@Test func aMoveThatFailsLeavesNothingBehind() throws {let directory = try TemporaryDirectory()let file = try directory.file("locked/notes.txt")- let folder = try directory.directory("Project/inside")+ let project = try directory.directory("Project/inside")let trash = try directory.directory("Trash")let item = try OpenItem.at(file.path(percentEncoded: false)).get()- let folderItem = try OpenItem.at(folder.path(percentEncoded: false)).get()+ let folderItem = try OpenItem.at(project.path(percentEncoded: false)).get()try directory.setPermissions(0o555, of: "locked")try directory.setPermissions(0o555, of: "Project")defer {try? directory.setPermissions(0o755, of: "locked")try? directory.setPermissions(0o755, of: "Project")}+ let likeExFAT: TrashMover.ExclusiveRename = { _, name, directory in+ var info = stat()+ let isTaken = name.withCString { fstatat(directory.descriptor, $0, &info, AT_SYMLINK_NOFOLLOW) } == 0+ return .failure(POSIXError(isTaken ? .EEXIST : .ENOTSUP))+ }let bin = try DirectoryHandle.at(trash.path(percentEncoded: false)).get()#expect(throws: POSIXError.self) { try TrashMover.move(item, into: bin, exclusively: likeExFAT).get() }#expect(throws: POSIXError.self) { try TrashMover.move(folderItem, into: bin, exclusively: likeExFAT).get() }#expect(try contents(of: trash).isEmpty)- #expect(file.isThere && folder.isThere)- }-- @Test func putBackNeverReplacesWhatCameBackInTheMeantime() throws {- let directory = try TemporaryDirectory()- let trashed = try directory.file("Trash/notes.txt", contents: Data("from the Trash".utf8))- let place = try directory.directory("folder")- let item = try OpenItem.at(trashed.path(percentEncoded: false)).get()- let folder = try DirectoryHandle.at(place.path(percentEncoded: false)).get()-- try TrashMover.rename(item, to: "notes.txt", in: folder, exclusively: likeExFAT).get()- #expect(try String(contentsOf: place.appending(path: "notes.txt"), encoding: .utf8) == "from the Trash")-- let another = try directory.file("Trash/other.txt", contents: Data("another".utf8))- let second = try OpenItem.at(another.path(percentEncoded: false)).get()- #expect(throws: POSIXError(.EEXIST)) { try TrashMover.rename(second, to: "notes.txt", in: folder, exclusively: likeExFAT).get() }- #expect(try String(contentsOf: place.appending(path: "notes.txt"), encoding: .utf8) == "from the Trash")- #expect(another.isThere)+ #expect(file.isThere && project.isThere)}}