|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Refs: https://github.com/nodejs/node/issues/4778 |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +const assert = require('assert'); |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const tmpdir = require('../common/tmpdir'); |
| 10 | +const file = path.join(tmpdir.path, 'test-extensions.foo.bar'); |
| 11 | + |
| 12 | +tmpdir.refresh(); |
| 13 | +fs.writeFileSync(file, '', 'utf8'); |
| 14 | + |
| 15 | +{ |
| 16 | + require.extensions['.bar'] = common.mustNotCall(); |
| 17 | + require.extensions['.foo.bar'] = common.mustNotCall(); |
| 18 | + const modulePath = path.join(tmpdir.path, 'test-extensions'); |
| 19 | + assert.throws( |
| 20 | + () => require(modulePath), |
| 21 | + new Error(`Cannot find module '${modulePath}'`) |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +{ |
| 26 | + delete require.extensions['.bar']; |
| 27 | + require.extensions['.foo.bar'] = common.mustNotCall(); |
| 28 | + const modulePath = path.join(tmpdir.path, 'test-extensions'); |
| 29 | + assert.throws( |
| 30 | + () => require(modulePath), |
| 31 | + new Error(`Cannot find module '${modulePath}'`) |
| 32 | + ); |
| 33 | + assert.throws( |
| 34 | + () => require(modulePath + '.foo'), |
| 35 | + new Error(`Cannot find module '${modulePath}.foo'`) |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +{ |
| 40 | + delete require.extensions['.bar']; |
| 41 | + delete require.extensions['.foo.bar']; |
| 42 | + const modulePath = path.join(tmpdir.path, 'test-extensions'); |
| 43 | + assert.throws( |
| 44 | + () => require(modulePath), |
| 45 | + new Error(`Cannot find module '${modulePath}'`) |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + delete require.extensions['.foo.bar']; |
| 51 | + require.extensions['.bar'] = common.mustCall((module, path) => { |
| 52 | + assert.strictEqual(module.id, file); |
| 53 | + assert.strictEqual(path, file); |
| 54 | + }); |
| 55 | + |
| 56 | + const modulePath = path.join(tmpdir.path, 'test-extensions.foo'); |
| 57 | + require(modulePath); |
| 58 | +} |
0 commit comments