• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const { execSync } = require('child_process');
26
27const errorMessagesByPlatform = {
28  win32: ['is not a valid Win32 application'],
29  linux: ['file too short', 'Exec format error'],
30  sunos: ['unknown file type', 'not an ELF file'],
31  darwin: ['file too short', 'not a mach-o file'],
32  aix: ['Cannot load module',
33        'Cannot run a file that does not have a valid format.',
34        'Exec format error'],
35  ibmi: ['Cannot load module',
36         'The module has too many section headers',
37         'or the file has been truncated.'],
38};
39// If we don't know a priori what the error would be, we accept anything.
40const platform = common.isIBMi ? 'ibmi' : process.platform;
41const errorMessages = errorMessagesByPlatform[platform] || [''];
42
43// On Windows, error messages are MUI dependent
44// Ref: https://github.com/nodejs/node/issues/13376
45let localeOk = true;
46if (common.isWindows) {
47  const powerShellFindMUI =
48    'powershell -NoProfile -ExecutionPolicy Unrestricted -c ' +
49    '"(Get-UICulture).TwoLetterISOLanguageName"';
50  try {
51    // If MUI != 'en' we'll ignore the content of the message
52    localeOk = execSync(powerShellFindMUI).toString('utf8').trim() === 'en';
53  } catch {
54    // It's only a best effort try to find the MUI
55  }
56}
57
58assert.throws(
59  () => { require('../fixtures/module-loading-error.node'); },
60  (e) => {
61    if (localeOk && !errorMessages.some((msg) => e.message.includes(msg)))
62      return false;
63    return e.name === 'Error';
64  }
65);
66
67const re = /^The "id" argument must be of type string\. Received /;
68[1, false, null, undefined, {}].forEach((value) => {
69  assert.throws(
70    () => { require(value); },
71    {
72      name: 'TypeError',
73      code: 'ERR_INVALID_ARG_TYPE',
74      message: re
75    });
76});
77
78
79assert.throws(
80  () => { require(''); },
81  {
82    name: 'TypeError',
83    code: 'ERR_INVALID_ARG_VALUE',
84    message: 'The argument \'id\' must be a non-empty string. Received \'\''
85  });
86
87assert.throws(
88  () => { require('../fixtures/packages/is-dir'); },
89  {
90    code: 'MODULE_NOT_FOUND',
91    message: /Cannot find module '\.\.\/fixtures\/packages\/is-dir'/
92  }
93);
94