Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
test(app): keep test preference suites out of ~/Library/Preferences (#2341)
Raystorms3f4d217fa588
diff --git a/app/Tests/BridgeplayAppTests/CrossOverRowTests.swift b/app/Tests/BridgeplayAppTests/CrossOverRowTests.swiftindex d89da89..4a89f66 100644--- a/app/Tests/BridgeplayAppTests/CrossOverRowTests.swift+++ b/app/Tests/BridgeplayAppTests/CrossOverRowTests.swift@@ -134,16 +134,9 @@ struct CrossOverRowTests {@Suite("Manually added CrossOver copies")struct ManualCrossOverTests {- private func defaults() -> UserDefaults {- let name = "np-manual-\(UUID().uuidString)"- let defaults = UserDefaults(suiteName: name)!- defaults.removePersistentDomain(forName: name)- return defaults- }-@Test("The single copy 1.0 remembered carries over as the first entry")func legacyChoiceCarriesOver() {- let defaults = defaults()+ let defaults = scratchDefaults()defaults.set("/Volumes/Spare/CrossOver.app", forKey: "manualCrossOverPath")#expect(CrossOverSource.manualBundles(defaults).map { $0.path(percentEncoded: false) }@@ -157,7 +150,7 @@ struct ManualCrossOverTests {@Test("Adding keeps earlier copies, skips repeats, and removing drops only the one named")func addAndRemove() {- let defaults = defaults()+ let defaults = scratchDefaults()let spare = URL(filePath: "/Volumes/Spare/CrossOver.app", directoryHint: .isDirectory)let other = URL(filePath: "/Volumes/Other/CrossOver Preview.app", directoryHint: .isDirectory)diff --git a/app/Tests/BridgeplayAppTests/PlayReportsTests.swift b/app/Tests/BridgeplayAppTests/PlayReportsTests.swiftindex 166968d..acb0267 100644--- a/app/Tests/BridgeplayAppTests/PlayReportsTests.swift+++ b/app/Tests/BridgeplayAppTests/PlayReportsTests.swift@@ -209,10 +209,8 @@ struct PlayReportsTests {}@Test("A report says when a newer Bridgeplay was out")- func newerRelease() throws {- let name = "latest-release-\(UUID().uuidString)"- let defaults = try #require(UserDefaults(suiteName: name))- defer { defaults.removePersistentDomain(forName: name) }+ func newerRelease() {+ let defaults = scratchDefaults()#expect(LatestRelease.newer(than: "44", defaults: defaults) == nil)LatestRelease.record(build: "55", version: "0.7.38", defaults: defaults)#expect(LatestRelease.newer(than: "44", defaults: defaults) == "0.7.38 (55)")@@ -344,7 +342,7 @@ struct PlayReportsFlowTests {private func model(_ path: String, queue: URL, version: String = "3.4.0",defaults: UserDefaults? = nil) -> (PlayReports, UserDefaults) {- let defaults = defaults ?? UserDefaults(suiteName: "play-reports-\(UUID().uuidString)")!+ let defaults = defaults ?? scratchDefaults()return (PlayReports(defaults: defaults, version: version, sender: { FakeEndpoint.sender(path, queue: queue) }), defaults)}diff --git a/app/Tests/BridgeplayAppTests/ScratchDefaultsTests.swift b/app/Tests/BridgeplayAppTests/ScratchDefaultsTests.swiftnew file mode 100644index 0000000..a2a1835--- /dev/null+++ b/app/Tests/BridgeplayAppTests/ScratchDefaultsTests.swift@@ -0,0 +1,22 @@+import Foundation+import Testing++@testable import BridgeplayApp++@Suite("Scratch defaults")+struct ScratchDefaultsTests {++ // The reason the helper exists: a named suite would leave a plist in ~/Library/Preferences,+ // so the suite's own file has to show up at the path it was given.+ @Test("A suite keeps its plist in the temporary folder")+ func staysOutOfPreferences() throws {+ let (defaults, plist) = scratchDefaultsAndPlist()+ defaults.set("value", forKey: "key")+ #expect(defaults.string(forKey: "key") == "value")+ defaults.synchronize()++ // The write is asynchronous, so wait for the plist to show up where it belongs.+ for _ in 0..<200 where !FileManager.default.fileExists(atPath: plist) { Thread.sleep(forTimeInterval: 0.1) }+ #expect(FileManager.default.fileExists(atPath: plist))+ }+}diff --git a/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift b/app/Tests/BridgeplayAppTests/SetupAssistantTests.swiftindex e6efd3c..ff5138f 100644--- a/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift+++ b/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift@@ -90,8 +90,8 @@ struct SetupAssistantTests {@Test("The assistant stays away once the user finished or dismissed it")@MainActor- func finishedFlag() throws {- let defaults = try #require(UserDefaults(suiteName: "setup-\(UUID().uuidString)"))+ func finishedFlag() {+ let defaults = scratchDefaults()defaults.set(true, forKey: SetupAssistant.finishedKey)#expect(!SetupAssistant.shouldShow(nil, defaults: defaults))}diff --git a/app/Tests/BridgeplayAppTests/TestSupport.swift b/app/Tests/BridgeplayAppTests/TestSupport.swiftindex d566cf9..5522b00 100644--- a/app/Tests/BridgeplayAppTests/TestSupport.swift+++ b/app/Tests/BridgeplayAppTests/TestSupport.swift@@ -21,3 +21,42 @@ func isolatedValveDownloads(in work: URL) throws -> URL {}return downloads}++// A UserDefaults that lives in a temporary folder, not in ~/Library/Preferences. Even after+// removePersistentDomain, a named suite leaves its plist behind there, one file per test run.+// The folder is deleted when the test process exits; ScratchDefaultsTests checks the location.+func scratchDefaults() -> UserDefaults {+ scratchDefaultsAndPlist().defaults+}++// The same, with the path its plist will be written to.+func scratchDefaultsAndPlist() -> (defaults: UserDefaults, plist: String) {+ let name = ScratchDefaults.folder.appending(path: UUID().uuidString).path(percentEncoded: false)+ // An absolute path as the suite name keeps the plist at that path.+ guard let defaults = UserDefaults(suiteName: name) else {+ fatalError("UserDefaults refused the suite \(name)")+ }+ return (defaults, name + ".plist")+}++enum ScratchDefaults {+ static let folder: URL = {+ let fm = FileManager.default+ let url = URL.temporaryDirectory.appending(path: "np-defaults-\(UUID().uuidString)")+ atexit { try? FileManager.default.removeItem(at: ScratchDefaults.folder) }++ // A run that crashed or was killed never reached the exit hook; clear what it left.+ // A week is far longer than any test run.+ let stale = Date().addingTimeInterval(-7 * 24 * 3600)+ for old in (try? fm.contentsOfDirectory(+ at: URL.temporaryDirectory, includingPropertiesForKeys: [.contentModificationDateKey])) ?? []+ where old.lastPathComponent.hasPrefix("np-defaults-") {+ let modified = try? old.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate+ if let modified, modified < stale { try? fm.removeItem(at: old) }+ }+ do { try fm.createDirectory(at: url, withIntermediateDirectories: true) } catch {+ fatalError("Cannot create scratch defaults folder \(url.path): \(error)")+ }+ return url+ }()+}diff --git a/app/Tests/BridgeplayAppTests/UpdateFlowTests.swift b/app/Tests/BridgeplayAppTests/UpdateFlowTests.swiftindex 0af65eb..6fd1dde 100644--- a/app/Tests/BridgeplayAppTests/UpdateFlowTests.swift+++ b/app/Tests/BridgeplayAppTests/UpdateFlowTests.swift@@ -179,34 +179,27 @@ struct UpdateFlowTests {@Suite("What's New")struct WhatsNewTests {- private func defaults() throws -> UserDefaults {- let name = "WhatsNewTests.\(UUID().uuidString)"- let defaults = try #require(UserDefaults(suiteName: name))- defaults.removePersistentDomain(forName: name)- return defaults- }-- @Test func showsOncePerBuild() throws {- let defaults = try defaults()+ @Test func showsOncePerBuild() {+ let defaults = scratchDefaults()LatestRelease.record(build: "77", version: "0.7.60", notes: "- fix(panel): show the menu (#1)", defaults: defaults)let first = LatestRelease.takeWhatsNew(build: "77", defaults: defaults)#expect(first == WhatsNew(version: "0.7.60", changes: ["Show the menu"]))#expect(LatestRelease.takeWhatsNew(build: "77", defaults: defaults) == nil)}- @Test func waitsForTheBuildItDescribes() throws {- let defaults = try defaults()+ @Test func waitsForTheBuildItDescribes() {+ let defaults = scratchDefaults()LatestRelease.record(build: "77", version: "0.7.60", notes: "- fix: a", defaults: defaults)#expect(LatestRelease.takeWhatsNew(build: "76", defaults: defaults) == nil)#expect(LatestRelease.takeWhatsNew(build: "77", defaults: defaults) != nil)}- @Test func nothingRecordedShowsNothing() throws {- #expect(LatestRelease.takeWhatsNew(build: "77", defaults: try defaults()) == nil)+ @Test func nothingRecordedShowsNothing() {+ #expect(LatestRelease.takeWhatsNew(build: "77", defaults: scratchDefaults()) == nil)}- @Test func aReleaseWithoutNotesStillSaysItUpdated() throws {- let defaults = try defaults()+ @Test func aReleaseWithoutNotesStillSaysItUpdated() {+ let defaults = scratchDefaults()LatestRelease.record(build: "77", version: "0.7.60", defaults: defaults)#expect(LatestRelease.takeWhatsNew(build: "77", defaults: defaults)?.changes == [])}