11import test from "ava" ;
2+ import fs from "fs" ;
23import path from "path" ;
34import rimraf from "rimraf" ;
45import webpack from "webpack" ;
@@ -11,7 +12,6 @@ const defaultCacheDir = path.join(
1112const cacheDir = path . join ( __dirname , "output/cache/cachefiles" ) ;
1213const outputDir = path . join ( __dirname , "output/cache" ) ;
1314const babelLoader = path . join ( __dirname , "../lib" ) ;
14- const { open } = require ( "lmdb" ) ;
1515
1616const globalConfig = {
1717 mode : "development" ,
@@ -48,7 +48,7 @@ test.beforeEach.cb(t => rimraf(defaultCacheDir, t.end));
4848test . afterEach . cb ( t => rimraf ( t . context . directory , t . end ) ) ;
4949test . afterEach . cb ( t => rimraf ( t . context . cacheDirectory , t . end ) ) ;
5050
51- test . cb ( "should build a cache database in the cache directory" , t => {
51+ test . cb ( "should output files to cache directory" , t => {
5252 const config = Object . assign ( { } , globalConfig , {
5353 output : {
5454 path : t . context . directory ,
@@ -73,14 +73,16 @@ test.cb("should build a cache database in the cache directory", t => {
7373 t . deepEqual ( stats . compilation . errors , [ ] ) ;
7474 t . deepEqual ( stats . compilation . warnings , [ ] ) ;
7575
76- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
77- t . true ( cacheDB . getStats ( ) . entryCount > 0 ) ;
78- t . end ( ) ;
76+ fs . readdir ( t . context . cacheDirectory , ( err , files ) => {
77+ t . is ( err , null ) ;
78+ t . true ( files . length > 0 ) ;
79+ t . end ( ) ;
80+ } ) ;
7981 } ) ;
8082} ) ;
8183
82- test . serial . cb . only (
83- "should add entries to cache db at standard cache dir by default" ,
84+ test . serial . cb (
85+ "should output json.gz files to standard cache dir by default" ,
8486 t => {
8587 const config = Object . assign ( { } , globalConfig , {
8688 output : {
@@ -106,15 +108,56 @@ test.serial.cb.only(
106108 t . deepEqual ( stats . compilation . errors , [ ] ) ;
107109 t . deepEqual ( stats . compilation . warnings , [ ] ) ;
108110
109- const cacheDB = open ( defaultCacheDir , { readOnly : true } ) ;
110- t . true ( cacheDB . getStats ( ) . entryCount > 0 ) ;
111- t . end ( ) ;
111+ fs . readdir ( defaultCacheDir , ( err , files ) => {
112+ files = files . filter ( file => / \b [ 0 - 9 a - f ] { 5 , 40 } \. j s o n \. g z \b / . test ( file ) ) ;
113+
114+ t . is ( err , null ) ;
115+ t . true ( files . length > 0 ) ;
116+ t . end ( ) ;
117+ } ) ;
112118 } ) ;
113119 } ,
114120) ;
115121
116122test . serial . cb (
117- "should add entries to cache db at standard cache dir if set to true in query" ,
123+ "should output non-compressed files to standard cache dir when cacheCompression is set to false" ,
124+ t => {
125+ const config = Object . assign ( { } , globalConfig , {
126+ output : {
127+ path : t . context . directory ,
128+ } ,
129+ module : {
130+ rules : [
131+ {
132+ test : / \. j s x ? / ,
133+ loader : babelLoader ,
134+ exclude : / n o d e _ m o d u l e s / ,
135+ options : {
136+ cacheDirectory : true ,
137+ cacheCompression : false ,
138+ presets : [ "@babel/preset-env" ] ,
139+ } ,
140+ } ,
141+ ] ,
142+ } ,
143+ } ) ;
144+
145+ webpack ( config , err => {
146+ t . is ( err , null ) ;
147+
148+ fs . readdir ( defaultCacheDir , ( err , files ) => {
149+ files = files . filter ( file => / \b [ 0 - 9 a - f ] { 5 , 40 } \b / . test ( file ) ) ;
150+
151+ t . is ( err , null ) ;
152+ t . true ( files . length > 0 ) ;
153+ t . end ( ) ;
154+ } ) ;
155+ } ) ;
156+ } ,
157+ ) ;
158+
159+ test . serial . cb (
160+ "should output files to standard cache dir if set to true in query" ,
118161 t => {
119162 const config = Object . assign ( { } , globalConfig , {
120163 output : {
@@ -136,9 +179,14 @@ test.serial.cb(
136179 t . deepEqual ( stats . compilation . errors , [ ] ) ;
137180 t . deepEqual ( stats . compilation . warnings , [ ] ) ;
138181
139- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
140- t . true ( cacheDB . getStats ( ) . entryCount > 0 ) ;
141- t . end ( ) ;
182+ fs . readdir ( defaultCacheDir , ( err , files ) => {
183+ files = files . filter ( file => / \b [ 0 - 9 a - f ] { 5 , 40 } \. j s o n \. g z \b / . test ( file ) ) ;
184+
185+ t . is ( err , null ) ;
186+
187+ t . true ( files . length > 0 ) ;
188+ t . end ( ) ;
189+ } ) ;
142190 } ) ;
143191 } ,
144192) ;
@@ -172,14 +220,16 @@ test.cb("should read from cache directory if cached file exists", t => {
172220
173221 webpack ( config , err => {
174222 t . is ( err , null ) ;
175- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
176- t . true ( cacheDB . getStats ( ) . entryCount > 0 ) ;
177- t . end ( ) ;
223+ fs . readdir ( t . context . cacheDirectory , ( err , files ) => {
224+ t . is ( err , null ) ;
225+ t . true ( files . length > 0 ) ;
226+ t . end ( ) ;
227+ } ) ;
178228 } ) ;
179229 } ) ;
180230} ) ;
181231
182- test . cb ( "should have one cache entry per module" , t => {
232+ test . cb ( "should have one file per module" , t => {
183233 const config = Object . assign ( { } , globalConfig , {
184234 output : {
185235 path : t . context . directory ,
@@ -204,13 +254,15 @@ test.cb("should have one cache entry per module", t => {
204254 t . deepEqual ( stats . compilation . errors , [ ] ) ;
205255 t . deepEqual ( stats . compilation . warnings , [ ] ) ;
206256
207- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
208- t . true ( cacheDB . getStats ( ) . entryCount === 3 ) ;
209- t . end ( ) ;
257+ fs . readdir ( t . context . cacheDirectory , ( err , files ) => {
258+ t . is ( err , null ) ;
259+ t . true ( files . length === 3 ) ;
260+ t . end ( ) ;
261+ } ) ;
210262 } ) ;
211263} ) ;
212264
213- test . cb ( "should add a new cache entry if the identifier changes" , t => {
265+ test . cb ( "should generate a new file if the identifier changes" , t => {
214266 const configs = [
215267 Object . assign ( { } , globalConfig , {
216268 output : {
@@ -261,9 +313,11 @@ test.cb("should add a new cache entry if the identifier changes", t => {
261313 counter -= 1 ;
262314
263315 if ( ! counter ) {
264- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
265- t . true ( cacheDB . getStats ( ) . entryCount === 6 ) ;
266- t . end ( ) ;
316+ fs . readdir ( t . context . cacheDirectory , ( err , files ) => {
317+ t . is ( err , null ) ;
318+ t . true ( files . length === 6 ) ;
319+ t . end ( ) ;
320+ } ) ;
267321 }
268322 } ) ;
269323 } ) ;
@@ -320,8 +374,10 @@ test.cb("should allow to specify the .babelrc file", t => {
320374 t . deepEqual ( multiStats . stats [ 1 ] . compilation . errors , [ ] ) ;
321375 t . deepEqual ( multiStats . stats [ 1 ] . compilation . warnings , [ ] ) ;
322376
323- const cacheDB = open ( t . context . cacheDirectory , { readOnly : true } ) ;
324- t . true ( cacheDB . getStats ( ) . entryCount === 1 ) ;
325- t . end ( ) ;
377+ fs . readdir ( t . context . cacheDirectory , ( err , files ) => {
378+ t . is ( err , null ) ;
379+ t . true ( files . length === 2 ) ;
380+ t . end ( ) ;
381+ } ) ;
326382 } ) ;
327383} ) ;
0 commit comments