|
| 1 | +'use strict'; |
| 2 | +var assert = require('assert'); |
| 3 | +var gutil = require('gulp-util'); |
| 4 | +var revOutdated = require('./index'); |
| 5 | +var path = require('path'); |
| 6 | + |
| 7 | +var assets = [ |
| 8 | + {path: 'css/style.css', time: 0}, |
| 9 | + {path: 'css/stylexxx.css', time: 0}, |
| 10 | + {path: 'css/style-22222222.css', time: 1403184415416}, |
| 11 | + {path: 'css/style-61e0be79.css', time: 1403184377571}, |
| 12 | + {path: 'css/style-a42f5380.css', time: 1403184303451}, |
| 13 | + {path: 'css/style-1d87bebe.css', time: 1222222222222}, |
| 14 | + {path: 'css/style-11111111.css', time: 1111111111111}, |
| 15 | + {path: 'css/style-00000000.css', time: 0}, |
| 16 | +]; |
| 17 | + |
| 18 | +var keepQuantity; |
| 19 | +var filteredQantity; |
| 20 | +var fileCount; |
| 21 | + |
| 22 | +it('should filter 5 files', function (cb) { |
| 23 | + keepQuantity = 1; |
| 24 | + filteredQantity = 6 - keepQuantity; |
| 25 | + fileCount = 0; |
| 26 | + |
| 27 | + var stream = initStream( revOutdated(keepQuantity) ); |
| 28 | + |
| 29 | + stream.on('data', streamDataCheck); |
| 30 | + |
| 31 | + stream.on('end', function() { |
| 32 | + assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream'); |
| 33 | + cb(); |
| 34 | + }); |
| 35 | + |
| 36 | + stream.end(); |
| 37 | +}); |
| 38 | + |
| 39 | +it('should filter 4 files using default keepQuantity option', function (cb) { |
| 40 | + keepQuantity = undefined; |
| 41 | + filteredQantity = 6 - 2; |
| 42 | + fileCount = 0; |
| 43 | + |
| 44 | + var stream = initStream( revOutdated() ); |
| 45 | + |
| 46 | + stream.on('data', streamDataCheck); |
| 47 | + |
| 48 | + stream.on('end', function() { |
| 49 | + assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream'); |
| 50 | + cb(); |
| 51 | + }); |
| 52 | + |
| 53 | + stream.end(); |
| 54 | +}); |
| 55 | + |
| 56 | +it('should filter correct files', function (cb) { |
| 57 | + keepQuantity = 3; |
| 58 | + filteredQantity = 6 - keepQuantity; |
| 59 | + fileCount = 0; |
| 60 | + |
| 61 | + var stream = initStream( revOutdated(keepQuantity) ); |
| 62 | + |
| 63 | + stream.on('data', function(file){ |
| 64 | + streamDataCheck(file); |
| 65 | + assert( |
| 66 | + /\/style-(1d87bebe|11111111|00000000)\.css/.test(file.path), |
| 67 | + 'should filter correct files' |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + stream.on('end', function() { |
| 72 | + assert.equal(fileCount, filteredQantity, 'Only ' + filteredQantity + ' files should pass through the stream'); |
| 73 | + cb(); |
| 74 | + }); |
| 75 | + |
| 76 | + stream.end(); |
| 77 | +}); |
| 78 | + |
| 79 | +function initStream(stream) { |
| 80 | + assets.forEach(function(asset){ |
| 81 | + stream.write(new gutil.File({ |
| 82 | + path: asset.path, |
| 83 | + stat: {ctime: new Date(asset.time)}, |
| 84 | + contents: new Buffer(' ') |
| 85 | + })); |
| 86 | + }); |
| 87 | + return stream; |
| 88 | +} |
| 89 | + |
| 90 | +function streamDataCheck (file) { |
| 91 | + assert( |
| 92 | + /\/style-[0-9a-f]{8}\.css/.test(file.path), |
| 93 | + 'should filter only revisioned files' |
| 94 | + ); |
| 95 | + fileCount++; |
| 96 | +} |
0 commit comments