Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return maybeCallback result to support promise return #281

Merged
merged 33 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
38c3192
fix: return maybeCallback result to support promise return
3cp Nov 5, 2019
e722509
test: add test coverage to fs.promises.access
3cp Nov 5, 2019
1e6146d
test: add test coverage to fs.promises.copyFile
3cp Nov 5, 2019
5f4c019
test: add test coverage to fs.promises.rename
3cp Nov 5, 2019
53966fb
chore: clean up test helper
3cp Nov 5, 2019
93155bd
test: add test coverage to fs.promises.stat
3cp Nov 5, 2019
8629479
test: add test coverage to fs.promises.readdir
3cp Nov 6, 2019
bbaec2e
test: add test coverage to fs.promises.open and fileHandler.close
3cp Nov 6, 2019
ae258b8
chore: clean up eslint warnings
3cp Nov 6, 2019
5af95dc
test: add test coverage to fileHandler.stat
3cp Nov 6, 2019
3607ed1
test: remove duplicated tests
3cp Nov 6, 2019
465b712
test: split tests to fils
3cp Nov 6, 2019
3aa50d8
test: add test coverage to filehandler.read
3cp Nov 6, 2019
d50d893
test: add test coverage to fs.promises.readFile
3cp Nov 6, 2019
54c86ae
fix: fix fshandle.write with string
3cp Nov 6, 2019
828f215
test: add test coverage to filehandler.write
3cp Nov 6, 2019
2c4e72d
test: add test coverage to fs.promises.writeFile
3cp Nov 6, 2019
ebc2128
test: add test coverage to fs.promises.appendFile
3cp Nov 6, 2019
d7c8690
test: add test coverage to fs.promises.mkdir
3cp Nov 6, 2019
8471c76
test: add test coverage to fs.promises.mkdtemp
3cp Nov 6, 2019
c06ec8a
test: add test coverage to fs.promises.rmdir
3cp Nov 6, 2019
6c0f57b
test: add test coverage to fs.promises.chown and filehandle.chown
3cp Nov 6, 2019
604d8cd
test: add test coverage to fs.promises.chmod and filehandle.chmod
3cp Nov 6, 2019
259ad0b
chore: cleanup
3cp Nov 6, 2019
3353d00
test: add test coverage to fs.promises.unlink
3cp Nov 6, 2019
bef86ed
test: add test coverage to fs.promises.utimes and filehandle.utimes
3cp Nov 6, 2019
8cf2098
test: add test coverage to fs.promises.link and fs.promises.symlink
3cp Nov 6, 2019
17a2e12
test: add test coverage to fs.promises.readlink
3cp Nov 6, 2019
4470222
test: add test coverage to fs.promises.lstat
3cp Nov 6, 2019
16b9026
test: add test coverage to fs.promises.realpath and fs.realpathSync
3cp Nov 6, 2019
5284ba0
chore: clean up
3cp Nov 7, 2019
e63c109
chore: use path.toNamespacedPath when available
3cp Nov 10, 2019
994a529
fix: remove win32 file namespace prefix for fs.realpath.native and fs…
3cp Nov 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,15 @@ Binding.prototype.realpath = function(filepath, encoding, callback, ctx) {
throw new FSError('ENOENT', filepath);
}

if (process.platform === 'win32' && realPath.startsWith('\\\\?\\')) {
// Remove win32 file namespace prefix \\?\
realPath = realPath.slice(4);
}

if (encoding === 'buffer') {
realPath = bufferFrom(realPath);
}

return realPath;
});
};
Expand Down Expand Up @@ -495,7 +501,7 @@ Binding.prototype.fstat = function(fd, options, callback, ctx) {
Binding.prototype.close = function(fd, callback, ctx) {
markSyscall(ctx, 'close');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
untrackDescriptorById(fd);
});
};
Expand Down Expand Up @@ -824,7 +830,7 @@ Binding.prototype.writeString = function(

const buffer = bufferFrom(string, encoding);
let wrapper;
if (callback) {
if (callback && callback !== kUsePromises) {
if (callback.oncomplete) {
callback = callback.oncomplete.bind(callback);
}
Expand Down Expand Up @@ -955,7 +961,7 @@ Binding.prototype.mkdir = function(pathname, mode, recursive, callback, ctx) {

markSyscall(ctx, 'mkdir');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (item) {
if (recursive && item instanceof Directory) {
Expand Down Expand Up @@ -995,7 +1001,7 @@ Binding.prototype.mkdir = function(pathname, mode, recursive, callback, ctx) {
Binding.prototype.rmdir = function(pathname, callback, ctx) {
markSyscall(ctx, 'rmdir');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (!item) {
throw new FSError('ENOENT', pathname);
Expand Down Expand Up @@ -1087,7 +1093,7 @@ Binding.prototype.mkdtemp = function(prefix, encoding, callback, ctx) {
Binding.prototype.ftruncate = function(fd, len, callback, ctx) {
markSyscall(ctx, 'ftruncate');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const descriptor = getDescriptorById(fd);
if (!descriptor.isWrite()) {
throw new FSError('EINVAL');
Expand Down Expand Up @@ -1123,7 +1129,7 @@ Binding.prototype.truncate = Binding.prototype.ftruncate;
Binding.prototype.chown = function(pathname, uid, gid, callback, ctx) {
markSyscall(ctx, 'chown');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (!item) {
throw new FSError('ENOENT', pathname);
Expand All @@ -1144,7 +1150,7 @@ Binding.prototype.chown = function(pathname, uid, gid, callback, ctx) {
Binding.prototype.fchown = function(fd, uid, gid, callback, ctx) {
markSyscall(ctx, 'fchown');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const descriptor = getDescriptorById(fd);
const item = descriptor.getItem();
item.setUid(uid);
Expand All @@ -1162,7 +1168,7 @@ Binding.prototype.fchown = function(fd, uid, gid, callback, ctx) {
Binding.prototype.chmod = function(pathname, mode, callback, ctx) {
markSyscall(ctx, 'chmod');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (!item) {
throw new FSError('ENOENT', pathname);
Expand All @@ -1181,7 +1187,7 @@ Binding.prototype.chmod = function(pathname, mode, callback, ctx) {
Binding.prototype.fchmod = function(fd, mode, callback, ctx) {
markSyscall(ctx, 'fchmod');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const descriptor = getDescriptorById(fd);
const item = descriptor.getItem();
item.setMode(mode);
Expand All @@ -1197,7 +1203,7 @@ Binding.prototype.fchmod = function(fd, mode, callback, ctx) {
Binding.prototype.unlink = function(pathname, callback, ctx) {
markSyscall(ctx, 'unlink');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (!item) {
throw new FSError('ENOENT', pathname);
Expand All @@ -1221,7 +1227,7 @@ Binding.prototype.unlink = function(pathname, callback, ctx) {
Binding.prototype.utimes = function(pathname, atime, mtime, callback, ctx) {
markSyscall(ctx, 'utimes');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(pathname);
if (!item) {
throw new FSError('ENOENT', pathname);
Expand All @@ -1242,7 +1248,7 @@ Binding.prototype.utimes = function(pathname, atime, mtime, callback, ctx) {
Binding.prototype.futimes = function(fd, atime, mtime, callback, ctx) {
markSyscall(ctx, 'futimes');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const descriptor = getDescriptorById(fd);
const item = descriptor.getItem();
item.setATime(new Date(atime * 1000));
Expand All @@ -1259,7 +1265,7 @@ Binding.prototype.futimes = function(fd, atime, mtime, callback, ctx) {
Binding.prototype.fsync = function(fd, callback, ctx) {
markSyscall(ctx, 'fsync');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
getDescriptorById(fd);
});
};
Expand All @@ -1273,7 +1279,7 @@ Binding.prototype.fsync = function(fd, callback, ctx) {
Binding.prototype.fdatasync = function(fd, callback, ctx) {
markSyscall(ctx, 'fdatasync');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
getDescriptorById(fd);
});
};
Expand All @@ -1288,7 +1294,7 @@ Binding.prototype.fdatasync = function(fd, callback, ctx) {
Binding.prototype.link = function(srcPath, destPath, callback, ctx) {
markSyscall(ctx, 'link');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
const item = _system.getItem(srcPath);
if (!item) {
throw new FSError('ENOENT', srcPath);
Expand Down Expand Up @@ -1321,7 +1327,7 @@ Binding.prototype.link = function(srcPath, destPath, callback, ctx) {
Binding.prototype.symlink = function(srcPath, destPath, type, callback, ctx) {
markSyscall(ctx, 'symlink');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
if (_system.getItem(destPath)) {
throw new FSError('EEXIST', destPath);
}
Expand Down Expand Up @@ -1420,7 +1426,7 @@ Binding.prototype.lstat = function(filepath, options, callback, ctx) {
Binding.prototype.access = function(filepath, mode, callback, ctx) {
markSyscall(ctx, 'access');

maybeCallback(normalizeCallback(callback), ctx, this, function() {
return maybeCallback(normalizeCallback(callback), ctx, this, function() {
let item = _system.getItem(filepath);
let links = 0;
while (item instanceof SymbolicLink) {
Expand Down
9 changes: 8 additions & 1 deletion lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ const SymbolicLink = require('./symlink');

const isWindows = process.platform === 'win32';

function toNamespacedPath(filePath) {
return path.toNamespacedPath
? path.toNamespacedPath(filePath)
: path._makeLong(filePath);
}

function getPathParts(filepath) {
const parts = path._makeLong(path.resolve(filepath)).split(path.sep);
const parts = toNamespacedPath(path.resolve(filepath)).split(path.sep);
parts.shift();
if (isWindows) {
// parts currently looks like ['', '?', 'c:', ...]
Expand Down Expand Up @@ -318,3 +324,4 @@ FileSystem.directory = function(config) {
*/
exports = module.exports = FileSystem;
exports.getPathParts = getPathParts;
exports.toNamespacedPath = toNamespacedPath;
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const realBinding = process.binding('fs');
const path = require('path');
const fs = require('fs');

const toNamespacedPath = FileSystem.toNamespacedPath;

const realProcessProps = {
cwd: process.cwd,
chdir: process.chdir
Expand Down Expand Up @@ -136,7 +138,7 @@ exports = module.exports = function mock(config, options) {
return currentPath;
},
function chdir(directory) {
if (!binding.stat(path._makeLong(directory)).isDirectory()) {
if (!binding.stat(toNamespacedPath(directory)).isDirectory()) {
throw new FSError('ENOTDIR');
}
currentPath = path.resolve(currentPath, directory);
Expand Down
19 changes: 17 additions & 2 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const chai = require('chai');
const constants = require('constants');
const semver = require('semver');
const fs = require('fs');
const hasPromise = !!fs.promises;

/** @type {boolean} */
chai.config.includeStack = true;
Expand All @@ -13,14 +15,19 @@ chai.config.includeStack = true;
*/
exports.assert = chai.assert;

const TEST = {it: it, xit: xit, describe: describe, xdescribe: xdescribe};
const NO_TEST = {it: xit, xit: xit, describe: xdescribe, xdescribe: xdescribe};

exports.inVersion = function(range) {
if (semver.satisfies(process.version, range)) {
return {it: it, describe: describe};
return TEST;
} else {
return {it: xit, describe: xdescribe};
return NO_TEST;
}
};

exports.withPromise = hasPromise ? TEST : NO_TEST;

/**
* Convert a string to flags for fs.open.
* @param {string} str String.
Expand Down Expand Up @@ -84,3 +91,11 @@ exports.flags = function(str) {
throw new Error('Unsupported flag: ' + str);
}
};

exports.assertEqualPaths = function(actual, expected) {
if (process.platform === 'win32') {
chai.assert.equal(actual.toLowerCase(), expected.toLowerCase());
} else {
chai.assert(actual, expected);
}
};
13 changes: 1 addition & 12 deletions test/lib/binding.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const bufferFrom = require('../../lib/buffer').from;
const bufferAlloc = require('../../lib/buffer').alloc;

const assert = helper.assert;
const assertEqualPaths = helper.assertEqualPaths;
const flags = helper.flags;

describe('Binding', function() {
Expand Down Expand Up @@ -238,18 +239,6 @@ describe('Binding', function() {
});

describe('#realpath()', function() {
function assertEqualPaths(actual, expected) {
if (path._makeLong(expected) === expected) {
// not on Windows
assert.equal(actual, expected);
} else {
assert.equal(
actual.toLowerCase(),
path._makeLong(expected).toLowerCase()
);
}
}

it('returns the real path for a regular file', function(done) {
const binding = new Binding(system);
binding.realpath('mock-dir/one.txt', 'utf-8', function(err, realPath) {
Expand Down
Loading