File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -106,8 +106,14 @@ export class NodeFSTreeContainer extends TreeContainer {
106
106
}
107
107
108
108
public readFile ( fsPath : SourcePath ) : Promise < Buffer > {
109
- // significant enough performance increase using sync instead of fs.promise version
110
- return Promise . resolve ( readFileSync ( fsPath ) ) ;
109
+ if ( this . fileContentMap . has ( fsPath ) ) {
110
+ return Promise . resolve ( this . fileContentMap . get ( fsPath ) ! ) ;
111
+ } else {
112
+ // significant enough performance increase using sync instead of fs.promise version
113
+ const content = readFileSync ( fsPath ) ;
114
+ this . fileContentMap . set ( fsPath , content ) ;
115
+ return Promise . resolve ( content ) ;
116
+ }
111
117
}
112
118
113
119
public readFileSync ( fsPath : SourcePath ) : Buffer {
Original file line number Diff line number Diff line change @@ -119,12 +119,23 @@ describe('Tree Containers', () => {
119
119
it ( 'should use expected Node API for readFileSync' , ( ) => {
120
120
const readFileStub = env . stub ( fs , 'readFileSync' ) ;
121
121
// @ts -ignore wants Dirents but string[] works as well
122
- readFileStub . withArgs ( path ) . returns ( Buffer . from ( 'test' ) ) ;
123
- const data = tree . readFileSync ( path ) ;
122
+ readFileStub . withArgs ( 'myNewPath' ) . returns ( Buffer . from ( 'test' ) ) ;
123
+ const data = tree . readFileSync ( 'myNewPath' ) ;
124
124
expect ( data . toString ( ) ) . to . deep . equal ( 'test' ) ;
125
125
expect ( readFileStub . calledOnce ) . to . be . true ;
126
126
} ) ;
127
127
128
+ it ( 'should use cached value for readFileSync' , ( ) => {
129
+ const readFileStub = env . stub ( fs , 'readFileSync' ) ;
130
+ // @ts -ignore wants Dirents but string[] works as well
131
+ readFileStub . withArgs ( 'myNewPath' ) . returns ( Buffer . from ( 'test' ) ) ;
132
+ const data = tree . readFileSync ( 'myNewPath' ) ;
133
+ // returns same value
134
+ expect ( data . toString ( ) ) . to . deep . equal ( 'test' ) ;
135
+ // didn't re-read the file
136
+ expect ( readFileStub . called ) . to . be . false ;
137
+ } ) ;
138
+
128
139
it ( 'should use expected Node API for stream' , ( ) => {
129
140
const readable = new Readable ( ) ;
130
141
// @ts -ignore wants ReadStream but Readable works for testing
You can’t perform that action at this time.
0 commit comments