@@ -9,8 +9,8 @@ io.stdout:setvbuf("no")
99io.stderr :setvbuf (" no" )
1010
1111-- Monkeypatch modules before loading library.
12- local lfs = not love and require " lfs"
13- local fakeModTime = 1
12+ local lfs = not love and require " lfs"
13+ local fakeModTimes = {}
1414
1515local function pack (...)
1616 return {n = select (" #" , ... ), ... }
@@ -24,26 +24,26 @@ if love then
2424 local info , err = _getInfo (path , ... )
2525 if not info then return nil , err end
2626
27- info .modtime = fakeModTime
27+ info .modtime = fakeModTimes [ path ] or 1
2828 return info
2929 end
3030
3131 else
3232 local _getLastModified = love .filesystem .getLastModified
3333 local _exists = love .filesystem .exists
3434
35- function love .filesystem .getLastModified (path ) return fakeModTime end
36- function love .filesystem .exists (path ) return true end
35+ function love .filesystem .getLastModified (path ) return fakeModTimes [ path ] or 1 end
36+ function love .filesystem .exists (path ) return true end
3737 end
3838
3939else
4040 local _attributes = lfs .attributes
4141
4242 function lfs .attributes (path , requestNameOrResultTable )
43- if requestNameOrResultTable == " modification" then return fakeModTime end
43+ if requestNameOrResultTable == " modification" then return fakeModTimes [ path ] or 1 end
4444
4545 local values = pack (_attributes (path , requestNameOrResultTable ))
46- if type (values [1 ]) == " table" then values [1 ].modification = fakeModTime end
46+ if type (values [1 ]) == " table" then values [1 ].modification = fakeModTimes [ path ] or 1 end
4747
4848 return unpack (values , 1 , values .n )
4949 end
8787 local hotLoader = newHotLoader ()
8888
8989 -- Load file (with a custom loader).
90- local path = (love and " " or thisDir ) .. " test.txt"
91- local loads = 0
90+ local path1 = (love and " " or thisDir ) .. " test1.txt"
91+ local loads = 0
92+ fakeModTimes [path1 ] = 1
9293
93- local text = hotLoader .load (path , function (path )
94+ local text = hotLoader .load (path1 , function (path )
9495 loads = loads + 1
9596 return readFile (path )
9697 end )
97- assert (hotLoader .hasLoaded (path ))
98+ assert (hotLoader .hasLoaded (path1 ))
9899 assert (loads == 1 ) -- The custom loader should've been used.
99- assert (text == " foobar " )
100- assert (hotLoader .load (path ) == " foobar " )
100+ assert (text == " foobar1 " )
101+ assert (hotLoader .load (path1 ) == " foobar1 " )
101102 assert (loads == 1 ) -- The file shouldn't have loaded again.
102103
103104 -- Reload file.
104- hotLoader .unload (path )
105- assert (not hotLoader .hasLoaded (path ))
106- hotLoader .unload (path ) -- Should do nothing.
107- hotLoader .load (path )
108- assert (hotLoader .hasLoaded (path ))
105+ hotLoader .unload (path1 )
106+ assert (not hotLoader .hasLoaded (path1 ))
107+ hotLoader .unload (path1 ) -- Should do nothing.
108+ hotLoader .load (path1 )
109+ assert (hotLoader .hasLoaded (path1 ))
109110 assert (loads == 2 )
110111
111112 -- Run updates for a couple of seconds.
112113 for i = 1 , 10 do hotLoader .update (.2 ) end
113114 assert (loads == 2 ) -- Nothing should've happened.
114115
115116 -- Update the file.
116- fakeModTime = fakeModTime + 1
117+ fakeModTimes [ path1 ] = fakeModTimes [ path1 ] + 1
117118
118119 -- Run updates for a couple of seconds.
120+ print (" Should reload." )
119121 for i = 1 , 10 do hotLoader .update (.2 ) end
120122 assert (loads == 3 ) -- The file should've reloaded.
123+
124+ -- Monitor the file (which replaces the custom loader).
125+ local path2 = (love and " " or thisDir ) .. " test2.txt"
126+ local monitors = 0
127+ fakeModTimes [path2 ] = 1
128+ hotLoader .monitor (path2 , function (path , ...)
129+ monitors = monitors + 1
130+ assert (select (" #" , ... ) == 0 )
131+ end )
132+ assert (monitors == 0 )
133+
134+ -- Run updates for a couple of seconds.
135+ for i = 1 , 10 do hotLoader .update (.2 ) end
136+ assert (monitors == 0 )
137+
138+ -- Update the file.
139+ fakeModTimes [path2 ] = fakeModTimes [path2 ] + 1
140+
141+ -- Run updates for a couple of seconds.
142+ print (" Should reload." )
143+ for i = 1 , 10 do hotLoader .update (.2 ) end
144+ assert (monitors == 1 )
145+ assert (loads == 3 ) -- Make sure the previous file didn't reload.
146+
147+ -- Monitor with data.
148+ hotLoader .monitor (path2 , " testdata" , function (path , data )
149+ monitors = monitors + 1
150+ assert (data == " testdata" )
151+ end )
152+ assert (monitors == 1 )
153+ fakeModTimes [path2 ] = fakeModTimes [path2 ] + 1
154+ print (" Should reload." )
155+ for i = 1 , 10 do hotLoader .update (.2 ) end
156+ assert (monitors == 2 )
121157end
122158
123159--
132168 end
133169
134170 -- Require file.
135- local moduleName = " test"
136- _G .requires = 0
171+ local moduleName = " test"
172+ local modulePath = (love and " " or thisDir ) .. " test.lua"
173+ fakeModTimes [modulePath ] = 1
174+ _G .requires = 0
137175 assert (not hotLoader .hasRequired (moduleName ))
138176 assert (hotLoader .require (moduleName ) == " foobar" )
139177 assert (hotLoader .hasRequired (moduleName ))
154192 assert (requires == 2 ) -- Nothing should've happened.
155193
156194 -- Update the file.
157- fakeModTime = fakeModTime + 1
195+ fakeModTimes [ modulePath ] = fakeModTimes [ modulePath ] + 1
158196
159197 -- Run updates for a couple of seconds.
198+ print (" Should reload." )
160199 for i = 1 , 10 do hotLoader .update (.2 ) end
161200 assert (requires == 3 ) -- The file should've reloaded.
162201
0 commit comments