X Tutup
Skip to content

Commit 05783d5

Browse files
chore: make the 'npm run test' command work out of the box (electron#14602)
1 parent 429b18d commit 05783d5

File tree

6 files changed

+100
-13
lines changed

6 files changed

+100
-13
lines changed

.circleci/config.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ build-steps: &build-steps
119119
if [ "$RUN_TESTS" != "false" ]; then
120120
cd src
121121
ninja -C out/Default third_party/electron_node:headers
122-
export npm_config_nodedir="$PWD/out/Default/gen/node_headers"
123-
(cd electron/spec && npm install)
124-
python electron/script/lib/dbus_mock.py ./out/Default/electron electron/spec --ci --enable-logging
122+
(cd electron && npm run test -- --ci --enable-logging)
125123
fi
126124
- <<: *notify-slack-failure
127125
- <<: *notify-slack-success
@@ -197,9 +195,7 @@ mac-build-steps: &mac-build-steps
197195
if [ "$RUN_TESTS" != "false" ]; then
198196
cd src
199197
ninja -C out/Default third_party/electron_node:headers
200-
export npm_config_nodedir="$PWD/out/Default/gen/node_headers"
201-
(cd electron/spec && npm install)
202-
./out/Default/Electron.app/Contents/MacOS/Electron electron/spec --ci --enable-logging
198+
(cd electron && npm run test -- --ci --enable-logging)
203199
fi
204200
- <<: *notify-slack-failure
205201
- <<: *notify-slack-success

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ compile_commands.json
5656
# Generated API definitions
5757
electron-api.json
5858
electron.d.ts
59+
60+
# Spec hash calculation
61+
spec/.hash

DEPS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ hooks = [
6161
'pattern': 'src/electron/package.json',
6262
'name': 'electron_npm_deps'
6363
},
64+
{
65+
'action': [
66+
'python',
67+
'-c',
68+
'import os; os.chdir("src"); os.chdir("electron/spec"); os.system("npm install")',
69+
],
70+
'pattern': 'src/electron/spec/package.json',
71+
'name': 'electron_spec_npm_deps'
72+
},
6473
]
6574

6675
recursedeps = [

appveyor.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ test_script:
3434
- ps: >-
3535
if ($env:GN_CONFIG -eq 'testing') {
3636
ninja -C out/Default third_party/electron_node:headers
37-
$env:npm_config_nodedir="$pwd/out/Default/gen/node_headers"
38-
$env:npm_config_msvs_version="2017"
3937
New-Item .\out\Default\gen\node_headers\Release -Type directory
4038
Copy-Item -path .\out\Default\electron.lib -destination .\out\Default\gen\node_headers\Release\node.lib
41-
Push-Location; cd electron/spec
42-
npm install
43-
Pop-Location
4439
} else {
4540
echo "Skipping tests for $env:GN_CONFIG build"
4641
}
47-
- if "%GN_CONFIG%"=="testing" ( echo Running test suite & .\out\Default\electron.exe electron\spec --ci )
42+
- cd electron
43+
- if "%GN_CONFIG%"=="testing" ( echo Running test suite & npm run test -- --ci )

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"electabul": "~0.0.4",
1515
"electron-docs-linter": "^2.3.4",
1616
"electron-typescript-definitions": "^2.0.0",
17+
"folder-hash": "^2.1.1",
1718
"github": "^9.2.0",
1819
"html-entities": "^1.2.1",
1920
"husky": "^0.14.3",
@@ -72,7 +73,7 @@
7273
"release": "node ./script/release.js",
7374
"repl": "python ./script/start.py --interactive",
7475
"start": "python ./script/start.py",
75-
"test": "python ./script/test.py"
76+
"test": "node ./script/spec-runner.js electron/spec"
7677
},
7778
"license": "MIT",
7879
"author": "Electron Community",

script/spec-runner.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const cp = require('child_process')
2+
const crypto = require('crypto')
3+
const fs = require('fs')
4+
const { hashElement } = require('folder-hash')
5+
const path = require('path')
6+
7+
const BASE = path.resolve(__dirname, '../..')
8+
const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
9+
const OUT_DIR = process.env.ELECTRON_SPEC_OUT_DIR || 'Default'
10+
11+
const specHashPath = path.resolve(__dirname, '../spec/.hash')
12+
13+
const [lastSpecHash, lastSpecInstallHash] = fs.existsSync(specHashPath)
14+
? fs.readFileSync(specHashPath, 'utf8').split('\n')
15+
: ['invalid', 'invalid']
16+
17+
getSpecHash().then(([currentSpecHash, currentSpecInstallHash]) => {
18+
const specChanged = currentSpecHash !== lastSpecHash
19+
const installChanged = lastSpecInstallHash !== currentSpecInstallHash
20+
if (specChanged || installChanged) {
21+
const out = cp.spawnSync(NPM_CMD, ['install'], {
22+
env: Object.assign({}, process.env, {
23+
npm_config_nodedir: path.resolve(BASE, `out/${OUT_DIR}/gen/node_headers`),
24+
npm_config_msvs_version: '2017'
25+
}),
26+
cwd: path.resolve(__dirname, '../spec')
27+
})
28+
if (out.status !== 0) {
29+
console.error('Failed to npm install in the spec folder')
30+
process.exit(1)
31+
}
32+
return getSpecHash()
33+
.then(([newSpecHash, newSpecInstallHash]) => {
34+
fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`)
35+
})
36+
}
37+
}).then(() => {
38+
let exe = path.resolve(BASE, getElectronExec())
39+
const args = process.argv.slice(2)
40+
if (process.platform === 'linux') {
41+
args.unshift(path.resolve(__dirname, 'lib/dbus_mock.py'), exe)
42+
exe = 'python'
43+
}
44+
const child = cp.spawn(exe, args, {
45+
cwd: path.resolve(__dirname, '../..'),
46+
stdio: 'inherit'
47+
})
48+
child.on('exit', (code) => {
49+
process.exit(code)
50+
})
51+
})
52+
53+
function getElectronExec () {
54+
switch (process.platform) {
55+
case 'darwin':
56+
return 'out/Default/Electron.app/Contents/MacOS/Electron'
57+
case 'win32':
58+
return 'out/Default/electron.exe'
59+
case 'linux':
60+
return 'out/Default/electron'
61+
default:
62+
throw new Error('Unknown path')
63+
}
64+
}
65+
66+
function getSpecHash () {
67+
return Promise.all([
68+
new Promise((resolve) => {
69+
const hasher = crypto.createHash('SHA256')
70+
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
71+
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package-lock.json')))
72+
resolve(hasher.digest('hex'))
73+
}),
74+
new Promise((resolve, reject) => {
75+
const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
76+
if (!fs.existsSync(specNodeModulesPath)) {
77+
return resolve('invalid')
78+
}
79+
hashElement(specNodeModulesPath).then((result) => resolve(result.hash)).catch(reject)
80+
})
81+
])
82+
}

0 commit comments

Comments
 (0)
X Tutup