refactor(uninstaller): 移除无调用方的应用列表接口并放宽取消测试

- 删除吞错的 installedApps(home:excluding:),统一使用可取消的 scanInstalledApps
- 取消测试改为按完整扫描检查点数在中途取消,不再绑定具体检查次数

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ysicingandClaude Opus 5.5 authored and 缘生 committed 2026-09-30 15:20:08 +08:00
1 parent bc2bfe8f5c
commit 036a4a6598
2 files changed
+9 -8

No files matched your search

@@ -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<String> = []) -> [InstalledApp] {
(try? installedApps(in: applicationDirectories(home: home), excluding: excludedIdentifiers, checkCancellation: {})) ?? []
}
/// 可取消的应用列表扫描;遍历中止时抛错,不发布部分列表。
/// /Applications 与 ~/Applications 下的应用(含一层子文件夹),按名称排序。
/// 可取消;遍历中止时抛错,不发布部分列表。
public static func scanInstalledApps(home: String = NSHomeDirectory(), excluding excludedIdentifiers: Set<String> = []) throws -> [InstalledApp] {
try installedApps(in: applicationDirectories(home: home), excluding: excludedIdentifiers,
checkCancellation: { try Task.checkCancellation() })
@@ -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" })
}