@@ -4,32 +4,63 @@ var pathFn = require('path');
44var should = require ( 'chai' ) . should ( ) ; // eslint-disable-line
55var fs = require ( 'hexo-fs' ) ;
66var yml = require ( 'js-yaml' ) ;
7+ var sinon = require ( 'sinon' )
8+ var rewire = require ( 'rewire' )
79
810describe ( 'config flag handling' , function ( ) {
911 var Hexo = require ( '../../../lib/hexo' ) ;
10- var hexo = new Hexo ( pathFn . join ( __dirname , 'test_dir' ) , {
11- silent : true ,
12- debug : true
13- } ) ;
12+ var hexo = new Hexo ( pathFn . join ( __dirname , 'test_dir' ) ) ;
1413
1514 var mcp = require ( '../../../lib/hexo/multi_config_path' ) ( hexo ) ;
1615 var base = hexo . base_dir ;
1716
18- var debugPath = pathFn . resolve ( 'debug.log' ) ;
19-
20- function readDebug ( ) {
21- return fs . readFile ( debugPath ) . then ( function ( out ) {
22- var lines = out . split ( '\n' ) ;
23-
24- var debug = [ ] ;
25- for ( var i = 0 ; i < lines . length ; i ++ ) {
26- debug . push ( yml . safeLoad ( lines [ i ] ) , { 'json' : true } ) ;
17+ function consoleReader ( ) {
18+ this . reader = [ ]
19+ this . i = function ( ) {
20+ var type = 'info' ;
21+ var message = '' ;
22+ for ( var i = 0 ; i < arguments . length ; ) {
23+ message += arguments [ i ] ;
24+ if ( ++ i < arguments . length )
25+ message += ' ' ;
2726 }
28-
29- return debug ;
30- } ) ;
27+ this . reader . push ( {
28+ type : type ,
29+ msg : message
30+ } )
31+ } . bind ( this ) ;
32+
33+ this . w = function ( ) {
34+ var type = 'warning' ;
35+ var message = '' ;
36+ for ( var i = 0 ; i < arguments . length ; ) {
37+ message += arguments [ i ] ;
38+ if ( ++ i < arguments . length )
39+ message += ' ' ;
40+ }
41+ this . reader . push ( {
42+ type : type ,
43+ msg : message
44+ } )
45+ } . bind ( this ) ;
46+
47+ this . e = function ( ) {
48+ var type = 'error' ;
49+ var message = '' ;
50+ for ( var i = 0 ; i < arguments . length ; ) {
51+ message += arguments [ i ] ;
52+ if ( ++ i < arguments . length )
53+ message += ' ' ;
54+ }
55+ this . reader . push ( {
56+ type : type ,
57+ msg : message
58+ } )
59+ } . bind ( this ) ;
3160 }
3261
62+ hexo . log = new consoleReader ( ) ;
63+
3364 var testYaml1 = [
3465 'author: foo' ,
3566 'type: dinosaur' ,
@@ -64,8 +95,6 @@ describe('config flag handling', function() {
6495 ] . join ( '\n' ) ;
6596
6697 before ( function ( ) {
67- if ( fs . existsSync ( debugPath ) ) fs . unlinkSync ( debugPath ) ;
68-
6998 fs . writeFileSync ( base + 'test1.yml' , testYaml1 ) ;
7099 fs . writeFileSync ( base + 'test2.yml' , testYaml2 ) ;
71100 fs . writeFileSync ( base + 'test1.json' , testJson1 ) ;
@@ -74,7 +103,7 @@ describe('config flag handling', function() {
74103 } ) ;
75104
76105 afterEach ( function ( ) {
77- if ( fs . existsSync ( debugPath ) ) fs . unlinkSync ( debugPath ) ;
106+ hexo . log . reader = [ ]
78107 return ;
79108 } ) ;
80109
@@ -84,10 +113,8 @@ describe('config flag handling', function() {
84113
85114 it ( 'no file' , function ( ) {
86115 mcp ( base ) . should . equal ( base + '_config.yml' ) ;
87- readDebug ( ) . then ( function ( debug ) {
88- debug [ 0 ] . level . should . eql ( 40 ) ;
89- debug [ 0 ] . msg . should . eql ( 'No config file entered.' ) ;
90- } ) ;
116+ hexo . log . reader [ 0 ] . type . should . eql ( 'warning' ) ;
117+ hexo . log . reader [ 0 ] . msg . should . eql ( 'No config file entered.' ) ;
91118 } ) ;
92119
93120 it ( '1 file' , function ( ) {
@@ -101,12 +128,11 @@ describe('config flag handling', function() {
101128 it ( '1 not found file warning' , function ( ) {
102129 var notFile = 'not_a_file.json' ;
103130
131+
104132 mcp ( base , notFile ) . should . eql ( pathFn . join ( base , '_config.yml' ) ) ;
105- readDebug ( ) . then ( function ( debug ) {
106- debug [ 0 ] . level . should . eql ( 40 ) ;
107- debug [ 0 ] . msg . should . eql ( 'Config file ' + notFile +
108- ' not found, using default.' ) ;
109- } ) ;
133+ hexo . log . reader [ 0 ] . type . should . eql ( 'warning' ) ;
134+ hexo . log . reader [ 0 ] . msg . should . eql ( 'Config file ' + notFile +
135+ ' not found, using default.' ) ;
110136 } ) ;
111137
112138 it ( 'combined config output' , function ( ) {
@@ -117,10 +143,24 @@ describe('config flag handling', function() {
117143 mcp ( base , 'test1.yml,test1.json' ) . should . eql ( combinedPath ) ;
118144 mcp ( base , 'test1.json,test2.json' ) . should . eql ( combinedPath ) ;
119145 mcp ( base , 'notafile.yml,test1.json' ) . should . eql ( combinedPath ) ;
146+
147+ hexo . log . reader [ 0 ] . type . should . eql ( 'info' ) ;
148+ hexo . log . reader [ 0 ] . msg . should . eql ( 'Config based on 2 files' ) ;
149+ hexo . log . reader [ 3 ] . type . should . eql ( 'warning' ) ;
150+ hexo . log . reader [ 3 ] . msg . should . eql ( 'Config file notafile.yml not found.' ) ;
151+ hexo . log . reader [ 4 ] . type . should . eql ( 'info' ) ;
152+ hexo . log . reader [ 4 ] . msg . should . eql ( 'Config based on 1 files' ) ;
153+ // because who cares about grammar anyway?
154+
155+ mcp ( base , 'notafile.yml,alsonotafile.json' ) . should . not . eql ( combinedPath ) ;
156+ hexo . log . reader [ 7 ] . type . should . eql ( 'error' ) ;
157+ hexo . log . reader [ 7 ] . msg . should . eql ( 'No config files found.' +
158+ ' Using _config.yml.' ) ;
120159 } ) ;
121160
122161 it ( '2 YAML overwrite' , function ( ) {
123- var config = fs . readFileSync ( mcp ( base , 'test1.yml,test2.yml' ) ) ;
162+ var configFile = mcp ( base , 'test1.yml,test2.yml' ) ;
163+ var config = fs . readFileSync ( configFile ) ;
124164 config = yml . safeLoad ( config ) ;
125165
126166 config . author . should . eql ( 'bar' ) ;
@@ -166,8 +206,4 @@ describe('config flag handling', function() {
166206 config . favorites . food . should . eql ( 'sushi' ) ;
167207 config . type . should . eql ( 'dinosaur' ) ;
168208 } ) ;
169-
170- it ( 'multifile warnings' , function ( ) {
171-
172- } ) ;
173209} ) ;
0 commit comments