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

Revert check for fs.realpath.native (#887) #953

Merged
merged 6 commits into from Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 57 additions & 1 deletion lib/fs/__tests__/realpath.test.js
@@ -1,11 +1,67 @@
'use strict'

const fse = require('../..')
const fs = require('fs')
const path = require('path')
const assert = require('assert')

/* eslint-env mocha */

describe('realpath.native does not exist', () => {
let warning
const warningListener = error => {
if (error.name === 'Warning') {
if (error.code.startsWith('fs-extra-WARN0003')) {
warning = error
}
}
}

const realpathNativeBackup = fs.realpath.native
const clearFseCache = () => {
const fsePath = path.dirname(require.resolve('../../..'))
for (const entry in require.cache) {
if (entry.startsWith(fsePath)) {
delete require.cache[entry]
}
}
}

before(() => {
process.on('warning', warningListener)

// clear existing require.cache
clearFseCache()

// simulate fs monkey-patch
delete fs.realpath.native
})

after(() => {
process.off('warning', warningListener)

// clear stubbed require.cache
clearFseCache()

// reinstate fs.realpath.native
fs.realpath.native = realpathNativeBackup
})

it('fse should not export realpath.native', done => {
const fse = require('../..')

// next event loop to allow event emitter/listener to happen
setImmediate(() => {
assert(warning, 'fs-extra-WARN0003 should be emitted')
lamweili marked this conversation as resolved.
Show resolved Hide resolved
done()
})

assert(!fse.realpath.native)
})
})

describe('realpath.native', () => {
const fse = require('../..')

it('works with callbacks', () => {
fse.realpath.native(__dirname, (err, path) => {
assert.ifError(err)
Expand Down
11 changes: 10 additions & 1 deletion lib/fs/index.js
Expand Up @@ -54,7 +54,6 @@ Object.assign(exports, fs)
api.forEach(method => {
exports[method] = u(fs[method])
})
exports.realpath.native = u(fs.realpath.native)

// We differ from mz/fs in that we still ship the old, broken, fs.exists()
// since we are a drop-in replacement for the native module
Expand Down Expand Up @@ -117,3 +116,13 @@ if (typeof fs.writev === 'function') {
})
}
}

// fs.realpath.native sometimes not available if fs is monkey-patched
if (typeof fs.realpath.native === 'function') {
exports.realpath.native = u(fs.realpath.native)
} else {
process.emitWarning(
'fs.realpath.native is not a function. Is fs being monkey-patched?',
'Warning', 'fs-extra-WARN0003'
)
}