Skip to content

Commit

Permalink
Merge pull request #281 from 3cp/fix-missing-promise-return
Browse files Browse the repository at this point in the history
fix: return maybeCallback result to support promise return
  • Loading branch information
tschaub authored Nov 26, 2019
2 parents 98ba9b6 + 994a529 commit 19334ee
Show file tree
Hide file tree
Showing 31 changed files with 5,638 additions and 3,350 deletions.
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 @@ -321,3 +327,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

0 comments on commit 19334ee

Please sign in to comment.