• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Refs: https://github.com/nodejs/node/issues/4778
4
5const common = require('../common');
6const assert = require('assert');
7const fs = require('fs');
8const path = require('path');
9const Module = require('module');
10const tmpdir = require('../common/tmpdir');
11const file = path.join(tmpdir.path, 'test-extensions.foo.bar');
12const dotfile = path.join(tmpdir.path, '.bar');
13const dotfileWithExtension = path.join(tmpdir.path, '.foo.bar');
14
15tmpdir.refresh();
16fs.writeFileSync(file, 'console.log(__filename);', 'utf8');
17fs.writeFileSync(dotfile, 'console.log(__filename);', 'utf8');
18fs.writeFileSync(dotfileWithExtension, 'console.log(__filename);', 'utf8');
19
20{
21  require.extensions['.bar'] = common.mustNotCall();
22  require.extensions['.foo.bar'] = common.mustCall();
23  const modulePath = path.join(tmpdir.path, 'test-extensions');
24  require(modulePath);
25  require(file);
26  delete require.cache[file];
27  delete require.extensions['.bar'];
28  delete require.extensions['.foo.bar'];
29  Module._pathCache = Object.create(null);
30}
31
32{
33  require.extensions['.foo.bar'] = common.mustCall();
34  const modulePath = path.join(tmpdir.path, 'test-extensions');
35  require(modulePath);
36  assert.throws(
37    () => require(`${modulePath}.foo`),
38    (err) => err.message.startsWith(`Cannot find module '${modulePath}.foo'`)
39  );
40  require(`${modulePath}.foo.bar`);
41  delete require.cache[file];
42  delete require.extensions['.foo.bar'];
43  Module._pathCache = Object.create(null);
44}
45
46{
47  const modulePath = path.join(tmpdir.path, 'test-extensions');
48  assert.throws(
49    () => require(modulePath),
50    (err) => err.message.startsWith(`Cannot find module '${modulePath}'`)
51  );
52  delete require.cache[file];
53  Module._pathCache = Object.create(null);
54}
55
56{
57  require.extensions['.bar'] = common.mustNotCall();
58  require.extensions['.foo.bar'] = common.mustCall();
59  const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
60  require(modulePath);
61  delete require.cache[file];
62  delete require.extensions['.bar'];
63  delete require.extensions['.foo.bar'];
64  Module._pathCache = Object.create(null);
65}
66
67{
68  require.extensions['.foo.bar'] = common.mustNotCall();
69  const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
70  assert.throws(
71    () => require(modulePath),
72    (err) => err.message.startsWith(`Cannot find module '${modulePath}'`)
73  );
74  delete require.extensions['.foo.bar'];
75  Module._pathCache = Object.create(null);
76}
77
78{
79  require.extensions['.bar'] = common.mustNotCall();
80  require(dotfile);
81  delete require.cache[dotfile];
82  delete require.extensions['.bar'];
83  Module._pathCache = Object.create(null);
84}
85
86{
87  require.extensions['.bar'] = common.mustCall();
88  require.extensions['.foo.bar'] = common.mustNotCall();
89  require(dotfileWithExtension);
90  delete require.cache[dotfileWithExtension];
91  delete require.extensions['.bar'];
92  delete require.extensions['.foo.bar'];
93  Module._pathCache = Object.create(null);
94}
95