Skip to content

Commit 25eb05a

Browse files
opteriontargos
authored andcommitted
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: #27909 Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 6ca4f03 commit 25eb05a

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

lib/fs.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1533,11 +1533,9 @@ realpathSync.native = (path, options) => {
15331533

15341534
function realpath(p, options, callback) {
15351535
callback = typeof options === 'function' ? options : maybeCallback(callback);
1536-
if (!options)
1537-
options = emptyObj;
1538-
else
1539-
options = getOptions(options, emptyObj);
1536+
options = getOptions(options, {});
15401537
p = toPathIfFileURL(p);
1538+
15411539
if (typeof p !== 'string') {
15421540
p += '';
15431541
}

test/parallel/test-fs-realpath.js

+62
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ function test_simple_error_callback(realpath, realpathSync, cb) {
8989
}));
9090
}
9191

92+
function test_simple_error_cb_with_null_options(realpath, realpathSync, cb) {
93+
realpath('/this/path/does/not/exist', null, common.mustCall(function(err, s) {
94+
assert(err);
95+
assert(!s);
96+
cb();
97+
}));
98+
}
99+
92100
function test_simple_relative_symlink(realpath, realpathSync, callback) {
93101
console.log('test_simple_relative_symlink');
94102
if (skipSymlinks) {
@@ -395,6 +403,7 @@ function test_up_multiple(realpath, realpathSync, cb) {
395403

396404
assertEqualPath(realpathSync(abedabeda), abedabeda_real);
397405
assertEqualPath(realpathSync(abedabed), abedabed_real);
406+
398407
realpath(abedabeda, function(er, real) {
399408
assert.ifError(er);
400409
assertEqualPath(abedabeda_real, real);
@@ -407,6 +416,48 @@ function test_up_multiple(realpath, realpathSync, cb) {
407416
}
408417

409418

419+
// Going up with .. multiple times with options = null
420+
// .
421+
// `-- a/
422+
// |-- b/
423+
// | `-- e -> ..
424+
// `-- d -> ..
425+
// realpath(a/b/e/d/a/b/e/d/a) ==> a
426+
function test_up_multiple_with_null_options(realpath, realpathSync, cb) {
427+
console.error('test_up_multiple');
428+
if (skipSymlinks) {
429+
common.printSkipMessage('symlink test (no privs)');
430+
return cb();
431+
}
432+
const tmpdir = require('../common/tmpdir');
433+
tmpdir.refresh();
434+
fs.mkdirSync(tmp('a'), 0o755);
435+
fs.mkdirSync(tmp('a/b'), 0o755);
436+
fs.symlinkSync('..', tmp('a/d'), 'dir');
437+
unlink.push(tmp('a/d'));
438+
fs.symlinkSync('..', tmp('a/b/e'), 'dir');
439+
unlink.push(tmp('a/b/e'));
440+
441+
const abedabed = tmp('abedabed'.split('').join('/'));
442+
const abedabed_real = tmp('');
443+
444+
const abedabeda = tmp('abedabeda'.split('').join('/'));
445+
const abedabeda_real = tmp('a');
446+
447+
assertEqualPath(realpathSync(abedabeda), abedabeda_real);
448+
assertEqualPath(realpathSync(abedabed), abedabed_real);
449+
450+
realpath(abedabeda, null, function(er, real) {
451+
assert.ifError(er);
452+
assertEqualPath(abedabeda_real, real);
453+
realpath(abedabed, null, function(er, real) {
454+
assert.ifError(er);
455+
assertEqualPath(abedabed_real, real);
456+
cb();
457+
});
458+
});
459+
}
460+
410461
// Absolute symlinks with children.
411462
// .
412463
// `-- a/
@@ -474,10 +525,19 @@ function test_root(realpath, realpathSync, cb) {
474525
});
475526
}
476527

528+
function test_root_with_null_options(realpath, realpathSync, cb) {
529+
realpath('/', null, function(err, result) {
530+
assert.ifError(err);
531+
assertEqualPath(root, result);
532+
cb();
533+
});
534+
}
535+
477536
// ----------------------------------------------------------------------------
478537

479538
const tests = [
480539
test_simple_error_callback,
540+
test_simple_error_cb_with_null_options,
481541
test_simple_relative_symlink,
482542
test_simple_absolute_symlink,
483543
test_deep_relative_file_symlink,
@@ -491,7 +551,9 @@ const tests = [
491551
test_upone_actual,
492552
test_abs_with_kids,
493553
test_up_multiple,
554+
test_up_multiple_with_null_options,
494555
test_root,
556+
test_root_with_null_options
495557
];
496558
const numtests = tests.length;
497559
let testsRun = 0;

0 commit comments

Comments
 (0)