From 25eb05a97aba7c6c0012e602b38939121848485e Mon Sep 17 00:00:00 2001 From: Alex Pry Date: Sun, 26 May 2019 17:38:13 +0300 Subject: [PATCH] lib: removed unnecessary fs.realpath `options` arg check + tests Removed duplicated check for options argument of fs.realpath. Added some tests which covering the cases for passing options arg as null. PR-URL: https://github.com/nodejs/node/pull/27909 Reviewed-By: Ujjwal Sharma Reviewed-By: Ruben Bridgewater Reviewed-By: Trivikram Kamat Reviewed-By: Rich Trott --- lib/fs.js | 6 +-- test/parallel/test-fs-realpath.js | 62 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 89f234b25d1888..e890d0c1305b95 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1533,11 +1533,9 @@ realpathSync.native = (path, options) => { function realpath(p, options, callback) { callback = typeof options === 'function' ? options : maybeCallback(callback); - if (!options) - options = emptyObj; - else - options = getOptions(options, emptyObj); + options = getOptions(options, {}); p = toPathIfFileURL(p); + if (typeof p !== 'string') { p += ''; } diff --git a/test/parallel/test-fs-realpath.js b/test/parallel/test-fs-realpath.js index d12c8a28d2a69b..8b3bb689675dc3 100644 --- a/test/parallel/test-fs-realpath.js +++ b/test/parallel/test-fs-realpath.js @@ -89,6 +89,14 @@ function test_simple_error_callback(realpath, realpathSync, cb) { })); } +function test_simple_error_cb_with_null_options(realpath, realpathSync, cb) { + realpath('/this/path/does/not/exist', null, common.mustCall(function(err, s) { + assert(err); + assert(!s); + cb(); + })); +} + function test_simple_relative_symlink(realpath, realpathSync, callback) { console.log('test_simple_relative_symlink'); if (skipSymlinks) { @@ -395,6 +403,7 @@ function test_up_multiple(realpath, realpathSync, cb) { assertEqualPath(realpathSync(abedabeda), abedabeda_real); assertEqualPath(realpathSync(abedabed), abedabed_real); + realpath(abedabeda, function(er, real) { assert.ifError(er); assertEqualPath(abedabeda_real, real); @@ -407,6 +416,48 @@ function test_up_multiple(realpath, realpathSync, cb) { } +// Going up with .. multiple times with options = null +// . +// `-- a/ +// |-- b/ +// | `-- e -> .. +// `-- d -> .. +// realpath(a/b/e/d/a/b/e/d/a) ==> a +function test_up_multiple_with_null_options(realpath, realpathSync, cb) { + console.error('test_up_multiple'); + if (skipSymlinks) { + common.printSkipMessage('symlink test (no privs)'); + return cb(); + } + const tmpdir = require('../common/tmpdir'); + tmpdir.refresh(); + fs.mkdirSync(tmp('a'), 0o755); + fs.mkdirSync(tmp('a/b'), 0o755); + fs.symlinkSync('..', tmp('a/d'), 'dir'); + unlink.push(tmp('a/d')); + fs.symlinkSync('..', tmp('a/b/e'), 'dir'); + unlink.push(tmp('a/b/e')); + + const abedabed = tmp('abedabed'.split('').join('/')); + const abedabed_real = tmp(''); + + const abedabeda = tmp('abedabeda'.split('').join('/')); + const abedabeda_real = tmp('a'); + + assertEqualPath(realpathSync(abedabeda), abedabeda_real); + assertEqualPath(realpathSync(abedabed), abedabed_real); + + realpath(abedabeda, null, function(er, real) { + assert.ifError(er); + assertEqualPath(abedabeda_real, real); + realpath(abedabed, null, function(er, real) { + assert.ifError(er); + assertEqualPath(abedabed_real, real); + cb(); + }); + }); +} + // Absolute symlinks with children. // . // `-- a/ @@ -474,10 +525,19 @@ function test_root(realpath, realpathSync, cb) { }); } +function test_root_with_null_options(realpath, realpathSync, cb) { + realpath('/', null, function(err, result) { + assert.ifError(err); + assertEqualPath(root, result); + cb(); + }); +} + // ---------------------------------------------------------------------------- const tests = [ test_simple_error_callback, + test_simple_error_cb_with_null_options, test_simple_relative_symlink, test_simple_absolute_symlink, test_deep_relative_file_symlink, @@ -491,7 +551,9 @@ const tests = [ test_upone_actual, test_abs_with_kids, test_up_multiple, + test_up_multiple_with_null_options, test_root, + test_root_with_null_options ]; const numtests = tests.length; let testsRun = 0;