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" }) }