summaryrefslogtreecommitdiff
path: root/test/parallel/test-module-multi-extensions.js
blob: 1257854115a5b5e0ea63436ef4ca87bd986157c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';

// Refs: https://github.com/nodejs/node/issues/4778

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const Module = require('module');
const tmpdir = require('../common/tmpdir');
const file = path.join(tmpdir.path, 'test-extensions.foo.bar');
const dotfile = path.join(tmpdir.path, '.bar');
const dotfileWithExtension = path.join(tmpdir.path, '.foo.bar');

tmpdir.refresh();
fs.writeFileSync(file, 'console.log(__filename);', 'utf8');
fs.writeFileSync(dotfile, 'console.log(__filename);', 'utf8');
fs.writeFileSync(dotfileWithExtension, 'console.log(__filename);', 'utf8');

{
  require.extensions['.bar'] = common.mustNotCall();
  require.extensions['.foo.bar'] = common.mustCall();
  const modulePath = path.join(tmpdir.path, 'test-extensions');
  require(modulePath);
  require(file);
  delete require.cache[file];
  delete require.extensions['.bar'];
  delete require.extensions['.foo.bar'];
  Module._pathCache = Object.create(null);
}

{
  require.extensions['.foo.bar'] = common.mustCall();
  const modulePath = path.join(tmpdir.path, 'test-extensions');
  require(modulePath);
  assert.throws(
    () => require(`${modulePath}.foo`),
    (err) => err.message.startsWith(`Cannot find module '${modulePath}.foo'`)
  );
  require(`${modulePath}.foo.bar`);
  delete require.cache[file];
  delete require.extensions['.foo.bar'];
  Module._pathCache = Object.create(null);
}

{
  const modulePath = path.join(tmpdir.path, 'test-extensions');
  assert.throws(
    () => require(modulePath),
    (err) => err.message.startsWith(`Cannot find module '${modulePath}'`)
  );
  delete require.cache[file];
  Module._pathCache = Object.create(null);
}

{
  require.extensions['.bar'] = common.mustNotCall();
  require.extensions['.foo.bar'] = common.mustCall();
  const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
  require(modulePath);
  delete require.cache[file];
  delete require.extensions['.bar'];
  delete require.extensions['.foo.bar'];
  Module._pathCache = Object.create(null);
}

{
  require.extensions['.foo.bar'] = common.mustNotCall();
  const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
  assert.throws(
    () => require(modulePath),
    (err) => err.message.startsWith(`Cannot find module '${modulePath}'`)
  );
  delete require.extensions['.foo.bar'];
  Module._pathCache = Object.create(null);
}

{
  require.extensions['.bar'] = common.mustNotCall();
  require(dotfile);
  delete require.cache[dotfile];
  delete require.extensions['.bar'];
  Module._pathCache = Object.create(null);
}

{
  require.extensions['.bar'] = common.mustCall();
  require.extensions['.foo.bar'] = common.mustNotCall();
  require(dotfileWithExtension);
  delete require.cache[dotfileWithExtension];
  delete require.extensions['.bar'];
  delete require.extensions['.foo.bar'];
  Module._pathCache = Object.create(null);
}