|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const fse = require('../..') |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
4 | 5 | const assert = require('assert')
|
5 | 6 |
|
6 | 7 | /* eslint-env mocha */
|
7 | 8 |
|
| 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 | + |
8 | 62 | describe('realpath.native', () => {
|
| 63 | + const fse = require('../..') |
| 64 | + |
9 | 65 | it('works with callbacks', () => {
|
10 | 66 | fse.realpath.native(__dirname, (err, path) => {
|
11 | 67 | assert.ifError(err)
|
|
0 commit comments