From 036a4a6598d2087faa6f428231e65c0491d2dd1e Mon Sep 17 00:00:00 2001 From: ysicing Date: Wed, 30 Sep 2026 14:55:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(uninstaller):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=97=A0=E8=B0=83=E7=94=A8=E6=96=B9=E7=9A=84=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E5=B9=B6=E6=94=BE=E5=AE=BD?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除吞错的 installedApps(home:excluding:),统一使用可取消的 scanInstalledApps - 取消测试改为按完整扫描检查点数在中途取消,不再绑定具体检查次数 Co-Authored-By: Claude Opus 5.5 --- Packages/XStatsKit/Sources/Cleaner/AppUninstaller.swift | 8 ++------ .../Tests/CleanerTests/ScanCancellationTests.swift | 9 +++++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Packages/XStatsKit/Sources/Cleaner/AppUninstaller.swift b/Packages/XStatsKit/Sources/Cleaner/AppUninstaller.swift index f47ab5a..46ed01c 100644 --- a/Packages/XStatsKit/Sources/Cleaner/AppUninstaller.swift +++ b/Packages/XStatsKit/Sources/Cleaner/AppUninstaller.swift @@ -76,12 +76,8 @@ public enum AppUninstaller { [URL(fileURLWithPath: "/Applications"), URL(fileURLWithPath: home).appendingPathComponent("Applications")] } - /// /Applications 与 ~/Applications 下的应用(含一层子文件夹),按名称排序 - public static func installedApps(home: String = NSHomeDirectory(), excluding excludedIdentifiers: Set = []) -> [InstalledApp] { - (try? installedApps(in: applicationDirectories(home: home), excluding: excludedIdentifiers, checkCancellation: {})) ?? [] - } - - /// 可取消的应用列表扫描;遍历中止时抛错,不发布部分列表。 + /// /Applications 与 ~/Applications 下的应用(含一层子文件夹),按名称排序。 + /// 可取消;遍历中止时抛错,不发布部分列表。 public static func scanInstalledApps(home: String = NSHomeDirectory(), excluding excludedIdentifiers: Set = []) throws -> [InstalledApp] { try installedApps(in: applicationDirectories(home: home), excluding: excludedIdentifiers, checkCancellation: { try Task.checkCancellation() }) diff --git a/Packages/XStatsKit/Tests/CleanerTests/ScanCancellationTests.swift b/Packages/XStatsKit/Tests/CleanerTests/ScanCancellationTests.swift index 0e5f41c..bc3533a 100644 --- a/Packages/XStatsKit/Tests/CleanerTests/ScanCancellationTests.swift +++ b/Packages/XStatsKit/Tests/CleanerTests/ScanCancellationTests.swift @@ -18,14 +18,19 @@ struct ScanCancellationTests { try PropertyListSerialization.data(fromPropertyList: info, format: .xml, options: 0) .write(to: app.appendingPathComponent("Info.plist")) } + // 先统计完整扫描的检查点,再在中途取消;不依赖检查点的具体位置。 + var total = 0 + _ = try AppUninstaller.installedApps(in: [root], excluding: []) { total += 1 } + #expect(total > 20, "每个嵌套候选都应检查取消,实际检查 \(total) 次") + let cancelAt = total / 2 var checks = 0 #expect(throws: CancellationError.self) { try AppUninstaller.installedApps(in: [root], excluding: []) { checks += 1 - if checks == 7 { throw CancellationError() } + if checks == cancelAt { throw CancellationError() } } } - #expect(checks == 7) + #expect(checks == cancelAt, "取消后不应继续遍历") let apps = try AppUninstaller.installedApps(in: [root], excluding: ["test.list.0"], checkCancellation: {}) #expect(apps.count == 19 && !apps.contains { $0.bundleIdentifier == "test.list.0" }) }