Skip to content

Commit 2475952

Browse files
committed
Cache coverage
1 parent c9da451 commit 2475952

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

lib/current-env.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ function getFamilyFromFilesystem () {
3030
}
3131
}
3232

33+
function getFamilyFromReport () {
34+
const originalExclude = process.report.excludeNetwork
35+
process.report.excludeNetwork = true
36+
const report = process.report.getReport()
37+
process.report.excludeNetwork = originalExclude
38+
if (report.header?.glibcVersionRuntime) {
39+
family = 'glibc'
40+
} else if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isMusl)) {
41+
family = 'musl'
42+
} else {
43+
family = null
44+
}
45+
return family
46+
}
47+
3348
let family
3449
function libc (osName) {
3550
if (osName !== 'linux') {
@@ -38,17 +53,7 @@ function libc (osName) {
3853
if (family === undefined) {
3954
family = getFamilyFromFilesystem()
4055
if (family === undefined) {
41-
const originalExclude = process.report.excludeNetwork
42-
process.report.excludeNetwork = true
43-
const report = process.report.getReport()
44-
process.report.excludeNetwork = originalExclude
45-
if (report.header?.glibcVersionRuntime) {
46-
family = 'glibc'
47-
} else if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isMusl)) {
48-
family = 'musl'
49-
} else {
50-
family = null
51-
}
56+
family = getFamilyFromReport()
5257
}
5358
}
5459
return family

test/check-platform.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,44 @@ t.test('libc', (t) => {
9898
let PLATFORM = 'linux'
9999
let REPORT = {}
100100
let readFileSync
101+
let noCache = true
102+
103+
function withCache (cb) {
104+
noCache = false
105+
cb()
106+
noCache = true
107+
withoutLibcCache()
108+
}
101109

102110
function withoutLibcCache () {
103111
readFileSync = () => {
104112
throw new Error('File not found')
105113
}
106-
noCacheChckPtfm = (...args) => {
107-
const original = t.mock('..', {
108-
'../lib/current-env': t.mock('../lib/current-env', {
109-
'node:fs': {
110-
readFileSync,
114+
const original = t.mock('..', {
115+
'../lib/current-env': t.mock('../lib/current-env', {
116+
'node:fs': {
117+
readFileSync: () => {
118+
return readFileSync()
111119
},
112-
'node:process': {
113-
platform: PLATFORM,
114-
report: {
115-
getReport: () => REPORT,
116-
},
120+
},
121+
'node:process': Object.defineProperty({
122+
report: {
123+
getReport: () => REPORT,
117124
},
125+
}, 'platform', {
126+
enumerable: true,
127+
get: () => PLATFORM,
118128
}),
119-
}).checkPlatform
120-
withoutLibcCache()
121-
return original(...args)
129+
}),
130+
}).checkPlatform
131+
noCacheChckPtfm = (...args) => {
132+
try {
133+
original(...args)
134+
} finally {
135+
if (noCache) {
136+
withoutLibcCache()
137+
}
138+
}
122139
}
123140
}
124141

@@ -137,8 +154,16 @@ t.test('libc', (t) => {
137154
t.test('glibc', (t) => {
138155
PLATFORM = 'linux'
139156

140-
readFileSync = () => 'this ldd file contains GNU C Library'
141-
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }), 'allows glibc on glibc from ldd file')
157+
withCache(() => {
158+
readFileSync = () => 'this ldd file contains GNU C Library'
159+
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }),
160+
'allows glibc on glibc from ldd file')
161+
162+
readFileSync = () => {
163+
throw new Error('File not found')
164+
}
165+
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }), 'allows glibc from ldd file cache')
166+
})
142167

143168
REPORT = {}
144169
t.throws(() => noCacheChckPtfm({ libc: 'glibc' }), { code: 'EBADPLATFORM' },
@@ -148,8 +173,13 @@ t.test('libc', (t) => {
148173
t.throws(() => noCacheChckPtfm({ libc: 'glibc' }), { code: 'EBADPLATFORM' },
149174
'fails when header is missing glibcVersionRuntime property')
150175

151-
REPORT = { header: { glibcVersionRuntime: '1' } }
152-
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }), 'allows glibc on glibc')
176+
withCache(() => {
177+
REPORT = { header: { glibcVersionRuntime: '1' } }
178+
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }), 'allows glibc on glibc')
179+
180+
REPORT = {}
181+
t.doesNotThrow(() => noCacheChckPtfm({ libc: 'glibc' }), 'allows glibc from report cache')
182+
})
153183

154184
readFileSync = () => 'this ldd file is unsupported'
155185
t.throws(() => noCacheChckPtfm({ libc: 'glibc' }), { code: 'EBADPLATFORM' },

0 commit comments

Comments
 (0)