11import chai from 'chai' ;
22import sinon from 'sinon' ;
3- import proxyquire from 'proxyquire' ;
4- import { writeFile , unlink , rmdir } from 'hexo-fs' ;
5- import { join } from 'path' ;
3+ import rewire from 'rewire' ;
4+ import ConsoleExtend from '../../lib/extend/console' ;
65chai . should ( ) ;
76
87require ( 'chai' ) . should ( ) ;
@@ -12,47 +11,261 @@ describe('hexo', () => {
1211
1312 it ( 'run help if no specified command' , async ( ) => {
1413 const spy = sinon . spy ( ) ;
15- const hexo = proxyquire ( '../../dist/hexo' , {
16- './console' ( ctx ) {
17- ctx . extend . console . register ( 'help' , spy ) ;
14+ const hexo = rewire ( '../../dist/hexo' ) ;
15+ return hexo . __with__ ( {
16+ console_1 : {
17+ default : ctx => {
18+ ctx . extend . console . register ( 'help' , spy ) ;
19+ }
1820 }
21+ } ) ( async ( ) => {
22+ // @ts -expect-error
23+ await hexo ( cwd , { _ : [ ] } ) ;
24+ spy . calledOnce . should . be . true ;
1925 } ) ;
20-
21- await hexo ( cwd , { _ : [ ] } ) ;
22- spy . calledOnce . should . be . true ;
2326 } ) ;
2427
2528 it ( 'run specified command' , async ( ) => {
2629 const spy = sinon . spy ( ) ;
27- const hexo = proxyquire ( '../../dist/hexo' , {
28- './console' ( ctx ) {
29- ctx . extend . console . register ( 'test' , spy ) ;
30+ const hexo = rewire ( '../../dist/hexo' ) ;
31+ return hexo . __with__ ( {
32+ console_1 : {
33+ default : ctx => {
34+ ctx . extend . console . register ( 'test' , spy ) ;
35+ }
3036 }
37+ } ) ( async ( ) => {
38+ // @ts -expect-error
39+ await hexo ( cwd , { _ : [ 'test' ] } ) ;
40+ spy . calledOnce . should . be . true ;
3141 } ) ;
32-
33- await hexo ( cwd , { _ : [ 'test' ] } ) ;
34- spy . calledOnce . should . be . true ;
3542 } ) ;
3643
3744 it ( 'run help if specified command not found' , async ( ) => {
3845 const spy = sinon . spy ( ) ;
39- const hexo = proxyquire ( '../../dist/hexo' , {
40- './console' ( ctx ) {
41- ctx . extend . console . register ( 'help' , spy ) ;
46+ const hexo = rewire ( '../../dist/hexo' ) ;
47+ return hexo . __with__ ( {
48+ console_1 : {
49+ default : ctx => {
50+ ctx . extend . console . register ( 'help' , spy ) ;
51+ }
52+ }
53+ } ) ( async ( ) => {
54+ // @ts -expect-error
55+ await hexo ( cwd , { _ : [ 'test' ] } ) ;
56+ spy . calledOnce . should . be . true ;
57+ } ) ;
58+ } ) ;
59+
60+ it ( 'path - number (issue hexo#4334)' , async ( ) => {
61+ let args ;
62+ const hexo = rewire ( '../../dist/hexo' ) ;
63+ return hexo . __with__ ( {
64+ find_pkg_1 : {
65+ default : ( _cwd , _args ) => {
66+ args = _args ;
67+ return Promise . resolve ( ) ;
68+ }
69+ }
70+ } ) ( async ( ) => {
71+ process . argv = [ 'hexo' , 'new' , '--path' , '123' , 'test' ] ;
72+ // @ts -expect-error
73+ hexo ( null , null ) ;
74+ args . path . should . eql ( '123' ) ;
75+ process . argv = [ ] ;
76+ } ) ;
77+ } ) ;
78+
79+ it ( 'p - number (issue hexo#4334)' , async ( ) => {
80+ let args ;
81+ const hexo = rewire ( '../../dist/hexo' ) ;
82+ return hexo . __with__ ( {
83+ find_pkg_1 : {
84+ default : ( _cwd , _args ) => {
85+ args = _args ;
86+ return Promise . resolve ( ) ;
87+ }
4288 }
89+ } ) ( async ( ) => {
90+ process . argv = [ 'hexo' , 'new' , '-p' , '123' , 'test' ] ;
91+ // @ts -expect-error
92+ hexo ( null , null ) ;
93+ args . p . should . eql ( '123' ) ;
94+ process . argv = [ ] ;
4395 } ) ;
96+ } ) ;
4497
45- await hexo ( cwd , { _ : [ 'test' ] } ) ;
46- spy . calledOnce . should . be . true ;
98+ it ( 'slug - number (issue hexo#4334)' , async ( ) => {
99+ let args ;
100+ const hexo = rewire ( '../../dist/hexo' ) ;
101+ return hexo . __with__ ( {
102+ find_pkg_1 : {
103+ default : ( _cwd , _args ) => {
104+ args = _args ;
105+ return Promise . resolve ( ) ;
106+ }
107+ }
108+ } ) ( async ( ) => {
109+ process . argv = [ 'hexo' , 'new' , '--slug' , '123' , 'test' ] ;
110+ // @ts -expect-error
111+ hexo ( null , null ) ;
112+ args . slug . should . eql ( '123' ) ;
113+ process . argv = [ ] ;
114+ } ) ;
115+ } ) ;
116+
117+ it ( 's - number (issue hexo#4334)' , async ( ) => {
118+ let args ;
119+ const hexo = rewire ( '../../dist/hexo' ) ;
120+ return hexo . __with__ ( {
121+ find_pkg_1 : {
122+ default : ( _cwd , _args ) => {
123+ args = _args ;
124+ return Promise . resolve ( ) ;
125+ }
126+ }
127+ } ) ( async ( ) => {
128+ process . argv = [ 'hexo' , 'new' , '-s' , '123' , 'test' ] ;
129+ // @ts -expect-error
130+ hexo ( null , null ) ;
131+ args . s . should . eql ( '123' ) ;
132+ process . argv = [ ] ;
133+ } ) ;
47134 } ) ;
48135
49136 it ( 'should call init() method' ) ;
50137
51- it ( 'should handle error properly' ) ;
138+ it ( 'should handle HexoNotFoundError properly' , ( ) => {
139+ const spy = sinon . spy ( ) ;
140+ const hexo = rewire ( '../../dist/hexo' ) ;
141+ const dummyPath = 'dummy' ;
142+ const dummyError = 'test' ;
143+ return hexo . __with__ ( {
144+ find_pkg_1 : {
145+ default : ( ) => Promise . resolve ( dummyPath )
146+ } ,
147+ loadModule : ( ) => Promise . reject ( new Error ( dummyError ) ) ,
148+ context_1 : {
149+ default : class Context {
150+ log : { error : typeof spy } ;
151+ constructor ( ) {
152+ this . log = {
153+ error : spy
154+ } ;
155+ }
156+ }
157+ }
158+ } ) ( async ( ) => {
159+ // @ts -expect-error
160+ await hexo ( cwd , { _ : [ 'test' ] } ) ;
161+ spy . args [ 0 ] [ 0 ] . should . eql ( dummyError ) ;
162+ spy . args [ 1 ] [ 0 ] . should . eql ( 'Local hexo loading failed in %s' ) ;
163+ spy . args [ 1 ] [ 1 ] . should . eql ( `\x1B[35m${ dummyPath } \x1B[39m` ) ;
164+ spy . args [ 2 ] [ 0 ] . should . eql ( 'Try running: \'rm -rf node_modules && npm install --force\'' ) ;
165+ process . exitCode ?. should . eql ( 2 ) ;
166+ } ) ;
167+ } ) ;
52168
53- it ( 'should watch SIGINT signal' ) ;
169+ it ( 'should handle other Error properly' , ( ) => {
170+ const spy = sinon . spy ( ) ;
171+ const hexo = rewire ( '../../dist/hexo' ) ;
172+ const dummyPath = 'dummy' ;
173+ const dummyError = 'error' ;
174+ return hexo . __with__ ( {
175+ find_pkg_1 : {
176+ default : ( ) => Promise . resolve ( dummyPath )
177+ } ,
178+ loadModule : ( ) => Promise . resolve ( ) ,
179+ console_1 : {
180+ default : ( ) => { /* empty */ }
181+ } ,
182+ context_1 : {
183+ default : class Context {
184+ log : { error : typeof spy , fatal : typeof spy } ;
185+ constructor ( ) {
186+ this . log = {
187+ error : spy ,
188+ fatal : spy
189+ } ;
190+ }
191+ init ( ) {
192+ throw new Error ( dummyError ) ;
193+ }
194+ }
195+ }
196+ } ) ( async ( ) => {
197+ // @ts -expect-error
198+ await hexo ( cwd , { _ : [ 'test' ] } ) ;
199+ spy . args [ 0 ] [ 0 ] . message . should . eql ( dummyError ) ;
200+ process . exitCode ?. should . eql ( 2 ) ;
201+ } ) ;
202+ } ) ;
54203
55- it ( 'load hexo module in parent folder recursively' ) ;
204+ it ( 'should watch SIGINT signal' , ( ) => {
205+ const spy = sinon . spy ( ) ;
206+ const watchSpy = sinon . spy ( ) ;
207+ const exitSpy = sinon . spy ( ) ;
208+ const dummyPath = 'dummy' ;
209+ const hexo = rewire ( '../../dist/hexo' ) ;
210+ const processSpy = {
211+ on : process . on ,
212+ emit : process . emit ,
213+ exit : exitSpy
214+ } ;
215+ return hexo . __with__ ( {
216+ find_pkg_1 : {
217+ default : ( ) => Promise . resolve ( dummyPath )
218+ } ,
219+ loadModule : ( ) => Promise . resolve ( ) ,
220+ console_1 : {
221+ default : ( ) => { /* empty */ }
222+ } ,
223+ process : processSpy ,
224+ context_1 : {
225+ default : class Context {
226+ log : { error : typeof spy , fatal : typeof spy , info : typeof spy } ;
227+ extend : {
228+ console : ConsoleExtend ;
229+ } ;
230+ constructor ( ) {
231+ this . log = {
232+ error : spy ,
233+ fatal : spy ,
234+ info : spy
235+ } ;
236+ this . extend = {
237+ console : new ConsoleExtend ( )
238+ } ;
239+ }
240+ init ( ) {
241+ return Promise . resolve ( ) ;
242+ }
243+ call ( ) {
244+ return Promise . resolve ( processSpy . emit ( 'SIGINT' ) ) ;
245+ }
246+ unwatch ( ) {
247+ watchSpy ( ) ;
248+ }
249+ exit ( ) {
250+ return Promise . resolve ( ) ;
251+ }
252+ }
253+ }
254+ } ) ( async ( ) => {
255+ // @ts -expect-error
256+ await hexo ( cwd , { _ : [ 'help' ] } ) ;
257+ [
258+ 'Good bye' ,
259+ 'See you again' ,
260+ 'Farewell' ,
261+ 'Have a nice day' ,
262+ 'Bye!' ,
263+ 'Catch you later'
264+ ] . includes ( spy . args [ 0 ] [ 0 ] ) . should . be . true ;
265+ watchSpy . calledOnce . should . be . true ;
266+ exitSpy . calledOnce . should . be . true ;
267+ } ) ;
268+ } ) ;
56269
57- it ( 'display error message if failed to load hexo module ' ) ;
270+ it ( 'load hexo module in parent folder recursively ' ) ;
58271} ) ;
0 commit comments