From fa661f366407c1115e123b29c9ae21f3383e6a50 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman <17342435+RyanZim@users.noreply.github.com> Date: Tue, 14 May 2019 13:12:27 -0400 Subject: [PATCH] Add support for fs.realpath.native in envs that support it (#682) * Add support for fs.realpath.native in envs that support it * Add test for realpathSync.native * assert.equal -> assert.strictEqual --- lib/fs/__tests__/realpath.test.js | 33 +++++++++++++++++++++++++++++++ lib/fs/index.js | 5 +++++ 2 files changed, 38 insertions(+) create mode 100644 lib/fs/__tests__/realpath.test.js diff --git a/lib/fs/__tests__/realpath.test.js b/lib/fs/__tests__/realpath.test.js new file mode 100644 index 00000000..59ee6fa3 --- /dev/null +++ b/lib/fs/__tests__/realpath.test.js @@ -0,0 +1,33 @@ +'use strict' + +const fs = require('fs') +const fse = require('../..') +const assert = require('assert') + +/* eslint-env mocha */ + +// fs.realpath.native only available in Node v9.2+ +if (typeof fs.realpath.native === 'function') { + describe('realpath.native', () => { + it('works with callbacks', () => { + fse.realpath.native(__dirname, (err, path) => { + assert.ifError(err) + assert.strictEqual(path, __dirname) + }) + }) + + it('works with promises', (done) => { + fse.realpath.native(__dirname) + .then(path => { + assert.strictEqual(path, __dirname) + done() + }) + .catch(done) + }) + + it('works with sync version', () => { + const path = fse.realpathSync.native(__dirname) + assert.strictEqual(path, __dirname) + }) + }) +} diff --git a/lib/fs/index.js b/lib/fs/index.js index 8ffdf7b0..a7b22922 100644 --- a/lib/fs/index.js +++ b/lib/fs/index.js @@ -102,3 +102,8 @@ exports.write = function (fd, buffer, ...args) { }) }) } + +// fs.realpath.native only available in Node v9.2+ +if (typeof fs.realpath.native === 'function') { + exports.realpath.native = u(fs.realpath.native) +}