11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ciconst common = require('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst { execSync } = require('child_process');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst errorMessagesByPlatform = {
281cb0ef41Sopenharmony_ci  win32: ['is not a valid Win32 application'],
291cb0ef41Sopenharmony_ci  linux: ['file too short', 'Exec format error'],
301cb0ef41Sopenharmony_ci  sunos: ['unknown file type', 'not an ELF file'],
311cb0ef41Sopenharmony_ci  darwin: ['file too short', 'not a mach-o file'],
321cb0ef41Sopenharmony_ci  aix: ['Cannot load module',
331cb0ef41Sopenharmony_ci        'Cannot run a file that does not have a valid format.',
341cb0ef41Sopenharmony_ci        'Exec format error'],
351cb0ef41Sopenharmony_ci  ibmi: ['Cannot load module',
361cb0ef41Sopenharmony_ci         'The module has too many section headers',
371cb0ef41Sopenharmony_ci         'or the file has been truncated.'],
381cb0ef41Sopenharmony_ci};
391cb0ef41Sopenharmony_ci// If we don't know a priori what the error would be, we accept anything.
401cb0ef41Sopenharmony_ciconst platform = common.isIBMi ? 'ibmi' : process.platform;
411cb0ef41Sopenharmony_ciconst errorMessages = errorMessagesByPlatform[platform] || [''];
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// On Windows, error messages are MUI dependent
441cb0ef41Sopenharmony_ci// Ref: https://github.com/nodejs/node/issues/13376
451cb0ef41Sopenharmony_cilet localeOk = true;
461cb0ef41Sopenharmony_ciif (common.isWindows) {
471cb0ef41Sopenharmony_ci  const powerShellFindMUI =
481cb0ef41Sopenharmony_ci    'powershell -NoProfile -ExecutionPolicy Unrestricted -c ' +
491cb0ef41Sopenharmony_ci    '"(Get-UICulture).TwoLetterISOLanguageName"';
501cb0ef41Sopenharmony_ci  try {
511cb0ef41Sopenharmony_ci    // If MUI != 'en' we'll ignore the content of the message
521cb0ef41Sopenharmony_ci    localeOk = execSync(powerShellFindMUI).toString('utf8').trim() === 'en';
531cb0ef41Sopenharmony_ci  } catch {
541cb0ef41Sopenharmony_ci    // It's only a best effort try to find the MUI
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciassert.throws(
591cb0ef41Sopenharmony_ci  () => { require('../fixtures/module-loading-error.node'); },
601cb0ef41Sopenharmony_ci  (e) => {
611cb0ef41Sopenharmony_ci    if (localeOk && !errorMessages.some((msg) => e.message.includes(msg)))
621cb0ef41Sopenharmony_ci      return false;
631cb0ef41Sopenharmony_ci    return e.name === 'Error';
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciconst re = /^The "id" argument must be of type string\. Received /;
681cb0ef41Sopenharmony_ci[1, false, null, undefined, {}].forEach((value) => {
691cb0ef41Sopenharmony_ci  assert.throws(
701cb0ef41Sopenharmony_ci    () => { require(value); },
711cb0ef41Sopenharmony_ci    {
721cb0ef41Sopenharmony_ci      name: 'TypeError',
731cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
741cb0ef41Sopenharmony_ci      message: re
751cb0ef41Sopenharmony_ci    });
761cb0ef41Sopenharmony_ci});
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciassert.throws(
801cb0ef41Sopenharmony_ci  () => { require(''); },
811cb0ef41Sopenharmony_ci  {
821cb0ef41Sopenharmony_ci    name: 'TypeError',
831cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
841cb0ef41Sopenharmony_ci    message: 'The argument \'id\' must be a non-empty string. Received \'\''
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciassert.throws(
881cb0ef41Sopenharmony_ci  () => { require('../fixtures/packages/is-dir'); },
891cb0ef41Sopenharmony_ci  {
901cb0ef41Sopenharmony_ci    code: 'MODULE_NOT_FOUND',
911cb0ef41Sopenharmony_ci    message: /Cannot find module '\.\.\/fixtures\/packages\/is-dir'/
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci);
94