33
44const path = require ( "path" ) ;
55const fs = require ( "fs" ) ;
6+ const rimraf = require ( "rimraf" ) ;
67
78const webpack = require ( "../" ) ;
89const WebpackOptionsDefaulter = require ( "../lib/WebpackOptionsDefaulter" ) ;
10+ let fixtureCount = 0 ;
911
1012describe ( "Compiler (caching)" , ( ) => {
1113 jest . setTimeout ( 15000 ) ;
@@ -89,51 +91,34 @@ describe("Compiler (caching)", () => {
8991 "fixtures" ,
9092 "temp-cache-fixture"
9193 ) ;
92- const aFilepath = path . join ( tempFixturePath , "a.js" ) ;
93- const cFilepath = path . join ( tempFixturePath , "c.js" ) ;
9494
9595 function cleanup ( ) {
96- function ignoreENOENT ( fn ) {
97- try {
98- return fn ( ) ;
99- } catch ( e ) {
100- if ( e . code !== "ENOENT" ) {
101- throw e ;
102- }
103- }
104- }
105- ignoreENOENT ( ( ) => fs . unlinkSync ( aFilepath ) ) ;
106- ignoreENOENT ( ( ) => fs . unlinkSync ( cFilepath ) ) ;
107- ignoreENOENT ( ( ) => fs . rmdirSync ( tempFixturePath ) ) ;
96+ rimraf . sync ( `${ tempFixturePath } -*` ) ;
10897 }
98+
10999 beforeAll ( cleanup ) ;
110100 afterAll ( cleanup ) ;
111101
112102 function createTempFixture ( ) {
103+ const fixturePath = `${ tempFixturePath } -${ fixtureCount } ` ;
104+ const aFilepath = path . join ( fixturePath , "a.js" ) ;
105+ const cFilepath = path . join ( fixturePath , "c.js" ) ;
106+
113107 // Remove previous copy if present
114- try {
115- if ( fs . statSync ( tempFixturePath ) ) {
116- fs . unlinkSync ( aFilepath ) ;
117- fs . unlinkSync ( cFilepath ) ;
118- fs . rmdirSync ( tempFixturePath ) ;
119- }
120- } catch ( e ) {
121- if ( e . code !== "ENOENT" ) {
122- throw e ;
123- }
124- }
108+ rimraf . sync ( fixturePath ) ;
125109
126110 // Copy over file since we"ll be modifying some of them
127- fs . mkdirSync ( tempFixturePath ) ;
111+ fs . mkdirSync ( fixturePath ) ;
128112 fs
129113 . createReadStream ( path . join ( __dirname , "fixtures" , "a.js" ) )
130114 . pipe ( fs . createWriteStream ( aFilepath ) ) ;
131115 fs
132116 . createReadStream ( path . join ( __dirname , "fixtures" , "c.js" ) )
133117 . pipe ( fs . createWriteStream ( cFilepath ) ) ;
134118
119+ fixtureCount ++ ;
135120 return {
136- rootPath : tempFixturePath ,
121+ rootPath : fixturePath ,
137122 aFilepath : aFilepath ,
138123 cFilepath : cFilepath
139124 } ;
@@ -143,171 +128,155 @@ describe("Compiler (caching)", () => {
143128 const options = { } ;
144129 const tempFixture = createTempFixture ( ) ;
145130
146- const helper = compile (
147- "./temp-cache-fixture/c" ,
148- options ,
149- ( stats , files ) => {
150- // Not cached the first time
131+ const helper = compile ( tempFixture . cFilepath , options , ( stats , files ) => {
132+ // Not cached the first time
133+ expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
134+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
135+
136+ helper . runAgain ( ( stats , files , iteration ) => {
137+ // Cached the second run
151138 expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
152- expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
139+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( false ) ;
153140
154- helper . runAgain ( ( stats , files , iteration ) => {
155- // Cached the second run
156- expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
157- expect ( stats . assets [ 0 ] . emitted ) . toBe ( false ) ;
141+ const aContent = fs
142+ . readFileSync ( tempFixture . aFilepath )
143+ . toString ( )
144+ . replace ( "This is a" , "This is a MODIFIED" ) ;
158145
159- const aContent = fs
160- . readFileSync ( tempFixture . aFilepath )
161- . toString ( )
162- . replace ( "This is a" , "This is a MODIFIED" ) ;
163-
164- setTimeout ( ( ) => {
165- fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
146+ setTimeout ( ( ) => {
147+ fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
166148
167- helper . runAgain ( ( stats , files , iteration ) => {
168- // Cached the third run
169- expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
170- expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
149+ helper . runAgain ( ( stats , files , iteration ) => {
150+ // Cached the third run
151+ expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
152+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
171153
172- done ( ) ;
173- } ) ;
174- } , 1100 ) ;
175- } ) ;
176- }
177- ) ;
154+ done ( ) ;
155+ } ) ;
156+ } , 1100 ) ;
157+ } ) ;
158+ } ) ;
178159 } ) ;
179160
180161 it ( "should cache single file (even with no timeout) " , done => {
181162 const options = { } ;
182163 const tempFixture = createTempFixture ( ) ;
183164
184- const helper = compile (
185- "./temp-cache-fixture/c" ,
186- options ,
187- ( stats , files ) => {
188- // Not cached the first time
189- expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
190- expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
165+ const helper = compile ( tempFixture . cFilepath , options , ( stats , files ) => {
166+ // Not cached the first time
167+ expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
168+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
191169
192- helper . runAgain ( ( stats , files , iteration ) => {
193- // Cached the second run
194- expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
195- expect ( stats . assets [ 0 ] . emitted ) . toBe ( false ) ;
170+ helper . runAgain ( ( stats , files , iteration ) => {
171+ // Cached the second run
172+ expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
173+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( false ) ;
196174
197- expect ( files [ "/bundle.js" ] ) . toMatch ( "This is a" ) ;
175+ expect ( files [ "/bundle.js" ] ) . toMatch ( "This is a" ) ;
198176
199- const aContent = fs
200- . readFileSync ( tempFixture . aFilepath )
201- . toString ( )
202- . replace ( "This is a" , "This is a MODIFIED" ) ;
177+ const aContent = fs
178+ . readFileSync ( tempFixture . aFilepath )
179+ . toString ( )
180+ . replace ( "This is a" , "This is a MODIFIED" ) ;
203181
204- fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
182+ fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
205183
206- helper . runAgain ( ( stats , files , iteration ) => {
207- // Cached the third run
208- expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
209- expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
184+ helper . runAgain ( ( stats , files , iteration ) => {
185+ // Cached the third run
186+ expect ( stats . assets [ 0 ] . name ) . toBe ( "bundle.js" ) ;
187+ expect ( stats . assets [ 0 ] . emitted ) . toBe ( true ) ;
210188
211- expect ( files [ "/bundle.js" ] ) . toMatch ( "This is a MODIFIED" ) ;
189+ expect ( files [ "/bundle.js" ] ) . toMatch ( "This is a MODIFIED" ) ;
212190
213- done ( ) ;
214- } ) ;
191+ done ( ) ;
215192 } ) ;
216- }
217- ) ;
193+ } ) ;
194+ } ) ;
218195 } ) ;
219196
220197 it ( "should only build when modified (with manual 2s wait)" , done => {
221198 const options = { } ;
222199 const tempFixture = createTempFixture ( ) ;
223200
224- const helper = compile (
225- "./temp-cache-fixture/c" ,
226- options ,
227- ( stats , files ) => {
228- // Built the first time
229- expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
230- expect ( stats . modules [ 0 ] . built ) . toBe ( true ) ;
201+ const helper = compile ( tempFixture . cFilepath , options , ( stats , files ) => {
202+ // Built the first time
203+ expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
204+ expect ( stats . modules [ 0 ] . built ) . toBe ( true ) ;
231205
232- expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
233- expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
206+ expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
207+ expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
234208
235- setTimeout ( ( ) => {
236- helper . runAgain ( ( stats , files , iteration ) => {
237- // Not built when cached the second run
238- expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
239- // expect(stats.modules[0].built).toBe(false);
209+ setTimeout ( ( ) => {
210+ helper . runAgain ( ( stats , files , iteration ) => {
211+ // Not built when cached the second run
212+ expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
213+ // expect(stats.modules[0].built).toBe(false);
240214
241- expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
242- // expect(stats.modules[1].built).toBe(false);
215+ expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
216+ // expect(stats.modules[1].built).toBe(false);
243217
244- const aContent = fs
245- . readFileSync ( tempFixture . aFilepath )
246- . toString ( )
247- . replace ( "This is a" , "This is a MODIFIED" ) ;
218+ const aContent = fs
219+ . readFileSync ( tempFixture . aFilepath )
220+ . toString ( )
221+ . replace ( "This is a" , "This is a MODIFIED" ) ;
248222
249- setTimeout ( ( ) => {
250- fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
223+ setTimeout ( ( ) => {
224+ fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
251225
252- helper . runAgain ( ( stats , files , iteration ) => {
253- // And only a.js built after it was modified
254- expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
255- expect ( stats . modules [ 0 ] . built ) . toBe ( false ) ;
226+ helper . runAgain ( ( stats , files , iteration ) => {
227+ // And only a.js built after it was modified
228+ expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
229+ expect ( stats . modules [ 0 ] . built ) . toBe ( false ) ;
256230
257- expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
258- expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
231+ expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
232+ expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
259233
260- done ( ) ;
261- } ) ;
262- } , 2100 ) ;
263- } ) ;
264- } , 4100 ) ;
265- }
266- ) ;
234+ done ( ) ;
235+ } ) ;
236+ } , 2100 ) ;
237+ } ) ;
238+ } , 4100 ) ;
239+ } ) ;
267240 } ) ;
268241
269242 it ( "should build when modified (even with no timeout)" , done => {
270243 const options = { } ;
271244 const tempFixture = createTempFixture ( ) ;
272245
273- const helper = compile (
274- "./temp-cache-fixture/c" ,
275- options ,
276- ( stats , files ) => {
277- // Built the first time
246+ const helper = compile ( tempFixture . cFilepath , options , ( stats , files ) => {
247+ // Built the first time
248+ expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
249+ expect ( stats . modules [ 0 ] . built ) . toBe ( true ) ;
250+
251+ expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
252+ expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
253+
254+ helper . runAgain ( ( stats , files , iteration ) => {
255+ // Not built when cached the second run
278256 expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
279- expect ( stats . modules [ 0 ] . built ) . toBe ( true ) ;
257+ // expect(stats.modules[0].built).toBe(false );
280258
281259 expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
282- expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
260+ // expect(stats.modules[1].built).toBe(false);
261+
262+ const aContent = fs
263+ . readFileSync ( tempFixture . aFilepath )
264+ . toString ( )
265+ . replace ( "This is a" , "This is a MODIFIED" ) ;
266+
267+ fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
283268
284269 helper . runAgain ( ( stats , files , iteration ) => {
285- // Not built when cached the second run
270+ // And only a.js built after it was modified
286271 expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
287272 // expect(stats.modules[0].built).toBe(false);
288273
289274 expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
290- // expect(stats.modules[1].built).toBe(false);
291-
292- const aContent = fs
293- . readFileSync ( tempFixture . aFilepath )
294- . toString ( )
295- . replace ( "This is a" , "This is a MODIFIED" ) ;
296-
297- fs . writeFileSync ( tempFixture . aFilepath , aContent ) ;
298-
299- helper . runAgain ( ( stats , files , iteration ) => {
300- // And only a.js built after it was modified
301- expect ( stats . modules [ 0 ] . name ) . toMatch ( "c.js" ) ;
302- // expect(stats.modules[0].built).toBe(false);
275+ expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
303276
304- expect ( stats . modules [ 1 ] . name ) . toMatch ( "a.js" ) ;
305- expect ( stats . modules [ 1 ] . built ) . toBe ( true ) ;
306-
307- done ( ) ;
308- } ) ;
277+ done ( ) ;
309278 } ) ;
310- }
311- ) ;
279+ } ) ;
280+ } ) ;
312281 } ) ;
313282} ) ;
0 commit comments