Skip to content

Commit 3db21d6

Browse files
committed
fix(root-user): don't check for root user on windows/macos
- windows doesn't have a getuid function, so we don't want to check it if we're on windows - macos doesn't need the root check either
1 parent 59f5d03 commit 3db21d6

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/utils/check-root-user.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict';
22

3+
const os = require('os');
34
const chalk = require('chalk');
45

56
function checkRootUser() {
7+
// Skip if we're on windows
8+
if (os.platform() !== 'linux') {
9+
return;
10+
}
11+
612
if (process.getuid() === 0) {
713
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')}
814
Please create a new user with regular account privileges and use this user to run the command.

test/unit/utils/check-root-user-spec.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
const expect = require('chai').expect;
44
const sinon = require('sinon');
5+
const os = require('os');
56
const checkRootUser = require('../../../lib/utils/check-root-user');
67

78
describe('checkRootUser', function () {
89
const sandbox = sinon.sandbox.create();
910

1011
afterEach(() => {
1112
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+
});
1332

1433
it('throws error command run with root', function () {
34+
const osStub = sandbox.stub(os, 'platform').returns('linux');
1535
const processStub = sandbox.stub(process, 'getuid').returns(0);
1636
const exitStub = sandbox.stub(process, 'exit').throws();
1737
const errorStub = sandbox.stub(console, 'error');
@@ -21,6 +41,7 @@ describe('checkRootUser', function () {
2141
throw new Error('should not be thrown');
2242
} catch (e) {
2343
expect(e.message).to.not.equal('should not be thrown');
44+
expect(osStub.calledOnce).to.be.true;
2445
expect(processStub.calledOnce).to.be.true;
2546
expect(errorStub.calledOnce).to.be.true;
2647
expect(exitStub.calledOnce).to.be.true;
@@ -29,11 +50,13 @@ describe('checkRootUser', function () {
2950
});
3051

3152
it('doesn\'t do anything if command run as non root user', function () {
53+
const osStub = sandbox.stub(os, 'platform').returns('linux');
3254
const processStub = sandbox.stub(process, 'getuid').returns(501);
3355
const exitStub = sandbox.stub(process, 'exit').throws();
3456
const errorStub = sandbox.stub(console, 'error');
3557

3658
checkRootUser('test');
59+
expect(osStub.calledOnce).to.be.true;
3760
expect(processStub.calledOnce).to.be.true;
3861
expect(errorStub.calledOnce).to.be.false;
3962
expect(exitStub.calledOnce).to.be.false;

0 commit comments

Comments
 (0)