Skip to content

Commit e95f094

Browse files
committed
fix: remove custom test reporter
1 parent 32a7787 commit e95f094

File tree

12 files changed

+136
-232
lines changed

12 files changed

+136
-232
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: 18 additions & 1 deletion
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:
@@ -77,7 +90,11 @@ jobs:
7790
if: runner.os != 'Windows'
7891
shell: bash
7992
run: npm test --python="${pythonLocation}/python"
93+
env:
94+
FAST_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '0' || '1' }}
8095
- name: Run tests (Windows)
8196
if: runner.os == 'Windows'
8297
shell: pwsh
8398
run: npm run test --python="${env:pythonLocation}\\python.exe"
99+
env:
100+
FAST_TEST: ${{ (matrix.node == '20.x' && matrix.python == '3.12') && '0' || '1' }}

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/install.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const processRelease = require('./process-release')
1414
const win = process.platform === 'win32'
1515

1616
async function install (gyp, argv) {
17-
console.log()
17+
log.stdout()
1818
const release = processRelease(argv, gyp, process.version, process.release)
1919
// Detecting target_arch based on logic from create-cnfig-gyp.js. Used on Windows only.
2020
const arch = win ? (gyp.opts.target_arch || gyp.opts.arch || process.arch || 'ia32') : ''

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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
},
4646
"scripts": {
4747
"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-*"
48+
"test": "NODE_GYP_NULL_LOGGER=true mocha --timeout 15000 test/test-download.js test/test-*"
4949
}
5050
}

test/common.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const envPaths = require('env-paths')
2+
const semver = require('semver')
23

34
module.exports.devDir = () => envPaths('node-gyp', { suffix: '' }).cache
45

@@ -15,3 +16,7 @@ module.exports.poison = (object, property) => {
1516
}
1617
Object.defineProperty(object, property, descriptor)
1718
}
19+
20+
module.exports.skip = process.env.FAST_TEST === '1' ||
21+
process.release.name !== 'node' ||
22+
semver.prerelease(process.version) !== null

test/reporter.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

test/test-addon.js

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,56 @@ const { describe, it } = require('mocha')
44
const assert = require('assert')
55
const path = require('path')
66
const fs = require('graceful-fs')
7-
const { execFileSync, execFile } = require('child_process')
87
const os = require('os')
8+
const cp = require('child_process')
9+
const util = require('../lib/util')
10+
911
const addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
1012
const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
1113

12-
function runHello (hostProcess) {
13-
if (!hostProcess) {
14-
hostProcess = process.execPath
15-
}
14+
const execFileSync = (...args) => cp.execFileSync(...args).toString().trim()
15+
16+
const execFile = async (cmd) => {
17+
const [err,, stderr] = await util.execFile(process.execPath, cmd, {
18+
env: { ...process.env, NODE_GYP_NULL_LOGGER: undefined },
19+
encoding: 'utf-8'
20+
})
21+
return [err, stderr.toString().trim().split(/\r?\n/)]
22+
}
23+
24+
function runHello (hostProcess = process.execPath) {
1625
const testCode = "console.log(require('hello_world').hello())"
17-
return execFileSync(hostProcess, ['-e', testCode], { cwd: __dirname }).toString()
26+
return execFileSync(hostProcess, ['-e', testCode], { cwd: __dirname })
1827
}
1928

2029
function getEncoding () {
2130
const code = 'import locale;print(locale.getdefaultlocale()[1])'
22-
return execFileSync('python', ['-c', code]).toString().trim()
31+
return execFileSync('python', ['-c', code])
2332
}
2433

2534
function checkCharmapValid () {
26-
let data
2735
try {
28-
data = execFileSync('python', ['fixtures/test-charmap.py'],
29-
{ cwd: __dirname })
30-
} catch (err) {
36+
const data = execFileSync('python', ['fixtures/test-charmap.py'], { cwd: __dirname })
37+
return data.split('\n').pop() === 'True'
38+
} catch {
3139
return false
3240
}
33-
const lines = data.toString().trim().split('\n')
34-
return lines.pop() === 'True'
3541
}
3642

3743
describe('addon', function () {
3844
this.timeout(300000)
3945

40-
it('build simple addon', function (done) {
46+
it('build simple addon', async function () {
4147
// Set the loglevel otherwise the output disappears when run via 'npm test'
4248
const cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
43-
const proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
44-
const logLines = stderr.toString().trim().split(/\r?\n/)
45-
const lastLine = logLines[logLines.length - 1]
46-
assert.strictEqual(err, null)
47-
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
48-
assert.strictEqual(runHello().trim(), 'world')
49-
done()
50-
})
51-
proc.stdout.setEncoding('utf-8')
52-
proc.stderr.setEncoding('utf-8')
49+
const [err, logLines] = await execFile(cmd)
50+
const lastLine = logLines[logLines.length - 1]
51+
assert.strictEqual(err, null)
52+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
53+
assert.strictEqual(runHello(), 'world')
5354
})
5455

55-
it('build simple addon in path with non-ascii characters', function (done) {
56+
it('build simple addon in path with non-ascii characters', async function () {
5657
if (!checkCharmapValid()) {
5758
return this.skip('python console app can\'t encode non-ascii character.')
5859
}
@@ -105,46 +106,30 @@ describe('addon', function () {
105106
'--loglevel=verbose',
106107
'-nodedir=' + testNodeDir
107108
]
108-
const proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
109-
try {
110-
fs.unlink(testNodeDir)
111-
} catch (err) {
112-
assert.fail(err)
113-
}
114-
115-
const logLines = stderr.toString().trim().split(/\r?\n/)
116-
const lastLine = logLines[logLines.length - 1]
117-
assert.strictEqual(err, null)
118-
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
119-
assert.strictEqual(runHello().trim(), 'world')
120-
done()
121-
})
122-
proc.stdout.setEncoding('utf-8')
123-
proc.stderr.setEncoding('utf-8')
124-
})
125-
126-
it('addon works with renamed host executable', function (done) {
127-
// No `fs.copyFileSync` before node8.
128-
if (process.version.substr(1).split('.')[0] < 8) {
129-
return this.skip('skipping test for old node version')
109+
const [err, logLines] = await execFile(cmd)
110+
try {
111+
fs.unlink(testNodeDir)
112+
} catch (err) {
113+
assert.fail(err)
130114
}
115+
const lastLine = logLines[logLines.length - 1]
116+
assert.strictEqual(err, null)
117+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
118+
assert.strictEqual(runHello(), 'world')
119+
})
131120

121+
it('addon works with renamed host executable', async function () {
132122
this.timeout(300000)
133123

134124
const notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
135125
fs.copyFileSync(process.execPath, notNodePath)
136126

137127
const cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
138-
const proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
139-
const logLines = stderr.toString().trim().split(/\r?\n/)
140-
const lastLine = logLines[logLines.length - 1]
141-
assert.strictEqual(err, null)
142-
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
143-
assert.strictEqual(runHello(notNodePath).trim(), 'world')
144-
fs.unlinkSync(notNodePath)
145-
done()
146-
})
147-
proc.stdout.setEncoding('utf-8')
148-
proc.stderr.setEncoding('utf-8')
128+
const [err, logLines] = await execFile(cmd)
129+
const lastLine = logLines[logLines.length - 1]
130+
assert.strictEqual(err, null)
131+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
132+
assert.strictEqual(runHello(notNodePath), 'world')
133+
fs.unlinkSync(notNodePath)
149134
})
150135
})

test/test-configure-python.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const assert = require('assert')
55
const path = require('path')
66
const devDir = require('./common').devDir()
77
const gyp = require('../lib/node-gyp')
8-
const log = require('../lib/log')
98
const requireInject = require('require-inject')
9+
1010
const configure = requireInject('../lib/configure', {
1111
'graceful-fs': {
1212
openSync: () => 0,
@@ -19,8 +19,6 @@ const configure = requireInject('../lib/configure', {
1919
}
2020
})
2121

22-
log.logger.stream = null
23-
2422
const EXPECTED_PYPATH = path.join(__dirname, '..', 'gyp', 'pylib')
2523
const SEPARATOR = process.platform === 'win32' ? ';' : ':'
2624
const SPAWN_RESULT = cb => ({ on: function () { cb() } })

0 commit comments

Comments
 (0)