Skip to content

Commit b192521

Browse files
rewrite tests from tap to mocha
After make-fetch-happen update GitHub Actions started failing. Rewritting tests from tap to mocha testing framework for GItHub Action stability. Deprecating tap completely.
1 parent 2a4cb64 commit b192521

12 files changed

+1581
-1721
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@
4141
"mocha": "^10.2.0",
4242
"nan": "^2.14.2",
4343
"require-inject": "^1.4.4",
44-
"standard": "^14.3.4",
45-
"tap": "^12.7.0"
44+
"standard": "^14.3.4"
4645
},
4746
"scripts": {
4847
"lint": "standard */*.js test/**/*.js",
49-
"test": "npm run lint && mocha test/test-*"
48+
"test": "npm run lint && mocha test/test-download.js test/test-*"
5049
}
5150
}

test/test-addon.js

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const test = require('tap').test
3+
const { describe, it } = require('mocha')
4+
const assert = require('assert')
45
const path = require('path')
56
const fs = require('graceful-fs')
67
const childProcess = require('child_process')
@@ -35,116 +36,117 @@ function checkCharmapValid () {
3536
return lines.pop() === 'True'
3637
}
3738

38-
test('build simple addon', function (t) {
39-
t.plan(3)
40-
41-
// Set the loglevel otherwise the output disappears when run via 'npm test'
42-
var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
43-
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
44-
var logLines = stderr.toString().trim().split(/\r?\n/)
45-
var lastLine = logLines[logLines.length - 1]
46-
t.strictEqual(err, null)
47-
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
48-
t.strictEqual(runHello().trim(), 'world')
39+
describe('addon', function () {
40+
this.timeout(120000)
41+
42+
it('build simple addon', function (done) {
43+
// Set the loglevel otherwise the output disappears when run via 'npm test'
44+
var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
45+
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
46+
var logLines = stderr.toString().trim().split(/\r?\n/)
47+
var lastLine = logLines[logLines.length - 1]
48+
assert.strictEqual(err, null)
49+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
50+
assert.strictEqual(runHello().trim(), 'world')
51+
done()
52+
})
53+
proc.stdout.setEncoding('utf-8')
54+
proc.stderr.setEncoding('utf-8')
4955
})
50-
proc.stdout.setEncoding('utf-8')
51-
proc.stderr.setEncoding('utf-8')
52-
})
53-
54-
test('build simple addon in path with non-ascii characters', function (t) {
55-
t.plan(1)
5656

57-
if (!checkCharmapValid()) {
58-
return t.skip('python console app can\'t encode non-ascii character.')
59-
}
57+
it('build simple addon in path with non-ascii characters', function (done) {
58+
if (!checkCharmapValid()) {
59+
return this.skip('python console app can\'t encode non-ascii character.')
60+
}
6061

61-
var testDirNames = {
62-
cp936: '文件夹',
63-
cp1252: 'Latīna',
64-
cp932: 'フォルダ'
65-
}
66-
// Select non-ascii characters by current encoding
67-
var testDirName = testDirNames[getEncoding()]
68-
// If encoding is UTF-8 or other then no need to test
69-
if (!testDirName) {
70-
return t.skip('no need to test')
71-
}
62+
var testDirNames = {
63+
cp936: '文件夹',
64+
cp1252: 'Latīna',
65+
cp932: 'フォルダ'
66+
}
67+
// Select non-ascii characters by current encoding
68+
var testDirName = testDirNames[getEncoding()]
69+
// If encoding is UTF-8 or other then no need to test
70+
if (!testDirName) {
71+
return this.skip('no need to test')
72+
}
7273

73-
t.plan(3)
74+
this.timeout(120000)
7475

75-
var data
76-
var configPath = path.join(addonPath, 'build', 'config.gypi')
77-
try {
78-
data = fs.readFileSync(configPath, 'utf8')
79-
} catch (err) {
80-
t.error(err)
81-
return
82-
}
83-
var config = JSON.parse(data.replace(/#.+\n/, ''))
84-
var nodeDir = config.variables.nodedir
85-
var testNodeDir = path.join(addonPath, testDirName)
86-
// Create symbol link to path with non-ascii characters
87-
try {
88-
fs.symlinkSync(nodeDir, testNodeDir, 'dir')
89-
} catch (err) {
90-
switch (err.code) {
91-
case 'EEXIST': break
92-
case 'EPERM':
93-
t.error(err, 'Please try to running console as an administrator')
94-
return
95-
default:
96-
t.error(err)
97-
return
76+
var data
77+
var configPath = path.join(addonPath, 'build', 'config.gypi')
78+
try {
79+
data = fs.readFileSync(configPath, 'utf8')
80+
} catch (err) {
81+
assert.fail(err)
82+
return
9883
}
99-
}
100-
101-
var cmd = [
102-
nodeGyp,
103-
'rebuild',
104-
'-C',
105-
addonPath,
106-
'--loglevel=verbose',
107-
'-nodedir=' + testNodeDir
108-
]
109-
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
84+
var config = JSON.parse(data.replace(/#.+\n/, ''))
85+
var nodeDir = config.variables.nodedir
86+
var testNodeDir = path.join(addonPath, testDirName)
87+
// Create symbol link to path with non-ascii characters
11088
try {
111-
fs.unlink(testNodeDir)
89+
fs.symlinkSync(nodeDir, testNodeDir, 'dir')
11290
} catch (err) {
113-
t.error(err)
91+
switch (err.code) {
92+
case 'EEXIST': break
93+
case 'EPERM':
94+
assert.fail(err, null, 'Please try to running console as an administrator')
95+
return
96+
default:
97+
assert.fail(err)
98+
return
99+
}
114100
}
115101

116-
var logLines = stderr.toString().trim().split(/\r?\n/)
117-
var lastLine = logLines[logLines.length - 1]
118-
t.strictEqual(err, null)
119-
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
120-
t.strictEqual(runHello().trim(), 'world')
102+
var cmd = [
103+
nodeGyp,
104+
'rebuild',
105+
'-C',
106+
addonPath,
107+
'--loglevel=verbose',
108+
'-nodedir=' + testNodeDir
109+
]
110+
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
111+
try {
112+
fs.unlink(testNodeDir)
113+
} catch (err) {
114+
assert.fail(err)
115+
}
116+
117+
var logLines = stderr.toString().trim().split(/\r?\n/)
118+
var lastLine = logLines[logLines.length - 1]
119+
assert.strictEqual(err, null)
120+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
121+
assert.strictEqual(runHello().trim(), 'world')
122+
done()
123+
})
124+
proc.stdout.setEncoding('utf-8')
125+
proc.stderr.setEncoding('utf-8')
121126
})
122-
proc.stdout.setEncoding('utf-8')
123-
proc.stderr.setEncoding('utf-8')
124-
})
125-
126-
test('addon works with renamed host executable', function (t) {
127-
// No `fs.copyFileSync` before node8.
128-
if (process.version.substr(1).split('.')[0] < 8) {
129-
t.skip('skipping test for old node version')
130-
t.end()
131-
return
132-
}
133127

134-
t.plan(3)
135-
136-
var notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
137-
fs.copyFileSync(process.execPath, notNodePath)
128+
it('addon works with renamed host executable', function (done) {
129+
// No `fs.copyFileSync` before node8.
130+
if (process.version.substr(1).split('.')[0] < 8) {
131+
return this.skip('skipping test for old node version')
132+
}
138133

139-
var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
140-
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
141-
var logLines = stderr.toString().trim().split(/\r?\n/)
142-
var lastLine = logLines[logLines.length - 1]
143-
t.strictEqual(err, null)
144-
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
145-
t.strictEqual(runHello(notNodePath).trim(), 'world')
146-
fs.unlinkSync(notNodePath)
134+
this.timeout(120000)
135+
136+
var notNodePath = path.join(os.tmpdir(), 'notnode' + path.extname(process.execPath))
137+
fs.copyFileSync(process.execPath, notNodePath)
138+
139+
var cmd = [nodeGyp, 'rebuild', '-C', addonPath, '--loglevel=verbose']
140+
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
141+
var logLines = stderr.toString().trim().split(/\r?\n/)
142+
var lastLine = logLines[logLines.length - 1]
143+
assert.strictEqual(err, null)
144+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
145+
assert.strictEqual(runHello(notNodePath).trim(), 'world')
146+
fs.unlinkSync(notNodePath)
147+
done()
148+
})
149+
proc.stdout.setEncoding('utf-8')
150+
proc.stderr.setEncoding('utf-8')
147151
})
148-
proc.stdout.setEncoding('utf-8')
149-
proc.stderr.setEncoding('utf-8')
150152
})

test/test-configure-python.js

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const test = require('tap').test
3+
const { describe, it } = require('mocha')
4+
const assert = require('assert')
45
const path = require('path')
56
const devDir = require('./common').devDir()
67
const gyp = require('../lib/node-gyp')
@@ -22,63 +23,59 @@ const configure = requireInject('../lib/configure', {
2223

2324
const EXPECTED_PYPATH = path.join(__dirname, '..', 'gyp', 'pylib')
2425
const SEPARATOR = process.platform === 'win32' ? ';' : ':'
25-
const SPAWN_RESULT = { on: function () { } }
26+
const SPAWN_RESULT = cb => ({ on: function () { cb() } })
2627

2728
require('npmlog').level = 'warn'
2829

29-
test('configure PYTHONPATH with no existing env', function (t) {
30-
t.plan(1)
31-
32-
delete process.env.PYTHONPATH
33-
34-
var prog = gyp()
35-
prog.parseArgv([])
36-
prog.spawn = function () {
37-
t.equal(process.env.PYTHONPATH, EXPECTED_PYPATH)
38-
return SPAWN_RESULT
39-
}
40-
prog.devDir = devDir
41-
configure(prog, [], t.fail)
42-
})
43-
44-
test('configure PYTHONPATH with existing env of one dir', function (t) {
45-
t.plan(2)
46-
47-
var existingPath = path.join('a', 'b')
48-
process.env.PYTHONPATH = existingPath
49-
50-
var prog = gyp()
51-
prog.parseArgv([])
52-
prog.spawn = function () {
53-
t.equal(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
54-
55-
var dirs = process.env.PYTHONPATH.split(SEPARATOR)
56-
t.deepEqual(dirs, [EXPECTED_PYPATH, existingPath])
57-
58-
return SPAWN_RESULT
59-
}
60-
prog.devDir = devDir
61-
configure(prog, [], t.fail)
62-
})
63-
64-
test('configure PYTHONPATH with existing env of multiple dirs', function (t) {
65-
t.plan(2)
66-
67-
var pythonDir1 = path.join('a', 'b')
68-
var pythonDir2 = path.join('b', 'c')
69-
var existingPath = [pythonDir1, pythonDir2].join(SEPARATOR)
70-
process.env.PYTHONPATH = existingPath
71-
72-
var prog = gyp()
73-
prog.parseArgv([])
74-
prog.spawn = function () {
75-
t.equal(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
76-
77-
var dirs = process.env.PYTHONPATH.split(SEPARATOR)
78-
t.deepEqual(dirs, [EXPECTED_PYPATH, pythonDir1, pythonDir2])
79-
80-
return SPAWN_RESULT
81-
}
82-
prog.devDir = devDir
83-
configure(prog, [], t.fail)
30+
describe('configure-python', function () {
31+
it('configure PYTHONPATH with no existing env', function (done) {
32+
delete process.env.PYTHONPATH
33+
34+
var prog = gyp()
35+
prog.parseArgv([])
36+
prog.spawn = function () {
37+
assert.strictEqual(process.env.PYTHONPATH, EXPECTED_PYPATH)
38+
return SPAWN_RESULT(done)
39+
}
40+
prog.devDir = devDir
41+
configure(prog, [], assert.fail)
42+
})
43+
44+
it('configure PYTHONPATH with existing env of one dir', function (done) {
45+
var existingPath = path.join('a', 'b')
46+
process.env.PYTHONPATH = existingPath
47+
48+
var prog = gyp()
49+
prog.parseArgv([])
50+
prog.spawn = function () {
51+
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
52+
53+
var dirs = process.env.PYTHONPATH.split(SEPARATOR)
54+
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, existingPath])
55+
56+
return SPAWN_RESULT(done)
57+
}
58+
prog.devDir = devDir
59+
configure(prog, [], assert.fail)
60+
})
61+
62+
it('configure PYTHONPATH with existing env of multiple dirs', function (done) {
63+
var pythonDir1 = path.join('a', 'b')
64+
var pythonDir2 = path.join('b', 'c')
65+
var existingPath = [pythonDir1, pythonDir2].join(SEPARATOR)
66+
process.env.PYTHONPATH = existingPath
67+
68+
var prog = gyp()
69+
prog.parseArgv([])
70+
prog.spawn = function () {
71+
assert.strictEqual(process.env.PYTHONPATH, [EXPECTED_PYPATH, existingPath].join(SEPARATOR))
72+
73+
var dirs = process.env.PYTHONPATH.split(SEPARATOR)
74+
assert.deepStrictEqual(dirs, [EXPECTED_PYPATH, pythonDir1, pythonDir2])
75+
76+
return SPAWN_RESULT(done)
77+
}
78+
prog.devDir = devDir
79+
configure(prog, [], assert.fail)
80+
})
8481
})

0 commit comments

Comments
 (0)