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 app's tests in a host of their own, so it never quits under them

Țugui Dragoș32417c2a9c21
diff --git a/Peel/Launch.swift b/Peel/Launch.swift
new file mode 100644
index 00000000..c6cdeb21
--- /dev/null
+++ b/Peel/Launch.swift
@@ -0,0 +1,35 @@
+import AppKit
+import SwiftUI
+
+/// Where Peel starts: Peel itself, or, for the PeelTests scheme, a host that only runs the tests.
+@main
+enum Launch {
+ static func main() {
+ if PeelApp.isHostingTests {
+ TestHost.main()
+ } else {
+ PeelApp.main()
+ }
+ }
+}
+
+/// The app the tests of Peel's models run in. It builds none of Peel's scenes, so nothing of Peel's starts or shows,
+/// and AppKit records nothing under Peel's name, such as whether its menu bar item is hidden.
+struct TestHost: App {
+ @NSApplicationDelegateAdaptor(TestHostDelegate.self) private var delegate
+
+ var body: some Scene {
+ Settings { EmptyView() }
+ }
+}
+
+final class TestHostDelegate: NSObject, NSApplicationDelegate {
+ func applicationWillFinishLaunching(_ notification: Notification) {
+ NSApp.setActivationPolicy(.prohibited)
+ }
+
+ /// AppKit asks this with no window open, and the test runner ends the host once the tests are done.
+ func applicationShouldTerminateAfterLastWindowClosed(_ application: NSApplication) -> Bool {
+ false
+ }
+}
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index f97fa86d..e7c53f02 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -6,14 +6,7 @@ import SwiftUI
/// Peel keeps running without a window only while it shows in the menu bar, so it is never running unseen.
@MainActor
final class PeelAppDelegate: NSObject, NSApplicationDelegate {
- func applicationWillFinishLaunching(_ notification: Notification) {
- if PeelApp.isHostingTests {
- NSApp.setActivationPolicy(.prohibited)
- }
- }
-
func applicationDidFinishLaunching(_ notification: Notification) {
- guard !PeelApp.isHostingTests else { return }
DockPresence.shared.start()
}
@@ -26,10 +19,8 @@ final class PeelAppDelegate: NSObject, NSApplicationDelegate {
}
}
-@main
struct PeelApp: App {
- /// Set by the PeelTests scheme for the Peel that hosts the tests, which drive its models alone: nothing of Peel's
- /// runs or shows then.
+ /// Set by the PeelTests scheme for the Peel that hosts the tests, which then runs `TestHost` in Peel's place.
static let isHostingTests = ProcessInfo.processInfo.environment["PEEL_HOSTS_TESTS"] != nil
@NSApplicationDelegateAdaptor(PeelAppDelegate.self) private var delegate
@@ -321,7 +312,6 @@ struct PeelApp: App {
/// Starts what Peel does from launch to quit, from whichever shows first: the main window, or the menu bar item.
/// Each piece starts once, and none is a child of the view that called it, so closing the window stops nothing.
private func start() {
- guard !Self.isHostingTests else { return }
#if DEBUG
// A render or an export only draws and quits, so it contacts nothing and writes nothing of Peel's.
guard HomeSnapshot.directory == nil, TerminalExport.directory == nil else { return }
@@ -382,8 +372,7 @@ struct PeelApp: App {
/// Whether the menu bar item shows. `MenuBarExtra` writes it back at every scene update, and each write to an
/// `AppStorage` makes every `AppStorage` in the app read again, so a write of the value it holds is left out.
private var menuBarItemIsInserted: Binding<Bool> {
- guard !Self.isHostingTests else { return .constant(false) }
- return Binding {
+ Binding {
showsInMenuBar
} set: { isInserted in
if isInserted != showsInMenuBar { showsInMenuBar = isInserted }
@@ -450,8 +439,6 @@ struct PeelApp: App {
}
}
.defaultSize(width: 1120, height: 764)
- .defaultLaunchBehavior(Self.isHostingTests ? .suppressed : .automatic)
- .restorationBehavior(Self.isHostingTests ? .disabled : .automatic)
.commands {
PeelCommands(
library: library, updater: updater, homebrew: homebrew, textEditing: textEditing,
diff --git a/PeelTests/HostingTests.swift b/PeelTests/HostingTests.swift
index b63c2251..c8fa9ae2 100644
--- a/PeelTests/HostingTests.swift
+++ b/PeelTests/HostingTests.swift
@@ -12,5 +12,11 @@ struct HostingTests {
$0[kCGWindowOwnerPID as String] as? pid_t == ProcessInfo.processInfo.processIdentifier
}
#expect(own.isEmpty)
+ // AppKit records whether a status item is hidden in Peel's own preferences, which the Mac's Peel reads.
+ #expect(!NSApp.windows.contains { $0.className == "NSStatusBarWindow" })
+ }
+
+ @Test func theHostNeverQuitsByItself() {
+ #expect(NSApp.delegate?.applicationShouldTerminateAfterLastWindowClosed?(NSApp) == false)
}
}