Skip to content

Commit 4e493d4

Browse files
authored
chore: misc testing fixes (#2930)
* chore: misc test fixes * Sort test runs by os first * Use cross-env for test env var * Try sorting matrix params * Make FAST_TEST the default and rename to FULL_TEST * Separate helper functions to not need to export test obj in files
1 parent d52997e commit 4e493d4

18 files changed

+239
-329
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Contributor guide: https://github.com/nodejs/node/blob/main/CONTRIBUTING.md
77
##### Checklist
88
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->
99

10-
- [ ] `npm install && npm test` passes
10+
- [ ] `npm install && npm run lint && npm test` passes
1111
- [ ] tests are included <!-- Bug fixes and new features should include tests -->
1212
- [ ] documentation is changed or added
1313
- [ ] commit message follows [commit guidelines](https://github.com/googleapis/release-please#how-should-i-write-my-commits)

.github/workflows/tests.yml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,24 @@ jobs:
1818
- uses: actions/checkout@v4
1919
- run: pip install --user ruff
2020
- run: ruff --output-format=github --select="E,F,PLC,PLE,UP,W,YTT" --ignore="E721,PLC1901,S101,UP031" --target-version=py38 .
21+
Lint_JS:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout Repository
25+
uses: actions/checkout@v4
26+
- name: Use Node.js 20.x
27+
uses: actions/setup-node@v3
28+
with:
29+
node-version: 20.x
30+
- name: Install Dependencies
31+
run: npm install --no-progress
32+
- name: Lint
33+
run: npm run lint
2134
Engines:
2235
runs-on: ubuntu-latest
2336
steps:
2437
- name: Checkout Repository
25-
uses: actions/checkout@v3
38+
uses: actions/checkout@v4
2639
- name: Use Node.js 20.x
2740
uses: actions/setup-node@v3
2841
with:
@@ -41,10 +54,11 @@ jobs:
4154
fail-fast: false
4255
max-parallel: 15
4356
matrix:
44-
node: [16.x, 18.x, 20.x]
57+
os: [macos, ubuntu, windows]
4558
python: ["3.8", "3.10", "3.12"]
46-
os: [macos-latest, ubuntu-latest, windows-latest]
47-
runs-on: ${{ matrix.os }}
59+
node: [16.x, 18.x, 20.x]
60+
name: ${{ matrix.os }} - ${{ matrix.python }} - ${{ matrix.node }}
61+
runs-on: ${{ matrix.os }}-latest
4862
steps:
4963
- name: Checkout Repository
5064
uses: actions/checkout@v4
@@ -63,7 +77,7 @@ jobs:
6377
npm install --no-progress
6478
pip install pytest
6579
- name: Set Windows environment
66-
if: startsWith(matrix.os, 'windows')
80+
if: matrix.os == 'windows'
6781
run: |
6882
echo 'GYP_MSVS_VERSION=2015' >> $Env:GITHUB_ENV
6983
echo 'GYP_MSVS_OVERRIDE_PATH=C:\\Dummy' >> $Env:GITHUB_ENV
@@ -77,7 +91,11 @@ jobs:
7791
if: runner.os != 'Windows'
7892
shell: bash
7993
run: npm test --python="${pythonLocation}/python"
94+
env:
95+
FULL_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '1' || '0' }}
8096
- name: Run tests (Windows)
8197
if: runner.os == 'Windows'
8298
shell: pwsh
8399
run: npm run test --python="${env:pythonLocation}\\python.exe"
100+
env:
101+
FULL_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '1' || '0' }}

bin/node-gyp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ if (prog.devDir) {
3232

3333
if (prog.todo.length === 0) {
3434
if (~process.argv.indexOf('-v') || ~process.argv.indexOf('--version')) {
35-
console.log('v%s', prog.version)
35+
log.stdout('v%s', prog.version)
3636
} else {
37-
console.log('%s', prog.usage())
37+
log.stdout('%s', prog.usage())
3838
}
3939
process.exit(0)
4040
}
@@ -82,12 +82,12 @@ async function run () {
8282

8383
if (command.name === 'list') {
8484
if (args.length) {
85-
args.forEach((version) => console.log(version))
85+
args.forEach((version) => log.stdout(version))
8686
} else {
87-
console.log('No node development files installed. Use `node-gyp install` to install a version.')
87+
log.stdout('No node development files installed. Use `node-gyp install` to install a version.')
8888
}
8989
} else if (args.length >= 1) {
90-
console.log(...args.slice(1))
90+
log.stdout(...args.slice(1))
9191
}
9292

9393
// now run the next command in the queue

lib/configure.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

3-
const { openSync, closeSync, promises: fs } = require('graceful-fs')
3+
const { promises: fs } = require('graceful-fs')
44
const path = require('path')
55
const log = require('./log')
66
const os = require('os')
77
const processRelease = require('./process-release')
88
const win = process.platform === 'win32'
99
const findNodeDirectory = require('./find-node-directory')
10-
const createConfigGypi = require('./create-config-gypi')
11-
const { format: msgFormat } = require('util')
10+
const { createConfigGypi } = require('./create-config-gypi')
11+
const { format: msgFormat, findAccessibleSync } = require('util')
1212
const { findPython } = require('./find-python')
1313
const { findVisualStudio } = win ? require('./find-visualstudio') : {}
1414

@@ -277,32 +277,5 @@ async function configure (gyp, argv) {
277277
}
278278
}
279279

280-
/**
281-
* Returns the first file or directory from an array of candidates that is
282-
* readable by the current user, or undefined if none of the candidates are
283-
* readable.
284-
*/
285-
function findAccessibleSync (logprefix, dir, candidates) {
286-
for (let next = 0; next < candidates.length; next++) {
287-
const candidate = path.resolve(dir, candidates[next])
288-
let fd
289-
try {
290-
fd = openSync(candidate, 'r')
291-
} catch (e) {
292-
// this candidate was not found or not readable, do nothing
293-
log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
294-
continue
295-
}
296-
closeSync(fd)
297-
log.silly(logprefix, 'Found readable %s', candidate)
298-
return candidate
299-
}
300-
301-
return undefined
302-
}
303-
304280
module.exports = configure
305-
module.exports.test = {
306-
findAccessibleSync
307-
}
308281
module.exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'

lib/create-config-gypi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ async function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo, python }) {
143143
return configPath
144144
}
145145

146-
module.exports = createConfigGypi
147-
module.exports.test = {
146+
module.exports = {
147+
createConfigGypi,
148148
parseConfigGypi,
149149
getCurrentConfigGypi
150150
}

lib/download.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fetch = require('make-fetch-happen')
2+
const { promises: fs } = require('graceful-fs')
3+
const log = require('./log')
4+
5+
async function download (gyp, url) {
6+
log.http('GET', url)
7+
8+
const requestOpts = {
9+
headers: {
10+
'User-Agent': `node-gyp v${gyp.version} (node ${process.version})`,
11+
Connection: 'keep-alive'
12+
},
13+
proxy: gyp.opts.proxy,
14+
noProxy: gyp.opts.noproxy
15+
}
16+
17+
const cafile = gyp.opts.cafile
18+
if (cafile) {
19+
requestOpts.ca = await readCAFile(cafile)
20+
}
21+
22+
const res = await fetch(url, requestOpts)
23+
log.http(res.status, res.url)
24+
25+
return res
26+
}
27+
28+
async function readCAFile (filename) {
29+
// The CA file can contain multiple certificates so split on certificate
30+
// boundaries. [\S\s]*? is used to match everything including newlines.
31+
const ca = await fs.readFile(filename, 'utf8')
32+
const re = /(-----BEGIN CERTIFICATE-----[\S\s]*?-----END CERTIFICATE-----)/g
33+
return ca.match(re)
34+
}
35+
36+
module.exports = {
37+
download,
38+
readCAFile
39+
}

lib/install.js

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const { Transform, promises: { pipeline } } = require('stream')
99
const crypto = require('crypto')
1010
const log = require('./log')
1111
const semver = require('semver')
12-
const fetch = require('make-fetch-happen')
12+
const { download } = require('./download')
1313
const processRelease = require('./process-release')
14+
1415
const win = process.platform === 'win32'
1516

1617
async function install (gyp, argv) {
17-
console.log()
18+
log.stdout()
1819
const release = processRelease(argv, gyp, process.version, process.release)
1920
// Detecting target_arch based on logic from create-cnfig-gyp.js. Used on Windows only.
2021
const arch = win ? (gyp.opts.target_arch || gyp.opts.arch || process.arch || 'ia32') : ''
@@ -410,41 +411,5 @@ class ShaSum extends Transform {
410411
}
411412
}
412413

413-
async function download (gyp, url) {
414-
log.http('GET', url)
415-
416-
const requestOpts = {
417-
headers: {
418-
'User-Agent': `node-gyp v${gyp.version} (node ${process.version})`,
419-
Connection: 'keep-alive'
420-
},
421-
proxy: gyp.opts.proxy,
422-
noProxy: gyp.opts.noproxy
423-
}
424-
425-
const cafile = gyp.opts.cafile
426-
if (cafile) {
427-
requestOpts.ca = await readCAFile(cafile)
428-
}
429-
430-
const res = await fetch(url, requestOpts)
431-
log.http(res.status, res.url)
432-
433-
return res
434-
}
435-
436-
async function readCAFile (filename) {
437-
// The CA file can contain multiple certificates so split on certificate
438-
// boundaries. [\S\s]*? is used to match everything including newlines.
439-
const ca = await fs.readFile(filename, 'utf8')
440-
const re = /(-----BEGIN CERTIFICATE-----[\S\s]*?-----END CERTIFICATE-----)/g
441-
return ca.match(re)
442-
}
443-
444414
module.exports = install
445-
module.exports.test = {
446-
download,
447-
install,
448-
readCAFile
449-
}
450415
module.exports.usage = 'Install node development files for the specified node version.'

lib/log.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class Logger {
7373
style: { fg: 'red', bg: 'black' }
7474
}]
7575

76-
constructor () {
76+
constructor (stream) {
7777
process.on('log', (...args) => this.#onLog(...args))
7878
this.#levels = new Map(this.#levels.map((level, index) => [level.id, { ...level, index }]))
7979
this.level = 'info'
80-
this.stream = process.stderr
80+
this.stream = stream
8181
procLog.pause()
8282
}
8383

@@ -158,8 +158,12 @@ class Logger {
158158
}
159159
}
160160

161+
// used to suppress logs in tests
162+
const NULL_LOGGER = !!process.env.NODE_GYP_NULL_LOGGER
163+
161164
module.exports = {
162-
logger: new Logger(),
165+
logger: new Logger(NULL_LOGGER ? null : process.stderr),
166+
stdout: NULL_LOGGER ? () => {} : (...args) => console.log(...args),
163167
withPrefix,
164168
...procLog
165169
}

lib/util.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

3-
const log = require('./log')
43
const cp = require('child_process')
54
const path = require('path')
5+
const { openSync, closeSync } = require('graceful-fs')
6+
const log = require('./log')
67

78
const execFile = async (...args) => new Promise((resolve) => {
89
const child = cp.execFile(...args, (...a) => resolve(a))
@@ -48,8 +49,33 @@ async function regSearchKeys (keys, value, addOpts) {
4849
}
4950
}
5051

52+
/**
53+
* Returns the first file or directory from an array of candidates that is
54+
* readable by the current user, or undefined if none of the candidates are
55+
* readable.
56+
*/
57+
function findAccessibleSync (logprefix, dir, candidates) {
58+
for (let next = 0; next < candidates.length; next++) {
59+
const candidate = path.resolve(dir, candidates[next])
60+
let fd
61+
try {
62+
fd = openSync(candidate, 'r')
63+
} catch (e) {
64+
// this candidate was not found or not readable, do nothing
65+
log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
66+
continue
67+
}
68+
closeSync(fd)
69+
log.silly(logprefix, 'Found readable %s', candidate)
70+
return candidate
71+
}
72+
73+
return undefined
74+
}
75+
5176
module.exports = {
5277
execFile,
5378
regGetValue,
54-
regSearchKeys
79+
regSearchKeys,
80+
findAccessibleSync
5581
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
},
3939
"devDependencies": {
4040
"bindings": "^1.5.0",
41+
"cross-env": "^7.0.3",
4142
"mocha": "^10.2.0",
4243
"nan": "^2.14.2",
4344
"require-inject": "^1.4.4",
4445
"standard": "^17.0.0"
4546
},
4647
"scripts": {
4748
"lint": "standard \"*/*.js\" \"test/**/*.js\" \".github/**/*.js\"",
48-
"test": "npm run lint && mocha --timeout 15000 --reporter=test/reporter.js test/test-download.js test/test-*"
49+
"test": "cross-env NODE_GYP_NULL_LOGGER=true mocha --timeout 15000 test/test-download.js test/test-*"
4950
}
5051
}

0 commit comments

Comments
 (0)