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.

Start every test program the same way, its signature checked

Țugui Dragoș1bd3b7feeb17
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
index f5d5c368..ef73d882 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OpenFilesTests.swift
@@ -168,7 +168,7 @@ struct OpenFilesTests {
let directory = try TemporaryDirectory()
let log = try directory.file("home/.gradle/daemon/9.2/daemon-1.out.log")
let main = "org.gradle.launcher.daemon.bootstrap.GradleDaemon"
- let daemon = try Self.program("jdk/bin/java", in: directory, arguments: ["-", main, "9.2"], writingTo: log)
+ let daemon = try directory.runningProgram("jdk/bin/java", arguments: ["-", main, "9.2"], writingTo: log)
defer { daemon.terminate() }
let refusal = OpenFiles().refusal(of: log.deletingLastPathComponent(), lettingItsProgramsRun: false)
@@ -197,9 +197,9 @@ struct OpenFilesTests {
let script = try directory.file("home/.npm/_npx/0123abcd/node_modules/.bin/server")
let errors = try directory.file("errors.log")
let arguments = ["-", script.path(percentEncoded: false)]
- let node = try Self.program("bin/node", in: directory, arguments: arguments, writingTo: errors)
+ let node = try directory.runningProgram("bin/node", arguments: arguments, writingTo: errors)
defer { node.terminate() }
- let viewer = try Self.program("bin/viewer", in: directory, arguments: arguments, writingTo: errors)
+ let viewer = try directory.runningProgram("bin/viewer", arguments: arguments, writingTo: errors)
defer { viewer.terminate() }
let refusal = OpenFiles().refusal(of: directory.url.appending(path: "home/.npm/_npx"), lettingItsProgramsRun: false)
@@ -248,37 +248,6 @@ struct OpenFilesTests {
/// A copy of `cat` at `path`, started with `arguments` and holding `file` open for writing as its error output. It
/// echoes a line before this returns, so macOS has let it run, and reads its input until the test closes it.
- private static func program(
- _ path: String, in directory: borrowing TemporaryDirectory, arguments: [String], writingTo file: URL
- ) throws -> Process {
- let program = directory.url.appending(path: path)
- try FileManager.default.createDirectory(
- at: program.deletingLastPathComponent(), withIntermediateDirectories: true
- )
- try FileManager.default.copyItem(at: URL(filePath: "/bin/cat"), to: program)
- let signing = try Process.run(
- URL(filePath: "/usr/bin/codesign"), arguments: ["--force", "--sign", "-", program.path(percentEncoded: false)]
- )
- signing.waitUntilExit()
- let input = Pipe()
- let output = Pipe()
- let errors = try FileHandle(forWritingTo: file)
- defer { try? errors.close() }
- let line = Data("running\n".utf8)
- try input.fileHandleForWriting.write(contentsOf: line)
- let process = Process()
- process.executableURL = program
- process.arguments = arguments
- process.standardInput = input
- process.standardOutput = output
- process.standardError = errors
- try process.run()
- guard try output.fileHandleForReading.read(upToCount: line.count) == line else {
- throw CocoaError(.executableLoad, userInfo: [NSFilePathErrorKey: program.path])
- }
- return process
- }
-
/// `iostat`, run from where macOS keeps it, writing into `file`, which it holds open until it ends five minutes on.
private static func macOSProgram(writingTo file: URL) throws -> Process {
let process = Process()
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift b/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
index 1952a3c5..71095a99 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TemporaryDirectory.swift
@@ -86,45 +86,68 @@ struct TemporaryDirectory: ~Copyable {
try runningPrograms([path])[0]
}
+ /// `runningProgram(_:)` started with `arguments`, its standard error going to `errors`, which it holds open while
+ /// it runs. With `-` first, `cat` reads its input and never reaches the files named after it.
+ func runningProgram(_ path: String, arguments: [String], writingTo errors: URL) throws -> Process {
+ let program = try signedCopyOfCat(at: path)
+ let handle = try FileHandle(forWritingTo: errors)
+ defer { try? handle.close() }
+ return try Self.echoed([Self.start(program, arguments: arguments, errors: handle)])[0]
+ }
+
/// Every copy is made before any starts: once a program inside an app starts, macOS may refuse to write into it.
/// Each is returned once it has echoed a line, so macOS has let it run: until then Gatekeeper reads it where it
/// started, and ends it if the test has moved it.
func runningPrograms(_ paths: [String]) throws -> [Process] {
- let programs = try paths.map { path in
- let program = url.appending(path: path)
- try FileManager.default.createDirectory(
- at: program.deletingLastPathComponent(),
- withIntermediateDirectories: true
- )
- try FileManager.default.copyItem(at: URL(filePath: "/bin/cat"), 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 program
+ var programs: [URL] = []
+ for path in paths {
+ programs.append(try signedCopyOfCat(at: path))
}
- let line = Data("running\n".utf8)
- let started = try programs.map { program in
- let input = Pipe()
- let output = Pipe()
- try input.fileHandleForWriting.write(contentsOf: line)
- let process = Process()
- process.executableURL = program
- process.standardInput = input
- process.standardOutput = output
- try process.run()
- return (process, output.fileHandleForReading)
+ return try Self.echoed(programs.map { try Self.start($0) })
+ }
+
+ private static let line = Data("running\n".utf8)
+
+ private func signedCopyOfCat(at path: String) throws -> URL {
+ let program = url.appending(path: path)
+ try FileManager.default.createDirectory(
+ at: program.deletingLastPathComponent(), withIntermediateDirectories: true
+ )
+ try FileManager.default.copyItem(at: URL(filePath: "/bin/cat"), 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 program
+ }
+
+ private static func start(
+ _ program: URL, arguments: [String] = [], errors: FileHandle? = nil
+ ) throws -> (process: Process, echo: FileHandle) {
+ let input = Pipe()
+ let output = Pipe()
+ try input.fileHandleForWriting.write(contentsOf: line)
+ let process = Process()
+ process.executableURL = program
+ process.arguments = arguments
+ process.standardInput = input
+ process.standardOutput = output
+ if let errors { process.standardError = errors }
+ try process.run()
+ return (process, output.fileHandleForReading)
+ }
+
+ private static func echoed(_ started: [(process: Process, echo: FileHandle)]) throws -> [Process] {
for (process, echo) in started {
guard try echo.read(upToCount: line.count) == line else {
throw CocoaError(.executableLoad, userInfo: [NSFilePathErrorKey: process.executableURL?.path ?? ""])
}
}
- return started.map(\.0)
+ return started.map(\.process)
}
func setPermissions(_ permissions: Int, of path: String) throws {