summaryrefslogtreecommitdiff
path: root/test/other/filewatch.lua
blob: f5d5a03d9031ad20b90fa175756594e4dd846eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local thread = require 'bee.thread'
local fw     = require 'filewatch'
local fs     = require 'bee.filesystem'
local fsu    = require 'fs-utility'

local path = fs.path(LOGPATH) / 'fw'

fs.create_directories(path)

os.remove((path / 'test.txt'):string())

local _ <close> = fw.watch(path:string(), true)
fsu.saveFile(path / 'test.txt', 'test')

local events
fw.event(function (ev, filename)
    events[#events+1] = {ev, filename}
end)

thread.sleep(1)
events = {}
fw.update()
assert(#events == 1)
assert(events[1][1] == 'create')

fsu.saveFile(path / 'test.txt', 'modify')

thread.sleep(1)
events = {}
fw.update()
assert(#events == 1)
assert(events[1][1] == 'change')

local f <close> = io.open((path / 'test.txt'):string(), 'w')
assert(f)
f:write('xxx')
f:flush()
f:close()

thread.sleep(1)
events = {}
fw.update()
assert(#events == 1)
assert(events[1][1] == 'change')