@@ -150,43 +150,57 @@ mod tests {
150150 use rspack_fs:: { MemoryFileSystem , WritableFileSystem } ;
151151 use rspack_paths:: ArcPath ;
152152
153- use super :: { HashHelper , PackageHelper } ;
153+ use super :: {
154+ super :: super :: super :: snapshot:: PathMatcher , HashHelper , PackageHelper , SnapshotOptions ,
155+ } ;
156+
157+ fn new_helper ( fs : Arc < MemoryFileSystem > ) -> HashHelper {
158+ HashHelper :: new (
159+ fs. clone ( ) ,
160+ Arc :: new ( SnapshotOptions :: new (
161+ vec ! [ PathMatcher :: String ( "immutable" . into( ) ) ] ,
162+ vec ! [ ] ,
163+ vec ! [ PathMatcher :: String ( "node_modules" . into( ) ) ] ,
164+ ) ) ,
165+ Arc :: new ( PackageHelper :: new ( fs) ) ,
166+ )
167+ }
154168
155169 #[ tokio:: test]
156170 async fn file_hash ( ) {
157171 let fs = Arc :: new ( MemoryFileSystem :: default ( ) ) ;
158172 fs. create_dir_all ( "/" . into ( ) ) . await . unwrap ( ) ;
159173 fs. write ( "/hash.js" . into ( ) , "abc" . as_bytes ( ) ) . await . unwrap ( ) ;
160174
161- let helper = HashHelper :: new (
162- fs. clone ( ) ,
163- Default :: default ( ) ,
164- Arc :: new ( PackageHelper :: new ( fs. clone ( ) ) ) ,
165- ) ;
175+ let helper = new_helper ( fs. clone ( ) ) ;
166176 assert ! (
167177 helper
168178 . file_hash( & ArcPath :: from( "/not_exist.js" ) )
169179 . await
170180 . is_none( )
171181 ) ;
182+ // check directory
172183 let hash0 = helper. file_hash ( & ArcPath :: from ( "/" ) ) . await . unwrap ( ) ;
173184 assert_eq ! ( hash0. hash, 0 ) ;
174185
175186 let hash1 = helper. file_hash ( & ArcPath :: from ( "/hash.js" ) ) . await . unwrap ( ) ;
176187
177- helper. file_cache . clear ( ) ;
178188 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
189+ // do nothing
190+ let helper = new_helper ( fs. clone ( ) ) ;
179191 let hash2 = helper. file_hash ( & ArcPath :: from ( "/hash.js" ) ) . await . unwrap ( ) ;
180192 assert_eq ! ( hash1. hash, hash2. hash) ;
181193 assert_eq ! ( hash1. mtime, hash2. mtime) ;
182194
183- helper. file_cache . clear ( ) ;
195+ // same content
196+ let helper = new_helper ( fs. clone ( ) ) ;
184197 fs. write ( "/hash.js" . into ( ) , "abc" . as_bytes ( ) ) . await . unwrap ( ) ;
185198 let hash3 = helper. file_hash ( & ArcPath :: from ( "/hash.js" ) ) . await . unwrap ( ) ;
186199 assert_eq ! ( hash1. hash, hash3. hash) ;
187200 assert ! ( hash1. mtime < hash3. mtime) ;
188201
189- helper. file_cache . clear ( ) ;
202+ // diff content
203+ let helper = new_helper ( fs. clone ( ) ) ;
190204 fs. write ( "/hash.js" . into ( ) , "abcd" . as_bytes ( ) )
191205 . await
192206 . unwrap ( ) ;
@@ -199,39 +213,106 @@ mod tests {
199213 async fn dir_hash ( ) {
200214 let fs = Arc :: new ( MemoryFileSystem :: default ( ) ) ;
201215 fs. create_dir_all ( "/a" . into ( ) ) . await . unwrap ( ) ;
216+ fs. create_dir_all ( "/node_modules/lib" . into ( ) ) . await . unwrap ( ) ;
202217 fs. write ( "/a/a1.js" . into ( ) , "a1" . as_bytes ( ) ) . await . unwrap ( ) ;
203218 fs. write ( "/a/a2.js" . into ( ) , "a2" . as_bytes ( ) ) . await . unwrap ( ) ;
204219 fs. write ( "/b.js" . into ( ) , "b" . as_bytes ( ) ) . await . unwrap ( ) ;
220+ fs. write ( "/immutable.js" . into ( ) , "immut" . as_bytes ( ) )
221+ . await
222+ . unwrap ( ) ;
223+ fs. write (
224+ "/node_modules/lib/index.js" . into ( ) ,
225+ "const a = 1" . as_bytes ( ) ,
226+ )
227+ . await
228+ . unwrap ( ) ;
229+ fs. write (
230+ "/node_modules/lib/package.json" . into ( ) ,
231+ r#"{"version": "0.0.1"}"# . as_bytes ( ) ,
232+ )
233+ . await
234+ . unwrap ( ) ;
205235
206- let helper = HashHelper :: new (
207- fs. clone ( ) ,
208- Default :: default ( ) ,
209- Arc :: new ( PackageHelper :: new ( fs. clone ( ) ) ) ,
210- ) ;
211-
236+ let helper = new_helper ( fs. clone ( ) ) ;
212237 let hash1 = helper. dir_hash ( & ArcPath :: from ( "/" ) ) . await . unwrap ( ) ;
238+ assert_eq ! ( hash1. mtime, 0 ) ;
213239
214- helper. file_cache . clear ( ) ;
215- helper. dir_cache . clear ( ) ;
216240 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
241+
242+ // do nothing
243+ let helper = new_helper ( fs. clone ( ) ) ;
217244 let hash2 = helper. dir_hash ( & ArcPath :: from ( "/" ) ) . await . unwrap ( ) ;
218245 assert_eq ! ( hash1. hash, hash2. hash) ;
219- assert_eq ! ( hash1. mtime, 0 ) ;
220246 assert_eq ! ( hash2. mtime, 0 ) ;
221247
222- helper. file_cache . clear ( ) ;
223- helper. dir_cache . clear ( ) ;
224248 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
249+
250+ // do something will not update hash
251+ let helper = new_helper ( fs. clone ( ) ) ;
252+ // write same content
225253 fs. write ( "/a/a2.js" . into ( ) , "a2" . as_bytes ( ) ) . await . unwrap ( ) ;
254+ // edit immutable file
255+ fs. write ( "/immutable.js" . into ( ) , "next" . as_bytes ( ) )
256+ . await
257+ . unwrap ( ) ;
258+ // edit node_modules file
259+ fs. write (
260+ "/node_modules/lib/index.js" . into ( ) ,
261+ "const a = 2" . as_bytes ( ) ,
262+ )
263+ . await
264+ . unwrap ( ) ;
265+ // update package.json
266+ fs. write (
267+ "/node_modules/lib/package.json" . into ( ) ,
268+ r#"{"version": "0.0.2"}"# . as_bytes ( ) ,
269+ )
270+ . await
271+ . unwrap ( ) ;
226272 let hash3 = helper. dir_hash ( & ArcPath :: from ( "/" ) ) . await . unwrap ( ) ;
227- assert_eq ! ( hash1 . hash, hash3. hash) ;
273+ assert_eq ! ( hash2 . hash, hash3. hash) ;
228274 assert_eq ! ( hash3. mtime, 0 ) ;
229275
230- helper . file_cache . clear ( ) ;
231- helper. dir_cache . clear ( ) ;
276+ // update file content
277+ let helper = new_helper ( fs . clone ( ) ) ;
232278 fs. write ( "/a/a2.js" . into ( ) , "a2a" . as_bytes ( ) ) . await . unwrap ( ) ;
233279 let hash4 = helper. dir_hash ( & ArcPath :: from ( "/" ) ) . await . unwrap ( ) ;
234- assert_ne ! ( hash1 . hash, hash4. hash) ;
280+ assert_ne ! ( hash3 . hash, hash4. hash) ;
235281 assert_eq ! ( hash4. mtime, 0 ) ;
282+
283+ // node_modules lib test
284+ let helper = new_helper ( fs. clone ( ) ) ;
285+ let hash1 = helper
286+ . dir_hash ( & ArcPath :: from ( "/node_modules/lib/" ) )
287+ . await
288+ . unwrap ( ) ;
289+
290+ // update lib content
291+ let helper = new_helper ( fs. clone ( ) ) ;
292+ fs. write (
293+ "/node_modules/lib/index.js" . into ( ) ,
294+ "const a = 3" . as_bytes ( ) ,
295+ )
296+ . await
297+ . unwrap ( ) ;
298+ let hash2 = helper
299+ . dir_hash ( & ArcPath :: from ( "/node_modules/lib/" ) )
300+ . await
301+ . unwrap ( ) ;
302+ assert_eq ! ( hash1. hash, hash2. hash) ;
303+
304+ // update package.json
305+ let helper = new_helper ( fs. clone ( ) ) ;
306+ fs. write (
307+ "/node_modules/lib/package.json" . into ( ) ,
308+ r#"{"version": "0.0.3"}"# . as_bytes ( ) ,
309+ )
310+ . await
311+ . unwrap ( ) ;
312+ let hash2 = helper
313+ . dir_hash ( & ArcPath :: from ( "/node_modules/lib/" ) )
314+ . await
315+ . unwrap ( ) ;
316+ assert_ne ! ( hash1. hash, hash2. hash) ;
236317 }
237318}
0 commit comments