Skip to content

Commit 20c8a0a

Browse files
TrottBethGriggs
authored andcommitted
test: delay loading 'os' in test/common module
There is a test that doesn't load the common module initially because it needs to monkey-patch the 'os' module. I think it would be a good idea to minimize the side-effects of loading common anyway, so let's defer loading 'os' unless/until it's actually needed. PR-URL: #30914 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent b830f44 commit 20c8a0a

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

‎test/common/index.js‎

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323
/* eslint-disable node-core/crypto-check */
2424
'use strict';
2525
constprocess=global.process;// Some tests tamper with the process global.
26-
constpath=require('path');
27-
constfs=require('fs');
26+
2827
constassert=require('assert');
29-
constos=require('os');
3028
const{ exec, execSync, spawnSync }=require('child_process');
29+
constfs=require('fs');
30+
// Do not require 'os' until needed so that test-os-checked-fucnction can
31+
// monkey patch it. If 'os' is required here, that test will fail.
32+
constpath=require('path');
3133
constutil=require('util');
34+
const{ isMainThread }=require('worker_threads');
35+
3236
consttmpdir=require('./tmpdir');
3337
constbits=['arm64','mips','mipsel','ppc64','s390x','x64']
3438
.includes(process.arch) ? 64 : 32;
3539
consthasIntl=!!process.config.variables.v8_enable_i18n_support;
36-
const{ isMainThread }=require('worker_threads');
3740

3841
// Some tests assume a umask of 0o022 so set that up front. Tests that need a
3942
// different umask will set it themselves.
@@ -102,23 +105,12 @@ if (process.argv.length === 2 &&
102105

103106
constisWindows=process.platform==='win32';
104107
constisAIX=process.platform==='aix';
105-
// On IBMi, process.platform and os.platform() both return 'aix',
106-
// It is not enough to differentiate between IBMi and real AIX system.
107-
constisIBMi=os.type()==='OS400';
108-
constisLinuxPPCBE=(process.platform==='linux')&&
109-
(process.arch==='ppc64')&&
110-
(os.endianness()==='BE');
111108
constisSunOS=process.platform==='sunos';
112109
constisFreeBSD=process.platform==='freebsd';
113110
constisOpenBSD=process.platform==='openbsd';
114111
constisLinux=process.platform==='linux';
115112
constisOSX=process.platform==='darwin';
116113

117-
constenoughTestMem=os.totalmem()>0x70000000;/* 1.75 Gb */
118-
constcpus=os.cpus();
119-
constenoughTestCpu=Array.isArray(cpus)&&
120-
(cpus.length>1||cpus[0].speed>999);
121-
122114
constrootDir=isWindows ? 'c:\\' : '/';
123115

124116
constbuildType=process.config.target_defaults ?
@@ -198,15 +190,6 @@ const PIPE = (() =>{
198190
returnpath.join(pipePrefix,pipeName);
199191
})();
200192

201-
consthasIPv6=(()=>{
202-
constiFaces=os.networkInterfaces();
203-
constre=isWindows ? /LoopbackPseudo-Interface/ : /lo/;
204-
returnObject.keys(iFaces).some((name)=>{
205-
returnre.test(name)&&
206-
iFaces[name].some(({ family })=>family==='IPv6');
207-
});
208-
})();
209-
210193
/*
211194
* Check that when running a test with
212195
* `$node --abort-on-uncaught-exception $file child`
@@ -749,8 +732,6 @@ module.exports ={
749732
childShouldThrowAndAbort,
750733
createZeroFilledFile,
751734
disableCrashOnUnhandledRejection,
752-
enoughTestCpu,
753-
enoughTestMem,
754735
expectsError,
755736
expectsInternalAssertion,
756737
expectWarning,
@@ -760,14 +741,11 @@ module.exports ={
760741
getTTYfd,
761742
hasIntl,
762743
hasCrypto,
763-
hasIPv6,
764744
hasMultiLocalhost,
765745
isAIX,
766746
isAlive,
767747
isFreeBSD,
768-
isIBMi,
769748
isLinux,
770-
isLinuxPPCBE,
771749
isMainThread,
772750
isOpenBSD,
773751
isOSX,
@@ -791,12 +769,28 @@ module.exports ={
791769
skipIfReportDisabled,
792770
skipIfWorker,
793771

794-
getlocalhostIPv6(){return'::1';},
772+
getenoughTestCPU(){
773+
constcpus=require('os').cpus();
774+
returnArray.isArray(cpus)&&(cpus.length>1||cpus[0].speed>999);
775+
},
776+
777+
getenoughTestMeme(){
778+
returnrequire('os').totalmem()>0x70000000;/* 1.75 Gb */
779+
},
795780

796781
gethasFipsCrypto(){
797782
returnhasCrypto&&require('crypto').getFips();
798783
},
799784

785+
gethasIPv6(){
786+
constiFaces=require('os').networkInterfaces();
787+
constre=isWindows ? /LoopbackPseudo-Interface/ : /lo/;
788+
returnObject.keys(iFaces).some((name)=>{
789+
returnre.test(name)&&
790+
iFaces[name].some(({ family })=>family==='IPv6');
791+
});
792+
},
793+
800794
getinFreeBSDJail(){
801795
if(inFreeBSDJail!==null)returninFreeBSDJail;
802796

@@ -809,6 +803,17 @@ module.exports ={
809803
returninFreeBSDJail;
810804
},
811805

806+
// On IBMi, process.platform and os.platform() both return 'aix',
807+
// It is not enough to differentiate between IBMi and real AIX system.
808+
getisIBMi(){
809+
returnrequire('os').type()==='OS400';
810+
},
811+
812+
getisLinuxPPCBE(){
813+
return(process.platform==='linux')&&(process.arch==='ppc64')&&
814+
(require('os').endianness()==='BE');
815+
},
816+
812817
getlocalhostIPv4(){
813818
if(localhostIPv4!==null)returnlocalhostIPv4;
814819

@@ -830,6 +835,8 @@ module.exports ={
830835
returnlocalhostIPv4;
831836
},
832837

838+
getlocalhostIPv6(){return'::1';},
839+
833840
// opensslCli defined lazily to reduce overhead of spawnSync
834841
getopensslCli(){
835842
if(opensslCli!==null)returnopensslCli;

0 commit comments

Comments
(0)