Skip to content

Commit 7bb0120

Browse files
lamweiliRyanZim
andauthored
Check and warn for the absence of fs.realpath.native (#953)
* Revert "Remove check for fs.realpath.native support, since it's everywhere (#887)" This reverts commit f4a880d. * Added warning and fallback support for missing fs.realpath.native Signed-off-by: Lam Wei Li <[email protected]> * Removed NodeJS check and fs.realpath.native fallback Signed-off-by: Lam Wei Li <[email protected]> * Leaving original listener intact during test and better phasing for warning Signed-off-by: Lam Wei Li <[email protected]> * Using done() for proper async test Signed-off-by: Lam Wei Li <[email protected]> * Updated comments for clarity Co-authored-by: Ryan Zimmerman <[email protected]> Co-authored-by: Ryan Zimmerman <[email protected]>
1 parent e0d298d commit 7bb0120

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/fs/__tests__/realpath.test.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
'use strict'
22

3-
const fse = require('../..')
3+
const fs = require('fs')
4+
const path = require('path')
45
const assert = require('assert')
56

67
/* eslint-env mocha */
78

9+
describe('realpath.native does not exist', () => {
10+
let warning
11+
const warningListener = error => {
12+
if (error.name === 'Warning') {
13+
if (error.code.startsWith('fs-extra-WARN0003')) {
14+
warning = error
15+
}
16+
}
17+
}
18+
19+
const realpathNativeBackup = fs.realpath.native
20+
const clearFseCache = () => {
21+
const fsePath = path.dirname(require.resolve('../../..'))
22+
for (const entry in require.cache) {
23+
if (entry.startsWith(fsePath)) {
24+
delete require.cache[entry]
25+
}
26+
}
27+
}
28+
29+
before(() => {
30+
process.on('warning', warningListener)
31+
32+
// clear existing require.cache
33+
clearFseCache()
34+
35+
// simulate fs monkey-patch
36+
delete fs.realpath.native
37+
})
38+
39+
after(() => {
40+
process.off('warning', warningListener)
41+
42+
// clear stubbed require.cache
43+
clearFseCache()
44+
45+
// reinstate fs.realpath.native
46+
fs.realpath.native = realpathNativeBackup
47+
})
48+
49+
it('fse should not export realpath.native', done => {
50+
const fse = require('../..')
51+
52+
// next event loop to allow event emitter/listener to happen
53+
setImmediate(() => {
54+
assert(warning, 'fs-extra-WARN0003 should be emitted')
55+
done()
56+
})
57+
58+
assert(!fse.realpath.native)
59+
})
60+
})
61+
862
describe('realpath.native', () => {
63+
const fse = require('../..')
64+
965
it('works with callbacks', () => {
1066
fse.realpath.native(__dirname, (err, path) => {
1167
assert.ifError(err)

lib/fs/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Object.assign(exports, fs)
5454
api.forEach(method => {
5555
exports[method] = u(fs[method])
5656
})
57-
exports.realpath.native = u(fs.realpath.native)
5857

5958
// We differ from mz/fs in that we still ship the old, broken, fs.exists()
6059
// since we are a drop-in replacement for the native module
@@ -117,3 +116,13 @@ if (typeof fs.writev === 'function') {
117116
})
118117
}
119118
}
119+
120+
// fs.realpath.native sometimes not available if fs is monkey-patched
121+
if (typeof fs.realpath.native === 'function') {
122+
exports.realpath.native = u(fs.realpath.native)
123+
} else {
124+
process.emitWarning(
125+
'fs.realpath.native is not a function. Is fs being monkey-patched?',
126+
'Warning', 'fs-extra-WARN0003'
127+
)
128+
}

0 commit comments

Comments
 (0)