218 lines
9.1 KiB
Swift
218 lines
9.1 KiB
Swift
import CoreServices
|
|
import Foundation
|
|
import HelderCore
|
|
@testable import HelderServices
|
|
import Testing
|
|
|
|
/// Collects watcher events from any queue.
|
|
private final class EventLog: @unchecked Sendable {
|
|
private let lock = NSLock()
|
|
private var events: [WatchEvent] = []
|
|
|
|
func append(_ event: WatchEvent) { lock.withLock { events.append(event) } }
|
|
func count(_ event: WatchEvent) -> Int { lock.withLock { events.filter { $0 == event }.count } }
|
|
func clear() { lock.withLock { events.removeAll() } }
|
|
}
|
|
|
|
/// Real FSEvents against temp folders; the waits are generous so a busy machine does not fail them.
|
|
@Suite(.serialized) struct FSEventsWatcherTests {
|
|
private let debounce = WatchDebounce(project: 0.3, git: 0.2, config: 0.1)
|
|
|
|
/// Starts a watcher and lets FSEvents settle so setup writes do not leak into the test.
|
|
private func watch(_ root: URL) async -> (FSEventsWatcher, EventLog) {
|
|
let log = EventLog()
|
|
let watcher = FSEventsWatcher(debounce: debounce)
|
|
watcher.start(root: root) { log.append($0) }
|
|
try? await Task.sleep(nanoseconds: 600_000_000)
|
|
log.clear()
|
|
return (watcher, log)
|
|
}
|
|
|
|
private func settle() async {
|
|
try? await Task.sleep(nanoseconds: 1_000_000_000)
|
|
}
|
|
|
|
@Test func aBurstOfProjectChangesFiresOneDebouncedEvent() async throws {
|
|
let dir = try TempDirectory()
|
|
let (watcher, log) = await watch(dir.root)
|
|
defer { watcher.stop() }
|
|
for i in 0..<5 { try dir.write("src/file\(i).txt", "\(i)") }
|
|
#expect(await eventually { log.count(.project) >= 1 })
|
|
await settle()
|
|
#expect(log.count(.project) == 1)
|
|
#expect(log.count(.git) == 0)
|
|
}
|
|
|
|
@Test func changesInsideIgnoredFoldersAreDropped() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.mkdir("node_modules/pkg")
|
|
try dir.mkdir("vendor")
|
|
let (watcher, log) = await watch(dir.root)
|
|
defer { watcher.stop() }
|
|
try dir.write("node_modules/pkg/index.js", "x")
|
|
try dir.write("vendor/autoload.php", "x")
|
|
try dir.write(".helder/recent.json", "[]")
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
}
|
|
|
|
@Test func theConfigFileFiresAConfigEventOnly() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.mkdir(".helder")
|
|
let (watcher, log) = await watch(dir.root)
|
|
defer { watcher.stop() }
|
|
try dir.write(".helder/config.json", #"{"editor":{"tabSize":2}}"#)
|
|
#expect(await eventually { log.count(.config) == 1 })
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
}
|
|
|
|
@Test func aCommitFiresAGitEvent() async throws {
|
|
let repo = try TempGitRepo()
|
|
try repo.commit(["a.txt": "a\n"])
|
|
let (watcher, log) = await watch(repo.root)
|
|
defer { watcher.stop() }
|
|
try repo.run("commit", "-q", "--allow-empty", "-m", "second")
|
|
#expect(await eventually { log.count(.git) >= 1 })
|
|
}
|
|
|
|
@Test func aCommitInAWorktreeFiresAGitEvent() async throws {
|
|
let repo = try TempGitRepo()
|
|
try repo.commit(["a.txt": "a\n"])
|
|
let worktree = repo.root.deletingLastPathComponent().appendingPathComponent("helder-wt-\(UUID().uuidString)")
|
|
defer { try? FileManager.default.removeItem(at: worktree) }
|
|
try repo.run("worktree", "add", "-q", "-b", "side", worktree.path)
|
|
let (watcher, log) = await watch(worktree)
|
|
defer { watcher.stop() }
|
|
try repo.run("-C", worktree.path, "commit", "-q", "--allow-empty", "-m", "in the worktree")
|
|
#expect(await eventually { log.count(.git) >= 1 })
|
|
}
|
|
|
|
@Test func stopSilencesTheWatcher() async throws {
|
|
let dir = try TempDirectory()
|
|
let (watcher, log) = await watch(dir.root)
|
|
watcher.stop()
|
|
try dir.write("after.txt", "x")
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
}
|
|
|
|
@Test func laravelCachesAndConfiguredExcludesAreDropped() async throws {
|
|
let dir = try TempDirectory()
|
|
try dir.write(".helder/config.json", #"{"files":{"exclude":["*.log"]}}"#)
|
|
try dir.mkdir("storage/framework/views")
|
|
try dir.mkdir("storage/logs")
|
|
let (watcher, log) = await watch(dir.root)
|
|
defer { watcher.stop() }
|
|
try dir.write("storage/framework/views/abc.php", "x")
|
|
try dir.write("storage/logs/laravel.log", "x")
|
|
try dir.write(".phpunit.result.cache", "x")
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
try dir.write("app/User.php", "x")
|
|
#expect(await eventually { log.count(.project) == 1 })
|
|
}
|
|
|
|
@Test func gitIgnoredChangesAreDroppedWhenTheProjectFollowsGitignore() async throws {
|
|
let repo = try TempGitRepo()
|
|
try repo.commit([".gitignore": "tmp/\n", ".helder/config.json": #"{"files":{"followGitignore":true}}"#])
|
|
try FileManager.default.createDirectory(at: repo.root.appendingPathComponent("tmp"), withIntermediateDirectories: true)
|
|
let (watcher, log) = await watch(repo.root)
|
|
defer { watcher.stop() }
|
|
try repo.write("tmp/a.txt", "x")
|
|
try repo.write("tmp/b.txt", "x")
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
try repo.write("tmp/c.txt", "x")
|
|
try repo.write("src/kept.txt", "x")
|
|
#expect(await eventually { log.count(.project) == 1 })
|
|
}
|
|
|
|
@Test func gitIgnoredChangesStillFireWhenTheProjectShowsIgnoredFiles() async throws {
|
|
let repo = try TempGitRepo()
|
|
try repo.commit([".gitignore": "tmp/\n"])
|
|
try FileManager.default.createDirectory(at: repo.root.appendingPathComponent("tmp"), withIntermediateDirectories: true)
|
|
let (watcher, log) = await watch(repo.root)
|
|
defer { watcher.stop() }
|
|
try repo.write("tmp/a.txt", "x")
|
|
#expect(await eventually { log.count(.project) == 1 })
|
|
}
|
|
|
|
@Test func startingAgainMovesTheWatchToTheNewRoot() async throws {
|
|
let first = try TempDirectory()
|
|
let second = try TempDirectory()
|
|
let (watcher, log) = await watch(first.root)
|
|
defer { watcher.stop() }
|
|
watcher.start(root: second.root) { log.append($0) }
|
|
try? await Task.sleep(nanoseconds: 600_000_000)
|
|
log.clear()
|
|
try first.write("old.txt", "x")
|
|
await settle()
|
|
#expect(log.count(.project) == 0)
|
|
try second.write("new.txt", "x")
|
|
#expect(await eventually { log.count(.project) == 1 })
|
|
}
|
|
}
|
|
|
|
@Suite struct EventRouterTests {
|
|
private let root = "/p"
|
|
private let git = "/p/.git"
|
|
private let none = FSEventStreamEventFlags(0)
|
|
|
|
private func classify(_ path: String, gitDirectory: String = "/p/.git", exclude: [String] = []) -> WatchEvent? {
|
|
EventRouter.classify(path: path, root: root, gitDirectory: gitDirectory, flags: none, exclude: exclude)
|
|
}
|
|
|
|
@Test func projectFilesAreProjectEvents() {
|
|
#expect(classify("/p/src/a.ts") == .project)
|
|
#expect(classify("/p/.env") == .project)
|
|
}
|
|
|
|
@Test func ignoredFoldersAtAnyDepthAreDropped() {
|
|
#expect(classify("/p/node_modules/x/y.js") == nil)
|
|
#expect(classify("/p/packages/web/dist/app.js") == nil)
|
|
#expect(classify("/p/.helder/recent.json") == nil)
|
|
#expect(classify("/p") == nil)
|
|
#expect(classify("/elsewhere/a.ts") == nil)
|
|
}
|
|
|
|
@Test func frameworkCachesAndExcludedGlobsAreDropped() {
|
|
#expect(classify("/p/storage/framework/cache/data/x") == nil)
|
|
#expect(classify("/p/bootstrap/cache/services.php") == nil)
|
|
#expect(classify("/p/storage/logs/laravel.log") == .project)
|
|
#expect(classify("/p/storage/logs/laravel.log", exclude: ["storage/logs"]) == nil)
|
|
}
|
|
|
|
@Test func theConfigFileIsAConfigEvent() {
|
|
#expect(classify("/p/.helder/config.json") == .config)
|
|
#expect(classify("/p/.helder/config.default.json") == nil)
|
|
}
|
|
|
|
@Test func onlyTheGitStateEntriesAreGitEvents() {
|
|
#expect(classify("/p/.git/HEAD") == .git)
|
|
#expect(classify("/p/.git/index") == .git)
|
|
#expect(classify("/p/.git/refs/heads/feature/x") == .git)
|
|
#expect(classify("/p/.git/refs/remotes/origin/main") == .git)
|
|
#expect(classify("/p/.git/logs/HEAD") == .git)
|
|
#expect(classify("/p/.git/MERGE_HEAD") == .git)
|
|
#expect(classify("/p/.git/objects/ab/cdef") == nil)
|
|
#expect(classify("/p/.git/index.lock") == nil)
|
|
#expect(classify("/p/.git/refs/tags/v1") == nil)
|
|
}
|
|
|
|
@Test func aWorktreeGitDirectoryOutsideTheRootIsWatchedToo() {
|
|
#expect(classify("/repo/.git/worktrees/wt/HEAD", gitDirectory: "/repo/.git/worktrees/wt") == .git)
|
|
#expect(classify("/p/.git", gitDirectory: "/repo/.git/worktrees/wt") == nil)
|
|
}
|
|
|
|
@Test func aDroppedEventBatchRescansTheProject() {
|
|
let flags = FSEventStreamEventFlags(kFSEventStreamEventFlagMustScanSubDirs)
|
|
#expect(EventRouter.classify(path: "/p/node_modules", root: root, gitDirectory: git, flags: flags) == .project)
|
|
}
|
|
|
|
@Test func realPathResolvesTheTempSymlinkEvenForAMissingFile() {
|
|
#expect(EventRouter.realPath("/var/nonexistent-helder-file") == "/private/var/nonexistent-helder-file")
|
|
#expect(EventRouter.realPath("/tmp") == "/private/tmp")
|
|
}
|
|
}
|