10
10
11
11
import crypto from 'crypto' ;
12
12
import * as path from 'path' ;
13
+ import { serialize } from 'v8' ;
13
14
14
15
jest . useRealTimers ( ) ;
15
16
@@ -153,7 +154,13 @@ jest.mock('fs', () => ({
153
154
} ) ) ;
154
155
155
156
const object = data => Object . assign ( Object . create ( null ) , data ) ;
156
- const createMap = obj => new Map ( Object . keys ( obj ) . map ( key => [ key , obj [ key ] ] ) ) ;
157
+ const createMap = obj => new Map ( Object . entries ( obj ) ) ;
158
+ const assertFileSystemEqual = ( fileSystem : FileSystem , fileData : FileData ) => {
159
+ expect ( fileSystem . getDifference ( fileData ) ) . toEqual ( {
160
+ changedFiles : new Map ( ) ,
161
+ removedFiles : new Set ( ) ,
162
+ } ) ;
163
+ } ;
157
164
158
165
// Jest toEqual does not match Map instances from different contexts
159
166
// This normalizes them for the uses cases in this test
@@ -406,11 +413,12 @@ describe('HasteMap', () => {
406
413
mocksPattern : '__mocks__' ,
407
414
} ) ;
408
415
409
- await hasteMap . build ( ) ;
416
+ const { fileSystem } = await hasteMap . build ( ) ;
410
417
411
418
expect ( cacheContent . clocks ) . toEqual ( mockClocks ) ;
412
419
413
- expect ( cacheContent . files ) . toEqual (
420
+ assertFileSystemEqual (
421
+ fileSystem ,
414
422
createMap ( {
415
423
[ path . join ( 'fruits' , 'Banana.js' ) ] : [
416
424
'Banana' ,
@@ -581,7 +589,7 @@ describe('HasteMap', () => {
581
589
582
590
await hasteMap . build ( ) ;
583
591
584
- expect ( cacheContent . files ) . toEqual (
592
+ expect (
585
593
createMap ( {
586
594
[ path . join ( 'fruits' , 'Banana.js' ) ] : [
587
595
'Banana' ,
@@ -693,11 +701,14 @@ describe('HasteMap', () => {
693
701
roots : [ ...defaultConfig . roots , path . join ( '/' , 'project' , 'video' ) ] ,
694
702
} ) ;
695
703
696
- await hasteMap . build ( ) ;
704
+ const { fileSystem } = await hasteMap . build ( ) ;
697
705
const data = cacheContent ;
698
706
699
707
expect ( data . map . get ( 'IRequireAVideo' ) ) . toBeDefined ( ) ;
700
- expect ( data . files . get ( path . join ( 'video' , 'video.mp4' ) ) ) . toBeDefined ( ) ;
708
+ expect ( fileSystem . linkStats ( path . join ( 'video' , 'video.mp4' ) ) ) . toEqual ( {
709
+ fileType : 'f' ,
710
+ modifiedTime : 32 ,
711
+ } ) ;
701
712
expect ( fs . readFileSync . mock . calls . map ( call => call [ 0 ] ) ) . not . toContain (
702
713
path . join ( 'video' , 'video.mp4' ) ,
703
714
) ;
@@ -716,15 +727,15 @@ describe('HasteMap', () => {
716
727
retainAllFiles : true ,
717
728
} ) ;
718
729
719
- await hasteMap . build ( ) ;
730
+ const { fileSystem } = await hasteMap . build ( ) ;
720
731
721
732
// Expect the node module to be part of files but make sure it wasn't
722
733
// read.
723
734
expect (
724
- cacheContent . files . get (
735
+ fileSystem . linkStats (
725
736
path . join ( 'fruits' , 'node_modules' , 'fbjs' , 'fbjs.js' ) ,
726
737
) ,
727
- ) . toEqual ( [ ' ', 32 , 42 , 0 , [ ] , null , 0 ] ) ;
738
+ ) . toEqual ( { fileType : 'f ', modifiedTime : 32 } ) ;
728
739
729
740
expect ( cacheContent . map . get ( 'fbjs' ) ) . not . toBeDefined ( ) ;
730
741
@@ -835,9 +846,10 @@ describe('HasteMap', () => {
835
846
const Blackberry = require("Blackberry");
836
847
` ;
837
848
838
- await new HasteMap ( defaultConfig ) . build ( ) ;
849
+ const { fileSystem } = await new HasteMap ( defaultConfig ) . build ( ) ;
839
850
840
- expect ( cacheContent . files ) . toEqual (
851
+ assertFileSystemEqual (
852
+ fileSystem ,
841
853
createMap ( {
842
854
[ path . join ( 'fruits' , 'Strawberry.android.js' ) ] : [
843
855
'Strawberry' ,
@@ -915,13 +927,22 @@ describe('HasteMap', () => {
915
927
// Expect no fs reads, because there have been no changes
916
928
expect ( fs . readFileSync . mock . calls . length ) . toBe ( 0 ) ;
917
929
expect ( deepNormalize ( data . clocks ) ) . toEqual ( mockClocks ) ;
918
- expect ( deepNormalize ( data . files ) ) . toEqual ( initialData . files ) ;
930
+ expect ( serialize ( data . fileSystem ) ) . toEqual (
931
+ serialize ( initialData . fileSystem ) ,
932
+ ) ;
919
933
expect ( deepNormalize ( data . map ) ) . toEqual ( initialData . map ) ;
920
934
} ) ;
921
935
922
936
it ( 'only does minimal file system access when files change' , async ( ) => {
923
937
// Run with a cold cache initially
924
- await new HasteMap ( defaultConfig ) . build ( ) ;
938
+ const { fileSystem : initialFileSystem } = await new HasteMap (
939
+ defaultConfig ,
940
+ ) . build ( ) ;
941
+
942
+ expect (
943
+ initialFileSystem . getDependencies ( path . join ( 'fruits' , 'Banana.js' ) ) ,
944
+ ) . toEqual ( [ 'Strawberry' ] ) ;
945
+
925
946
const initialData = cacheContent ;
926
947
fs . readFileSync . mockClear ( ) ;
927
948
expect ( mockCacheManager . read ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -939,7 +960,7 @@ describe('HasteMap', () => {
939
960
vegetables : 'c:fake-clock:2' ,
940
961
} ) ;
941
962
942
- await new HasteMap ( defaultConfig ) . build ( ) ;
963
+ const { fileSystem } = await new HasteMap ( defaultConfig ) . build ( ) ;
943
964
const data = cacheContent ;
944
965
945
966
expect ( mockCacheManager . read ) . toHaveBeenCalledTimes ( 2 ) ;
@@ -950,18 +971,9 @@ describe('HasteMap', () => {
950
971
951
972
expect ( deepNormalize ( data . clocks ) ) . toEqual ( mockClocks ) ;
952
973
953
- const files = new Map ( initialData . files ) ;
954
- files . set ( path . join ( 'fruits' , 'Banana.js' ) , [
955
- 'Banana' ,
956
- 32 ,
957
- 42 ,
958
- 1 ,
959
- 'Kiwi' ,
960
- null ,
961
- 0 ,
962
- ] ) ;
963
-
964
- expect ( deepNormalize ( data . files ) ) . toEqual ( files ) ;
974
+ expect (
975
+ fileSystem . getDependencies ( path . join ( 'fruits' , 'Banana.js' ) ) ,
976
+ ) . toEqual ( [ 'Kiwi' ] ) ;
965
977
966
978
const map = new Map ( initialData . map ) ;
967
979
expect ( deepNormalize ( data . map ) ) . toEqual ( map ) ;
@@ -984,16 +996,13 @@ describe('HasteMap', () => {
984
996
vegetables : 'c:fake-clock:2' ,
985
997
} ) ;
986
998
987
- await new HasteMap ( defaultConfig ) . build ( ) ;
988
- const data = cacheContent ;
999
+ const { fileSystem} = await new HasteMap ( defaultConfig ) . build ( ) ;
989
1000
990
- const files = new Map ( initialData . files ) ;
991
- files . delete ( path . join ( 'fruits' , 'Banana.js' ) ) ;
992
- expect ( deepNormalize ( data . files ) ) . toEqual ( files ) ;
1001
+ expect ( fileSystem . exists ( path . join ( 'fruits' , 'Banana.js' ) ) ) . toEqual ( false ) ;
993
1002
994
1003
const map = new Map ( initialData . map ) ;
995
1004
map . delete ( 'Banana' ) ;
996
- expect ( deepNormalize ( data . map ) ) . toEqual ( map ) ;
1005
+ expect ( deepNormalize ( cacheContent . map ) ) . toEqual ( map ) ;
997
1006
} ) ;
998
1007
999
1008
it ( 'correctly handles platform-specific file additions' , async ( ) => {
@@ -1258,11 +1267,11 @@ describe('HasteMap', () => {
1258
1267
} ;
1259
1268
} ) ;
1260
1269
1261
- await new HasteMap ( defaultConfig ) . build ( ) ;
1262
- expect ( cacheContent . files . size ) . toBe ( 5 ) ;
1270
+ const { fileSystem } = await new HasteMap ( defaultConfig ) . build ( ) ;
1271
+ expect ( fileSystem . getDifference ( new Map ( ) ) . removedFiles . size ) . toBe ( 5 ) ;
1263
1272
1264
1273
// Ensure this file is not part of the file list.
1265
- expect ( cacheContent . files . get ( invalidFilePath ) ) . toBe ( undefined ) ;
1274
+ expect ( fileSystem . exists ( invalidFilePath ) ) . toBe ( false ) ;
1266
1275
} ) ;
1267
1276
1268
1277
it ( 'distributes work across workers' , async ( ) => {
@@ -1362,11 +1371,13 @@ describe('HasteMap', () => {
1362
1371
} ) ;
1363
1372
} ) ;
1364
1373
1365
- await new HasteMap ( defaultConfig ) . build ( ) ;
1374
+ const { fileSystem} = await new HasteMap ( defaultConfig ) . build ( ) ;
1375
+
1366
1376
expect ( watchman ) . toBeCalled ( ) ;
1367
1377
expect ( node ) . toBeCalled ( ) ;
1368
1378
1369
- expect ( cacheContent . files ) . toEqual (
1379
+ assertFileSystemEqual (
1380
+ fileSystem ,
1370
1381
createMap ( {
1371
1382
[ path . join ( 'fruits' , 'Banana.js' ) ] : [
1372
1383
'Banana' ,
@@ -1399,12 +1410,13 @@ describe('HasteMap', () => {
1399
1410
} ) ;
1400
1411
} ) ;
1401
1412
1402
- await new HasteMap ( defaultConfig ) . build ( ) ;
1413
+ const { fileSystem } = await new HasteMap ( defaultConfig ) . build ( ) ;
1403
1414
1404
1415
expect ( watchman ) . toBeCalled ( ) ;
1405
1416
expect ( node ) . toBeCalled ( ) ;
1406
1417
1407
- expect ( cacheContent . files ) . toEqual (
1418
+ assertFileSystemEqual (
1419
+ fileSystem ,
1408
1420
createMap ( {
1409
1421
[ path . join ( 'fruits' , 'Banana.js' ) ] : [
1410
1422
'Banana' ,
0 commit comments