@@ -3,6 +3,7 @@ const expect = require('chai').expect;
3
3
const sinon = require ( 'sinon' ) ;
4
4
const stripAnsi = require ( 'strip-ansi' ) ;
5
5
const proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
6
+ const path = require ( 'path' ) ;
6
7
7
8
const modulePath = '../../../../lib/commands/doctor/checks/install' ;
8
9
const errors = require ( '../../../../lib/errors' ) ;
@@ -34,57 +35,182 @@ describe('Unit: Doctor Checks > Install', function () {
34
35
} ) ;
35
36
36
37
describe ( 'node version check' , function ( ) {
37
- it ( 'doesn\'t do anything if GHOST_NODE_VERSION_CHECK is false' , function ( ) {
38
- let originalEnv = process . env ;
38
+ it ( 'rejects if global bin is different than the one ghost is running from' , function ( ) {
39
+ let execaStub = sinon . stub ( ) . returns ( { stdout : '/usr/local/bin' } ) ;
40
+ let originalArgv = process . argv ;
41
+ process . argv = [ 'node' , '/home/ghost/.nvm/versions/6.11.1/bin' ] ;
42
+
43
+ const task = proxyquire ( modulePath , {
44
+ execa : { shellSync : execaStub }
45
+ } ) . tasks . nodeVersion ;
46
+
47
+ return task ( ) . then ( ( ) => {
48
+ expect ( false , 'error should be thrown' ) . to . be . true ;
49
+ process . argv = originalArgv ;
50
+ } ) . catch ( ( error ) => {
51
+ process . argv = originalArgv ;
52
+ expect ( error ) . to . be . an . instanceof ( errors . SystemError ) ;
53
+ expect ( error . message ) . to . match ( / v e r s i o n o f G h o s t - C L I y o u a r e r u n n i n g w a s n o t i n s t a l l e d w i t h t h i s v e r s i o n o f N o d e ./ ) ;
54
+ expect ( execaStub . calledOnce ) . to . be . true ;
55
+ expect ( execaStub . calledWithExactly ( 'npm bin -g' ) ) . to . be . true ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ it ( 'rejects if node version is not in range' , function ( ) {
39
60
let cliPackage = {
40
61
engines : {
41
62
node : '0.10.0'
42
63
}
43
64
} ;
44
- process . env = { GHOST_NODE_VERSION_CHECK : 'false' } ;
65
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
45
66
46
67
const task = proxyquire ( modulePath , {
47
- '../../../../package' : cliPackage
68
+ '../../../../package' : cliPackage ,
69
+ execa : { shellSync : execaStub }
48
70
} ) . tasks . nodeVersion ;
49
71
50
72
return task ( ) . then ( ( ) => {
51
- process . env = originalEnv ;
73
+ expect ( false , 'error should be thrown' ) . to . be . true ;
74
+ } ) . catch ( ( error ) => {
75
+ expect ( error ) . to . be . an . instanceof ( errors . SystemError ) ;
76
+ let message = stripAnsi ( error . message ) ;
77
+
78
+ expect ( message ) . to . match ( / S u p p o r t e d : 0 .1 0 .0 / ) ;
79
+ expect ( message ) . to . match ( new RegExp ( `Installed: ${ process . versions . node } ` ) ) ;
80
+ expect ( execaStub . calledOnce ) . to . be . true ;
52
81
} ) ;
53
82
} ) ;
54
83
55
- it ( 'doesn\'t do anything if node version is in range ' , function ( ) {
84
+ it ( 'doesn\'t reject if bin is the local ghost bin file from the install (and local is true) ' , function ( ) {
56
85
let cliPackage = {
57
86
engines : {
58
87
node : process . versions . node // this future-proofs the test
59
88
}
60
89
} ;
90
+ let execaStub = sinon . stub ( ) . returns ( { stdout : '/usr/local/bin' } ) ;
91
+ let originalArgv = process . argv ;
92
+ process . argv = [ 'node' , path . join ( __dirname , '../../../../bin/ghost' ) ] ;
61
93
62
- const task = proxyquire ( modulePath , {
63
- '../../../../package' : cliPackage
64
- } ) . tasks . nodeVersion ;
94
+ const tasks = proxyquire ( modulePath , {
95
+ '../../../../package' : cliPackage ,
96
+ execa : { shellSync : execaStub }
97
+ } ) . tasks ;
98
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
65
99
66
- return task ( ) ;
100
+ return tasks . nodeVersion ( { local : true } ) . then ( ( ) => {
101
+ process . argv = originalArgv ;
102
+ expect ( execaStub . calledOnce ) . to . be . true ;
103
+ expect ( checkDirectoryStub . called ) . to . be . false ;
104
+ } ) ;
67
105
} ) ;
68
106
69
- it ( 'throws error if node version is not in range' , function ( ) {
107
+ it ( 'doesn\'t do anything if GHOST_NODE_VERSION_CHECK is false and local is true' , function ( ) {
108
+ let originalEnv = process . env ;
70
109
let cliPackage = {
71
110
engines : {
72
111
node : '0.10.0'
73
112
}
74
113
} ;
114
+ process . env = { GHOST_NODE_VERSION_CHECK : 'false' } ;
115
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
75
116
76
- const task = proxyquire ( modulePath , {
77
- '../../../../package' : cliPackage
78
- } ) . tasks . nodeVersion ;
117
+ const tasks = proxyquire ( modulePath , {
118
+ '../../../../package' : cliPackage ,
119
+ execa : { shellSync : execaStub }
120
+ } ) . tasks ;
121
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
79
122
80
- return task ( ) . then ( ( ) => {
81
- expect ( false , 'error should be thrown' ) . to . be . true ;
82
- } ) . catch ( ( error ) => {
83
- expect ( error ) . to . be . an . instanceof ( errors . SystemError ) ;
84
- let message = stripAnsi ( error . message ) ;
123
+ return tasks . nodeVersion ( { local : true } ) . then ( ( ) => {
124
+ process . env = originalEnv ;
125
+ expect ( execaStub . calledOnce ) . to . be . true ;
126
+ expect ( checkDirectoryStub . called ) . to . be . false ;
127
+ } ) ;
128
+ } ) ;
85
129
86
- expect ( message ) . to . match ( / S u p p o r t e d : 0 .1 0 .0 / ) ;
87
- expect ( message ) . to . match ( new RegExp ( `Installed: ${ process . versions . node } ` ) ) ;
130
+ it ( 'doesn\'t do anything if node version is in range and local is true' , function ( ) {
131
+ let cliPackage = {
132
+ engines : {
133
+ node : process . versions . node // this future-proofs the test
134
+ }
135
+ } ;
136
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
137
+
138
+ const tasks = proxyquire ( modulePath , {
139
+ '../../../../package' : cliPackage ,
140
+ execa : { shellSync : execaStub }
141
+ } ) . tasks ;
142
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
143
+
144
+ return tasks . nodeVersion ( { local : true } ) . then ( ( ) => {
145
+ expect ( execaStub . calledOnce ) . to . be . true ;
146
+ expect ( checkDirectoryStub . called ) . to . be . false ;
147
+ } ) ;
148
+ } ) ;
149
+
150
+ it ( 'doesn\'t call checkDirectoryAndAbove if os is not linux' , function ( ) {
151
+ let cliPackage = {
152
+ engines : {
153
+ node : process . versions . node // this future-proofs the test
154
+ }
155
+ } ;
156
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
157
+ let platformStub = sinon . stub ( ) . returns ( 'darwin' ) ;
158
+
159
+ const tasks = proxyquire ( modulePath , {
160
+ '../../../../package' : cliPackage ,
161
+ execa : { shellSync : execaStub } ,
162
+ os : { platform : platformStub }
163
+ } ) . tasks ;
164
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
165
+
166
+ return tasks . nodeVersion ( { local : false } ) . then ( ( ) => {
167
+ expect ( execaStub . calledOnce ) . to . be . true ;
168
+ expect ( checkDirectoryStub . called ) . to . be . false ;
169
+ } ) ;
170
+ } ) ;
171
+
172
+ it ( 'doesn\'t call checkDirectoryAndAbove if no-setup-linux-user is passed' , function ( ) {
173
+ let cliPackage = {
174
+ engines : {
175
+ node : process . versions . node // this future-proofs the test
176
+ }
177
+ } ;
178
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
179
+ let platformStub = sinon . stub ( ) . returns ( 'linux' ) ;
180
+
181
+ const tasks = proxyquire ( modulePath , {
182
+ '../../../../package' : cliPackage ,
183
+ execa : { shellSync : execaStub } ,
184
+ os : { platform : platformStub }
185
+ } ) . tasks ;
186
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
187
+
188
+ return tasks . nodeVersion ( { local : false , argv : { 'setup-linux-user' : false } } ) . then ( ( ) => {
189
+ expect ( execaStub . calledOnce ) . to . be . true ;
190
+ expect ( checkDirectoryStub . called ) . to . be . false ;
191
+ } ) ;
192
+ } ) ;
193
+
194
+ it ( 'calls checkDirectoryAndAbove if none of the three conditions are true' , function ( ) {
195
+ let cliPackage = {
196
+ engines : {
197
+ node : process . versions . node // this future-proofs the test
198
+ }
199
+ } ;
200
+ let execaStub = sinon . stub ( ) . returns ( { stdout : process . argv [ 1 ] } ) ;
201
+ let platformStub = sinon . stub ( ) . returns ( 'linux' ) ;
202
+
203
+ const tasks = proxyquire ( modulePath , {
204
+ '../../../../package' : cliPackage ,
205
+ execa : { shellSync : execaStub } ,
206
+ os : { platform : platformStub }
207
+ } ) . tasks ;
208
+ let checkDirectoryStub = sinon . stub ( tasks , 'checkDirectoryAndAbove' ) . resolves ( ) ;
209
+
210
+ return tasks . nodeVersion ( { local : false , argv : { 'setup-linux-user' : true } } ) . then ( ( ) => {
211
+ expect ( execaStub . calledOnce ) . to . be . true ;
212
+ expect ( checkDirectoryStub . calledOnce ) . to . be . true ;
213
+ expect ( checkDirectoryStub . calledWith ( process . argv [ 0 ] ) ) . to . be . true ;
88
214
} ) ;
89
215
} ) ;
90
216
} ) ;
0 commit comments