Implement watch exclusion lists
Allow exclusion of directories when watching recursively.
This commit is contained in:
17
Tests/InotifyIntegrationTests/DirectoryResolverTests.swift
Normal file
17
Tests/InotifyIntegrationTests/DirectoryResolverTests.swift
Normal 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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user