Implement watch exclusion lists
Some checks failed
Docs / docs (push) Has been cancelled
Docs / deploy (push) Has been cancelled

Allow exclusion of directories when watching recursively.
This commit is contained in:
T. R. Bernstein
2026-03-15 22:30:10 +01:00
parent c87099e4a7
commit 134e4e152d
8 changed files with 115 additions and 25 deletions

View File

@@ -0,0 +1,17 @@
import Foundation
import Testing
@testable import Inotify
@Suite("Directory Resolver")
struct DirectoryResolverTests {
@Test func listsDirectoryTree() async throws {
try await withTempDir { dir in
let subDirectory = "\(dir)/Subfolder/Folder 01"
try FileManager.default.createDirectory(atPath: subDirectory, withIntermediateDirectories: true)
let directories = try await DirectoryResolver.resolve(dir)
#expect(directories.count == 3)
#expect(directories.map { $0.description } == [dir, "\(dir)/Subfolder", subDirectory])
}
}
}

View File

@@ -21,6 +21,24 @@ struct RecursiveEventTests {
}
}
@Test func ignoresFileCreationInIgnoredSubfolder() async throws {
try await withTempDir { dir in
let subDirectory = "\(dir)/Subfolder"
let filepath = "\(subDirectory)/modify-target.txt"
try FileManager.default.createDirectory(atPath: subDirectory, withIntermediateDirectories: true)
let events = try await getEventsForTrigger(
in: dir,
mask: [.create],
recursive: .recursive,
exclude: ["Subfolder"]
) { _ in try createFile(at: "\(filepath)", contents: "hello") }
let createEvent = events.first { $0.mask.contains(.create) && $0.path.string == filepath }
#expect(createEvent == nil, "Did not expect CREATE for '\(filepath)', got: \(events)")
}
}
@Test func newSubfoldersOfRecursiveWatchAreAutomaticallyWatchedToo() async throws {
try await withTempDir { dir in
let subDirectory = "\(dir)/Subfolder"
@@ -32,7 +50,7 @@ struct RecursiveEventTests {
recursive: .withAutomaticSubtreeWatching
) { _ in
try FileManager.default.createDirectory(atPath: subDirectory, withIntermediateDirectories: true)
try await Task.sleep(for: .milliseconds(200))
try await Task.sleep(for: .milliseconds(400))
try createFile(at: "\(filepath)", contents: "hello")
}

View File

@@ -10,9 +10,11 @@ func getEventsForTrigger(
in dir: String,
mask: InotifyEventMask,
recursive: RecursivKind = .nonrecursive,
exclude: [String] = [],
trigger: @escaping (String) async throws -> Void,
) async throws -> [InotifyEvent] {
let watcher = try Inotify()
await watcher.exclude(names: exclude)
switch recursive {
case .nonrecursive:
try await watcher.addWatch(path: dir, mask: mask)