From 885254627470d9ae661f699c26ce96d73b26b80d Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 13 Jan 2014 14:38:39 -0700 Subject: [PATCH 1/5] Work where process.getuid and getgid are absent --- test/lib/index.spec.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/lib/index.spec.js b/test/lib/index.spec.js index a28ab7f4..15aff4b5 100644 --- a/test/lib/index.spec.js +++ b/test/lib/index.spec.js @@ -372,8 +372,16 @@ describe('Mocking the file system', function() { assert.instanceOf(stats.ctime, Date); assert.instanceOf(stats.mtime, Date); assert.instanceOf(stats.atime, Date); - assert.isNumber(stats.uid); - assert.isNumber(stats.gid); + if (process.getuid) { + assert.isNumber(stats.uid); + } else { + assert.isUndefined(stats.uid); + } + if (process.getgid) { + assert.isNumber(stats.gid); + } else { + assert.isUndefined(stats.gid); + } assert.equal(stats.nlink, 3); assert.isNumber(stats.blocks); assert.isNumber(stats.blksize); From e01ef8df8c59405aaace9da7899a5dc19186e260 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 13 Jan 2014 15:36:51 -0700 Subject: [PATCH 2/5] On Windows, add base directories like \\?\c:\ The path._makeLong function will return paths like these on Windows: \\?\c:\foo\bar The fs.realpath function walks through Windows paths treating '\\?\c:\' as the base directory, so this is what we store. The process.cwd and os.tmpdir functions may not return the same case for base directory names, so we normalize to lowercase. --- lib/filesystem.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/filesystem.js b/lib/filesystem.js index 1021168d..0ead6bab 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -12,7 +12,11 @@ function getPathParts(filepath) { var parts = path._makeLong(path.resolve(filepath)).split(path.sep); parts.shift(); if (isWindows) { + // parts currently looks like ['', '?', 'c:', ...] parts.shift(); + var q = parts.shift(); // should be '?' + var base = '\\\\' + q + '\\' + parts.shift().toLowerCase() + '\\'; + parts.unshift(base); } return parts; } From 8b3ec271bbbc875a6715e15ed4d842940c71bf17 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 13 Jan 2014 15:43:34 -0700 Subject: [PATCH 3/5] Instead of storing the trailing slash, ignore it --- lib/filesystem.js | 5 ++++- test/lib/index.spec.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 0ead6bab..8af6601f 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -15,9 +15,12 @@ function getPathParts(filepath) { // parts currently looks like ['', '?', 'c:', ...] parts.shift(); var q = parts.shift(); // should be '?' - var base = '\\\\' + q + '\\' + parts.shift().toLowerCase() + '\\'; + var base = '\\\\' + q + '\\' + parts.shift().toLowerCase(); parts.unshift(base); } + if (parts[parts.length - 1] === '') { + parts.pop(); + } return parts; } diff --git a/test/lib/index.spec.js b/test/lib/index.spec.js index 15aff4b5..960ba141 100644 --- a/test/lib/index.spec.js +++ b/test/lib/index.spec.js @@ -90,6 +90,34 @@ describe('The API', function() { }); + it('works with a trailing slash', function() { + + mock({ + 'path/to/dir/': mock.directory({ + mtime: new Date(8675309), + mode: 0644 + }) + }); + + assert.isTrue(fs.statSync('path/to/dir').isDirectory()); + assert.isTrue(fs.statSync('path/to/dir/').isDirectory()); + + }); + + it('works without a trailing slash', function() { + + mock({ + 'path/to/dir': mock.directory({ + mtime: new Date(8675309), + mode: 0644 + }) + }); + + assert.isTrue(fs.statSync('path/to/dir').isDirectory()); + assert.isTrue(fs.statSync('path/to/dir/').isDirectory()); + + }); + }); describe('mock.symlink()', function() { From c9a33d3c3be712443bfdd059350e92aa8b1f58a3 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 13 Jan 2014 15:48:42 -0700 Subject: [PATCH 4/5] Test that process.cwd() and os.tmpdir() are created --- test/lib/index.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/lib/index.spec.js b/test/lib/index.spec.js index 960ba141..b9aae304 100644 --- a/test/lib/index.spec.js +++ b/test/lib/index.spec.js @@ -1,4 +1,5 @@ var fs = require('fs'); +var os = require('os'); var path = require('path'); var mock = require('../../lib/index'); @@ -20,6 +21,27 @@ describe('The API', function() { }); + describe('mock()', function() { + + it('creates process.cwd() and os.tmpdir() by default', function() { + mock(); + + assert.isTrue(fs.statSync(process.cwd()).isDirectory()); + var tmp; + if (os.tmpdir) { + tmp = os.tmpdir(); + } else if (os.tmpDir) { + tmp = os.tmpDir(); + } + if (tmp) { + assert.isTrue(fs.statSync(tmp).isDirectory()); + } + + mock.restore(); + }); + + }); + describe('mock.restore()', function() { it('restores bindings for the real file system', function() { From a904f8843438a365ace2f40563cc65f117a2ab75 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 13 Jan 2014 16:07:33 -0700 Subject: [PATCH 5/5] Add note about OS and versions --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index e2f96ce0..2b0670a3 100644 --- a/readme.md +++ b/readme.md @@ -188,4 +188,6 @@ Mock `fs.Stats` objects have the following properties: `dev`, `ino`, `nlink`, `m The following `fs` functions are *not* currently mocked (if your tests use these, they will work against the real file system): `fs.FSWatcher`, `fs.unwatchFile`, `fs.watch`, and `fs.watchFile`. Pull requests welcome. +Tested on Linux, OSX, and Windows using Node 0.8, 0.10, and 0.11. Check the tickets for a list of [known issues](https://github.com/tschaub/mock-fs/issues). + [![Current Status](https://secure.travis-ci.org/tschaub/mock-fs.png?branch=master)](https://travis-ci.org/tschaub/mock-fs)