Implement recursive watching of a directory
This commit is contained in:
23
Tests/InotifyIntegrationTests/RecursiveEventTests.swift
Normal file
23
Tests/InotifyIntegrationTests/RecursiveEventTests.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import Inotify
|
||||
|
||||
@Suite("Recursive Event Detection")
|
||||
struct RecursiveEventTests {
|
||||
@Test func detectsFileCreationInSubfolder() 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: true
|
||||
) { _ in try createFile(at: "\(filepath)", contents: "hello") }
|
||||
|
||||
let createEvent = events.first { $0.mask.contains(.create) && $0.path.string == filepath }
|
||||
#expect(createEvent != nil, "Expected CREATE for '\(filepath)', got: \(events)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,15 @@ import Inotify
|
||||
func getEventsForTrigger(
|
||||
in dir: String,
|
||||
mask: InotifyEventMask,
|
||||
trigger: @escaping (String) async throws -> Void
|
||||
recursive: Bool = false,
|
||||
trigger: @escaping (String) async throws -> Void,
|
||||
) async throws -> [InotifyEvent] {
|
||||
let watcher = try Inotify()
|
||||
try await watcher.addWatch(path: dir, mask: mask)
|
||||
if recursive {
|
||||
try await watcher.addRecursiveWatch(forDirectory: dir, mask: mask)
|
||||
} else {
|
||||
try await watcher.addWatch(path: dir, mask: mask)
|
||||
}
|
||||
|
||||
let eventTask = Task {
|
||||
var events: [InotifyEvent] = []
|
||||
|
||||
Reference in New Issue
Block a user