2
2
const fs = require ( 'fs' ) ;
3
3
const path = require ( 'path' ) ;
4
4
const expect = require ( 'chai' ) . expect ;
5
+ const minimatch = require ( 'minimatch' ) ;
5
6
6
7
// Local modules.
7
8
const optimizeImage = require ( '../lib/optimizeImage' ) ;
8
9
9
10
// Configure.
10
- const fixture = path . join ( __dirname , 'fixture.png' ) ;
11
- const size = fs . statSync ( fixture ) . size ;
11
+ const fileSize = { } ;
12
+ const fixtures = [ ] ;
13
+ fs . readdir ( path . resolve ( __dirname , './fixture' ) , ( err , files ) => {
14
+ if ( err ) {
15
+ console . error ( err ) ;
16
+ return [ ] ;
17
+ }
18
+ files . forEach ( file => {
19
+ const filePath = path . resolve ( __dirname , './fixture' , file )
20
+ fixtures . push ( filePath ) ;
21
+ fileSize [ filePath ] = fs . statSync ( filePath ) . size ;
22
+ } )
23
+ } ) ;
12
24
13
25
// Stub hexo.route.
14
26
const hexoRoute = {
15
- buffer : null ,
27
+ buffer : { } ,
16
28
get : function ( name ) {
17
29
return fs . createReadStream ( name ) ;
18
30
} ,
19
31
list : function ( ) {
20
- return [ fixture ] ;
32
+ return fixtures ;
21
33
} ,
22
34
set : function ( name , buffer ) {
23
- this . buffer = buffer ; // Save.
35
+ this . buffer [ name ] = buffer ; // Save.
24
36
}
25
37
} ;
26
38
27
39
// Test suite.
28
40
describe ( 'hexo-image-minifier' , function ( ) {
29
41
// Reset the buffer.
30
42
beforeEach ( 'hexoRoute' , function ( ) {
31
- hexoRoute . buffer = null ;
43
+ hexoRoute . buffer = { } ;
32
44
} ) ;
33
45
34
46
// Tests.
35
- it ( 'should minify an image.' , function ( ) {
47
+ it ( 'should minify an image.' , ( ) => {
36
48
// Configure.
37
49
const hexo = {
38
50
config : {
@@ -49,13 +61,15 @@ describe('hexo-image-minifier', function () {
49
61
} ;
50
62
// Filter and test.
51
63
const promise = optimizeImage . call ( hexo ) ;
52
- return promise . then ( function ( ) {
53
- expect ( hexoRoute . buffer !== null ) ;
54
- expect ( size > hexoRoute . buffer . length )
64
+ return promise . then ( ( ) => {
65
+ for ( const file of fixtures ) {
66
+ expect ( hexoRoute . buffer [ file ] ) . to . be . ok ;
67
+ expect ( fileSize [ file ] ) . to . be . greaterThan ( hexoRoute . buffer [ file ] . length ) ;
68
+ }
55
69
} ) ;
56
70
} ) ;
57
71
58
- it ( 'should do nothing if disabled.' , function ( ) {
72
+ it ( 'should do nothing if disabled.' , ( ) => {
59
73
// Configure.
60
74
const hexo = {
61
75
config : {
@@ -65,7 +79,34 @@ describe('hexo-image-minifier', function () {
65
79
} ;
66
80
67
81
// Filter and test.
68
- optimizeImage . call ( hexo ) ;
69
- expect ( hexoRoute . buffer ) . to . be . null ;
82
+ expect ( optimizeImage . call ( hexo ) ) . to . be . undefined ;
83
+ expect ( hexoRoute . buffer ) . to . be . empty ;
84
+ } ) ;
85
+
86
+ it ( 'should exclude files when the file extensions are listed in `exclude` options' , ( ) => {
87
+ const exclude = [ '*.svg' ] ;
88
+ // Configure.
89
+ const hexo = {
90
+ config : {
91
+ image_minifier : {
92
+ exclude,
93
+ optimizationLevel : 3 ,
94
+ }
95
+ } ,
96
+ route : hexoRoute
97
+ } ;
98
+
99
+ // Filter and test.
100
+ const promise = optimizeImage . call ( hexo ) ;
101
+ return promise . then ( ( ) => {
102
+ for ( const file of fixtures ) {
103
+ if ( exclude . every ( pattern => ! minimatch ( file , pattern , { nocase : true , matchBase : true } ) ) ) {
104
+ expect ( hexoRoute . buffer [ file ] ) . to . be . ok ;
105
+ expect ( fileSize [ file ] ) . to . be . greaterThan ( hexoRoute . buffer [ file ] . length ) ;
106
+ } else {
107
+ expect ( hexoRoute . buffer [ file ] ) . to . be . undefined ;
108
+ }
109
+ }
110
+ } ) ;
70
111
} ) ;
71
112
} ) ;
0 commit comments