From 29ba37ce3cadb85448773988d961fec4bc44c14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 23 Nov 2022 12:27:58 +0000 Subject: add test for filewatch --- test/other/filewatch.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ test/other/init.lua | 12 +----------- 2 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 test/other/filewatch.lua (limited to 'test/other') diff --git a/test/other/filewatch.lua b/test/other/filewatch.lua new file mode 100644 index 00000000..59c5812d --- /dev/null +++ b/test/other/filewatch.lua @@ -0,0 +1,43 @@ +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 _ = fw.watch(path:string()) +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 = io.open((path / 'test.txt'):string(), 'a') +assert(f) +f:write('xxx') +f:flush() + +thread.sleep(1) +events = {} +fw.update() +assert(#events == 1) +assert(events[1][1] == 'change') diff --git a/test/other/init.lua b/test/other/init.lua index 069a2454..dcb57404 100644 --- a/test/other/init.lua +++ b/test/other/init.lua @@ -1,11 +1 @@ -local fs = require 'bee.filesystem' -local platform = require 'bee.platform' -local path = fs.path '/a/b/c/d/e/../../../..' -local absolute = fs.absolute(path) -if platform.OS == 'Windows' then - assert(absolute:string():sub(-2) == '/a', absolute:string()) -elseif platform.OS == 'Linux' then - assert(absolute:string():sub(-3) == '/a/', absolute:string()) -elseif platform.OS == 'macOS' then - -- 不支持 -end +require 'other.filewatch' -- cgit v1.2.3