Skip to content

Commit

Permalink
Merge pull request #41 from timkendrick/feature-mock-cwd
Browse files Browse the repository at this point in the history
Mock process.cwd and process.chdir methods.
  • Loading branch information
tschaub committed Jun 25, 2015
2 parents 28d8839 + 5e41c3e commit 1a11c2a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var semver = require('semver');

var Binding = require('./binding');
var FileSystem = require('./filesystem');
var FSError = require('./error');

var versions = {
'0.8.x': 'fs-0.8.26.js',
Expand Down Expand Up @@ -48,12 +49,21 @@ for (var name in mockFs) {
realFs[name] = mockFs[name];
}
}
var originalProcess = {
cwd: process.cwd,
chdir: process.chdir
};

function setBinding(binding, Stats) {
mockFs.__set__('binding', binding);
mockFs.Stats = realFs.Stats = Stats;
}

function setProcess(cwd, chdir) {
process.cwd = cwd;
process.chdir = chdir;
}


/**
* Swap out the fs bindings for a mock file system.
Expand All @@ -63,6 +73,19 @@ var exports = module.exports = function mock(config) {
var system = FileSystem.create(config);
var binding = new Binding(system);
setBinding(binding, binding.Stats);

var currentPath = process.cwd();
setProcess(
function cwd() {
return currentPath;
},
function chdir(directory) {
if (!mockFs.statSync(directory).isDirectory()) {
throw new FSError('ENOTDIR');
}
currentPath = path.resolve(currentPath, directory);
}
);
};


Expand All @@ -71,6 +94,7 @@ var exports = module.exports = function mock(config) {
*/
exports.restore = function() {
setBinding(originalBinding, originalStats);
setProcess(originalProcess.cwd, originalProcess.chdir);
};


Expand Down
90 changes: 90 additions & 0 deletions test/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,96 @@ describe('Mocking the file system', function() {

});

describe('process.cwd()', function() {

afterEach(mock.restore);

it('maintains current working directory', function() {

var originalCwd = process.cwd();
mock();

var cwd = process.cwd();
assert.equal(cwd, originalCwd);

});

it('allows changing directory', function() {

var originalCwd = process.cwd();
mock({
'dir': {}
});

process.chdir('dir');
var cwd = process.cwd();
assert.equal(cwd, path.join(originalCwd, 'dir'));

});

it('prevents changing directory to non-existent path', function() {

var originalCwd = process.cwd();
mock();

var err;
try {
process.chdir('dir');
} catch (e) {
err = e;
}
assert.instanceOf(err, Error);
assert.equal(err.code, 'ENOENT');

});

it('prevents changing directory to non-directory path', function() {

var originalCwd = process.cwd();
mock({
'file': ''
});

var err;
try {
process.chdir('file');
} catch (e) {
err = e;
}
assert.instanceOf(err, Error);
assert.equal(err.code, 'ENOTDIR');

});

it('restores original methods on restore', function() {

var originalCwd = process.cwd;
var originalChdir = process.chdir;
mock();

mock.restore();
assert.equal(process.cwd, originalCwd);
assert.equal(process.chdir, originalChdir);

});

it('restores original working directory on restore', function() {

var originalCwd = process.cwd();
mock({
'dir': {}
});

process.chdir('dir');
mock.restore();

var cwd = process.cwd();
assert.equal(cwd, originalCwd);

});

});

if (process.getuid && process.getgid) {

describe('security', function() {
Expand Down

0 comments on commit 1a11c2a

Please sign in to comment.