Skip to content

Commit

Permalink
isOwner updates and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Feb 4, 2016
1 parent 1f1790c commit 222113b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/dest/fileOperations/isOwner.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';

function isOwner(fsStat) {
var uid = process.geteuid ? process.geteuid() : process.getuid();
var uid;
if (typeof process.geteuid === 'function') {
uid = process.geteuid();
} else {
uid = process.getuid();
}

if (fsStat.uid !== uid && uid !== 0) {
return false;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"del": "^2.2.0",
"eslint": "^1.10.3",
"eslint-config-gulp": "^2.0.0",
"expect": "^1.14.0",
"github-changes": "^1.0.1",
"istanbul": "^0.3.0",
"istanbul-coveralls": "^1.0.1",
Expand All @@ -48,7 +49,8 @@
"scripts": {
"lint": "eslint . && jscs index.js lib/ test/",
"test": "npm run lint && mocha",
"coveralls": "istanbul cover _mocha && istanbul-coveralls",
"cover": "istanbul cover _mocha",
"coveralls": "npm run cover && istanbul-coveralls",
"changelog": "github-changes -o gulpjs -r vinyl-fs -b master -f ./CHANGELOG.md --order-semver --use-commit-body"
},
"engines": {
Expand Down
90 changes: 90 additions & 0 deletions test/fileOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

var expect = require('expect');

var isOwner = require('../lib/dest/fileOperations/isOwner');

function noop() {}

describe('isOwner', function() {

var ownerStat = {
uid: 9001,
};

var nonOwnerStat = {
uid: 9002,
};

var getuidSpy;
var geteuidSpy;

beforeEach(function(done) {
if (typeof process.geteuid !== 'function') {
process.geteuid = noop;
}

getuidSpy = expect.spyOn(process, 'getuid').andReturn(ownerStat.uid);
geteuidSpy = expect.spyOn(process, 'geteuid').andReturn(ownerStat.uid);

done();
});

afterEach(function(done) {
expect.restoreSpies();

if (process.geteuid === noop) {
delete process.geteuid;
}

console.log(process.geteuid);

done();
});

it('uses process.geteuid() when available', function(done) {

isOwner(ownerStat);

expect(getuidSpy.calls.length).toEqual(0);
expect(geteuidSpy.calls.length).toEqual(1);

done();
});

it('uses process.getuid() when geteuid() is not available', function(done) {
delete process.geteuid;

isOwner(ownerStat);

expect(getuidSpy.calls.length).toEqual(1);

done();
});

it('returns false when non-root and non-owner', function(done) {
var result = isOwner(nonOwnerStat);

expect(result).toEqual(false);

done();
});

it('returns true when owner and non-root', function(done) {
var result = isOwner(ownerStat);

expect(result).toEqual(true);

done();
});

it('returns true when non-owner but root', function(done) {
expect.spyOn(process, 'geteuid').andReturn(0); // 0 is root uid

var result = isOwner(nonOwnerStat);

expect(result).toEqual(true);

done();
});
});

0 comments on commit 222113b

Please sign in to comment.