11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/4778 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst common = require('../common'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst fs = require('fs'); 81cb0ef41Sopenharmony_ciconst path = require('path'); 91cb0ef41Sopenharmony_ciconst Module = require('module'); 101cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 111cb0ef41Sopenharmony_ciconst file = path.join(tmpdir.path, 'test-extensions.foo.bar'); 121cb0ef41Sopenharmony_ciconst dotfile = path.join(tmpdir.path, '.bar'); 131cb0ef41Sopenharmony_ciconst dotfileWithExtension = path.join(tmpdir.path, '.foo.bar'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_citmpdir.refresh(); 161cb0ef41Sopenharmony_cifs.writeFileSync(file, 'console.log(__filename);', 'utf8'); 171cb0ef41Sopenharmony_cifs.writeFileSync(dotfile, 'console.log(__filename);', 'utf8'); 181cb0ef41Sopenharmony_cifs.writeFileSync(dotfileWithExtension, 'console.log(__filename);', 'utf8'); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci{ 211cb0ef41Sopenharmony_ci require.extensions['.bar'] = common.mustNotCall(); 221cb0ef41Sopenharmony_ci require.extensions['.foo.bar'] = common.mustCall(); 231cb0ef41Sopenharmony_ci const modulePath = path.join(tmpdir.path, 'test-extensions'); 241cb0ef41Sopenharmony_ci require(modulePath); 251cb0ef41Sopenharmony_ci require(file); 261cb0ef41Sopenharmony_ci delete require.cache[file]; 271cb0ef41Sopenharmony_ci delete require.extensions['.bar']; 281cb0ef41Sopenharmony_ci delete require.extensions['.foo.bar']; 291cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci{ 331cb0ef41Sopenharmony_ci require.extensions['.foo.bar'] = common.mustCall(); 341cb0ef41Sopenharmony_ci const modulePath = path.join(tmpdir.path, 'test-extensions'); 351cb0ef41Sopenharmony_ci require(modulePath); 361cb0ef41Sopenharmony_ci assert.throws( 371cb0ef41Sopenharmony_ci () => require(`${modulePath}.foo`), 381cb0ef41Sopenharmony_ci (err) => err.message.startsWith(`Cannot find module '${modulePath}.foo'`) 391cb0ef41Sopenharmony_ci ); 401cb0ef41Sopenharmony_ci require(`${modulePath}.foo.bar`); 411cb0ef41Sopenharmony_ci delete require.cache[file]; 421cb0ef41Sopenharmony_ci delete require.extensions['.foo.bar']; 431cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci{ 471cb0ef41Sopenharmony_ci const modulePath = path.join(tmpdir.path, 'test-extensions'); 481cb0ef41Sopenharmony_ci assert.throws( 491cb0ef41Sopenharmony_ci () => require(modulePath), 501cb0ef41Sopenharmony_ci (err) => err.message.startsWith(`Cannot find module '${modulePath}'`) 511cb0ef41Sopenharmony_ci ); 521cb0ef41Sopenharmony_ci delete require.cache[file]; 531cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci{ 571cb0ef41Sopenharmony_ci require.extensions['.bar'] = common.mustNotCall(); 581cb0ef41Sopenharmony_ci require.extensions['.foo.bar'] = common.mustCall(); 591cb0ef41Sopenharmony_ci const modulePath = path.join(tmpdir.path, 'test-extensions.foo'); 601cb0ef41Sopenharmony_ci require(modulePath); 611cb0ef41Sopenharmony_ci delete require.cache[file]; 621cb0ef41Sopenharmony_ci delete require.extensions['.bar']; 631cb0ef41Sopenharmony_ci delete require.extensions['.foo.bar']; 641cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci{ 681cb0ef41Sopenharmony_ci require.extensions['.foo.bar'] = common.mustNotCall(); 691cb0ef41Sopenharmony_ci const modulePath = path.join(tmpdir.path, 'test-extensions.foo'); 701cb0ef41Sopenharmony_ci assert.throws( 711cb0ef41Sopenharmony_ci () => require(modulePath), 721cb0ef41Sopenharmony_ci (err) => err.message.startsWith(`Cannot find module '${modulePath}'`) 731cb0ef41Sopenharmony_ci ); 741cb0ef41Sopenharmony_ci delete require.extensions['.foo.bar']; 751cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci{ 791cb0ef41Sopenharmony_ci require.extensions['.bar'] = common.mustNotCall(); 801cb0ef41Sopenharmony_ci require(dotfile); 811cb0ef41Sopenharmony_ci delete require.cache[dotfile]; 821cb0ef41Sopenharmony_ci delete require.extensions['.bar']; 831cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci{ 871cb0ef41Sopenharmony_ci require.extensions['.bar'] = common.mustCall(); 881cb0ef41Sopenharmony_ci require.extensions['.foo.bar'] = common.mustNotCall(); 891cb0ef41Sopenharmony_ci require(dotfileWithExtension); 901cb0ef41Sopenharmony_ci delete require.cache[dotfileWithExtension]; 911cb0ef41Sopenharmony_ci delete require.extensions['.bar']; 921cb0ef41Sopenharmony_ci delete require.extensions['.foo.bar']; 931cb0ef41Sopenharmony_ci Module._pathCache = Object.create(null); 941cb0ef41Sopenharmony_ci} 95