Skip to content

Commit

Permalink
fix(root-user): don't check for root user on windows/macos
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
acburdine committed Feb 2, 2018
1 parent 59f5d03 commit 3db21d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/utils/check-root-user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict';

const os = require('os');
const chalk = require('chalk');

function checkRootUser() {
// Skip if we're on windows
if (os.platform() !== 'linux') {
return;
}

if (process.getuid() === 0) {
console.error(`${chalk.yellow('Can\'t run command as \'root\' user.')}
Please create a new user with regular account privileges and use this user to run the command.
Expand Down
25 changes: 24 additions & 1 deletion test/unit/utils/check-root-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@

const expect = require('chai').expect;
const sinon = require('sinon');
const os = require('os');
const checkRootUser = require('../../../lib/utils/check-root-user');

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

afterEach(() => {
sandbox.restore();
})
});

it('skips check if run on windows', function () {
const osStub = sandbox.stub(os, 'platform').returns('win32');
const processStub = sandbox.stub(process, 'getuid').returns(0);

checkRootUser('test');
expect(osStub.calledOnce).to.be.true;
expect(processStub.called).to.be.false;
});

it('skips check if run on macos', function () {
const osStub = sandbox.stub(os, 'platform').returns('darwin');
const processStub = sandbox.stub(process, 'getuid').returns(0);

checkRootUser('test');
expect(osStub.calledOnce).to.be.true;
expect(processStub.called).to.be.false;
});

it('throws error command run with root', function () {
const osStub = sandbox.stub(os, 'platform').returns('linux');
const processStub = sandbox.stub(process, 'getuid').returns(0);
const exitStub = sandbox.stub(process, 'exit').throws();
const errorStub = sandbox.stub(console, 'error');
Expand All @@ -21,6 +41,7 @@ describe('checkRootUser', function () {
throw new Error('should not be thrown');
} catch (e) {
expect(e.message).to.not.equal('should not be thrown');
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.true;
expect(exitStub.calledOnce).to.be.true;
Expand All @@ -29,11 +50,13 @@ describe('checkRootUser', function () {
});

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

checkRootUser('test');
expect(osStub.calledOnce).to.be.true;
expect(processStub.calledOnce).to.be.true;
expect(errorStub.calledOnce).to.be.false;
expect(exitStub.calledOnce).to.be.false;
Expand Down

0 comments on commit 3db21d6

Please sign in to comment.