2
2
3
3
const expect = require ( 'chai' ) . expect ;
4
4
const sinon = require ( 'sinon' ) ;
5
+ const os = require ( 'os' ) ;
5
6
const checkRootUser = require ( '../../../lib/utils/check-root-user' ) ;
6
7
7
8
describe ( 'checkRootUser' , function ( ) {
8
9
const sandbox = sinon . sandbox . create ( ) ;
9
10
10
11
afterEach ( ( ) => {
11
12
sandbox . restore ( ) ;
12
- } )
13
+ } ) ;
14
+
15
+ it ( 'skips check if run on windows' , function ( ) {
16
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'win32' ) ;
17
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
18
+
19
+ checkRootUser ( 'test' ) ;
20
+ expect ( osStub . calledOnce ) . to . be . true ;
21
+ expect ( processStub . called ) . to . be . false ;
22
+ } ) ;
23
+
24
+ it ( 'skips check if run on macos' , function ( ) {
25
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'darwin' ) ;
26
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
27
+
28
+ checkRootUser ( 'test' ) ;
29
+ expect ( osStub . calledOnce ) . to . be . true ;
30
+ expect ( processStub . called ) . to . be . false ;
31
+ } ) ;
13
32
14
33
it ( 'throws error command run with root' , function ( ) {
34
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
15
35
const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
16
36
const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
17
37
const errorStub = sandbox . stub ( console , 'error' ) ;
@@ -21,6 +41,7 @@ describe('checkRootUser', function () {
21
41
throw new Error ( 'should not be thrown' ) ;
22
42
} catch ( e ) {
23
43
expect ( e . message ) . to . not . equal ( 'should not be thrown' ) ;
44
+ expect ( osStub . calledOnce ) . to . be . true ;
24
45
expect ( processStub . calledOnce ) . to . be . true ;
25
46
expect ( errorStub . calledOnce ) . to . be . true ;
26
47
expect ( exitStub . calledOnce ) . to . be . true ;
@@ -29,11 +50,13 @@ describe('checkRootUser', function () {
29
50
} ) ;
30
51
31
52
it ( 'doesn\'t do anything if command run as non root user' , function ( ) {
53
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
32
54
const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 501 ) ;
33
55
const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
34
56
const errorStub = sandbox . stub ( console , 'error' ) ;
35
57
36
58
checkRootUser ( 'test' ) ;
59
+ expect ( osStub . calledOnce ) . to . be . true ;
37
60
expect ( processStub . calledOnce ) . to . be . true ;
38
61
expect ( errorStub . calledOnce ) . to . be . false ;
39
62
expect ( exitStub . calledOnce ) . to . be . false ;
0 commit comments