Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Run the test programs from a copy macOS lets start anywhere

Țugui Dragoșd410e2021620
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
index c796b09d..3972b9b7 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
@@ -40,12 +40,7 @@ struct OpenFilesTests {
/// A program running from inside a folder holds it, as an agent an app keeps in its support folder does.
@Test func seesAProgramRunningFromInsideAFolder() throws {
let directory = try TemporaryDirectory()
- let tool = try directory.directory("Support/Agent.app/Contents/MacOS").appending(path: "agent")
- try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: tool.path(percentEncoded: false))
- let process = Process()
- process.executableURL = tool
- process.arguments = ["30"]
- try process.run()
+ let process = try directory.runningProgram("Support/Agent.app/Contents/MacOS/agent")
defer { process.terminate() }
let folder = directory.url.appending(path: "Support", directoryHint: .isDirectory)
@@ -77,13 +72,7 @@ struct OpenFilesTests {
/// An app extension runs on its app's behalf and macOS ends it once the app goes, so it holds nothing.
@Test func anAppExtensionRunningFromInsideAnAppHoldsNothing() throws {
let directory = try TemporaryDirectory()
- let tool = try directory.directory("Example.app/Contents/PlugIns/Share.appex/Contents/MacOS")
- .appending(path: "share")
- try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: tool.path(percentEncoded: false))
- let process = Process()
- process.executableURL = tool
- process.arguments = ["30"]
- try process.run()
+ let process = try directory.runningProgram("Example.app/Contents/PlugIns/Share.appex/Contents/MacOS/share")
defer { process.terminate() }
#expect(OpenFiles(excluding: nil).holders(of: directory.url.appending(path: "Example.app")).isEmpty)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
index e8ea4612..47ffb085 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHygieneTests.swift
@@ -588,12 +588,7 @@ struct PrivacyResetTests {
let directory = try TemporaryDirectory()
let held = InstalledApp(url: try directory.directory("Applications/Held.app"), bundleIdentifier: "org.example.held", name: "Held")
let free = InstalledApp(url: try directory.directory("Applications/Free.app"), bundleIdentifier: "org.example.free", name: "Free")
- let program = try directory.directory("Applications/Held.app/Contents/MacOS").appending(path: "held")
- try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: program.path(percentEncoded: false))
- let process = Process()
- process.executableURL = program
- process.arguments = ["30"]
- try process.run()
+ let process = try directory.runningProgram("Applications/Held.app/Contents/MacOS/held")
defer { process.terminate() }
#expect(PrivacyReset.goingNow([held, free], with: service(in: directory)).map(\.bundleIdentifier) == ["org.example.free"])
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift b/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
index eb9b1219..945e88c8 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
@@ -80,6 +80,26 @@ struct TemporaryDirectory: ~Copyable {
return String(decoding: help, as: UTF8.self).contains("--noclone")
}()
+ /// macOS kills a copy of one of its own programs started from anywhere else (a launch constraint), so the copy of
+ /// `sleep` is signed ad hoc first.
+ func runningProgram(_ path: String) throws -> Process {
+ let program = url.appending(path: path)
+ try FileManager.default.createDirectory(
+ at: program.deletingLastPathComponent(),
+ withIntermediateDirectories: true
+ )
+ try FileManager.default.copyItem(at: URL(filePath: "/bin/sleep"), to: program)
+ let signing = try Process.run(
+ URL(filePath: "/usr/bin/codesign"),
+ arguments: ["--force", "--sign", "-", program.path(percentEncoded: false)]
+ )
+ signing.waitUntilExit()
+ guard signing.terminationStatus == 0 else {
+ throw CocoaError(.executableLoad, userInfo: [NSFilePathErrorKey: program.path])
+ }
+ return try Process.run(program, arguments: ["30"])
+ }
+
func setPermissions(_ permissions: Int, of path: String) throws {
try setPermissions(permissions, of: url.appending(path: path))
}