Skip to content

Commit 491f69d

Browse files
committed
Add ability to run tests with coverage report
1 parent e17e195 commit 491f69d

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"scripts": {
2525
"bootstrap": "python ./script/bootstrap.py",
2626
"build": "python ./script/build.py -c D",
27+
"coverage": "npm run instrument-code-coverage && npm run test -- --use-instrumented-asar",
2728
"instrument-code-coverage": "node ./spec/coverage/instrument.js",
2829
"lint": "npm run lint-js && npm run lint-cpp && npm run lint-docs",
2930
"lint-js": "standard && cd spec && standard",

script/test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
import os
4+
import shutil
45
import subprocess
56
import sys
67

@@ -24,17 +25,30 @@ def main():
2425
electron = os.path.join(SOURCE_ROOT, 'out', config,
2526
'{0}.app'.format(PRODUCT_NAME), 'Contents',
2627
'MacOS', PRODUCT_NAME)
28+
resources_path = os.path.join(SOURCE_ROOT, 'out', config,
29+
'{0}.app'.format(PRODUCT_NAME), 'Contents',
30+
'Resources')
2731
elif sys.platform == 'win32':
2832
electron = os.path.join(SOURCE_ROOT, 'out', config,
2933
'{0}.exe'.format(PROJECT_NAME))
34+
resources_path = os.path.join(SOURCE_ROOT, 'out', config)
3035
else:
3136
electron = os.path.join(SOURCE_ROOT, 'out', config, PROJECT_NAME)
37+
resources_path = os.path.join(SOURCE_ROOT, 'out', config)
3238

39+
use_instrumented_asar = '--use-instrumented-asar' in sys.argv
3340
returncode = 0
3441
try:
42+
if use_instrumented_asar:
43+
install_instrumented_asar_file(resources_path)
3544
subprocess.check_call([electron, 'spec'] + sys.argv[1:])
3645
except subprocess.CalledProcessError as e:
3746
returncode = e.returncode
47+
except KeyboardInterrupt:
48+
returncode = 0
49+
50+
if use_instrumented_asar:
51+
restore_uninstrumented_asar_file(resources_path)
3852

3953
if os.environ.has_key('OUTPUT_TO_FILE'):
4054
output_to_file = os.environ['OUTPUT_TO_FILE']
@@ -46,5 +60,23 @@ def main():
4660
return returncode
4761

4862

63+
def install_instrumented_asar_file(resources_path):
64+
asar_path = os.path.join(resources_path, '{0}.asar'.format(PROJECT_NAME))
65+
uninstrumented_path = os.path.join(resources_path,
66+
'{0}-original.asar'.format(PROJECT_NAME))
67+
instrumented_path = os.path.join(SOURCE_ROOT, 'out', 'coverage',
68+
'{0}.asar'.format(PROJECT_NAME))
69+
shutil.move(asar_path, uninstrumented_path)
70+
shutil.move(instrumented_path, asar_path)
71+
72+
73+
def restore_uninstrumented_asar_file(resources_path):
74+
asar_path = os.path.join(resources_path, '{0}.asar'.format(PROJECT_NAME))
75+
uninstrumented_path = os.path.join(resources_path,
76+
'{0}-original.asar'.format(PROJECT_NAME))
77+
os.remove(asar_path)
78+
shutil.move(uninstrumented_path, asar_path)
79+
80+
4981
if __name__ == '__main__':
5082
sys.exit(main())

spec/coverage/instrument.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ glob.sync('**/*.js', {cwd: libPath}).forEach(function (relativePath) {
2222
fs.writeFileSync(generatedPath, generated)
2323
})
2424

25-
var asarPath = path.join(outputPath, 'electron-instrumented.asar')
25+
var asarPath = path.join(outputPath, 'electron.asar')
2626
asar.createPackageWithOptions(path.join(outputPath, 'lib'), asarPath, {}, function (error) {
2727
if (error) {
2828
console.error(error.stack || error)
Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const addUnrequiredFiles = (coverage) => {
3333
}
3434

3535
// Generate a code coverage report in out/coverage/lcov-report
36-
exports.generate = () => {
36+
exports.generateReport = () => {
3737
const coverage = window.__coverage__
3838
if (coverage == null) return
3939

@@ -49,27 +49,3 @@ exports.generate = () => {
4949
reporter.addAll(['text', 'lcov'])
5050
reporter.write(collector, true, function () {})
5151
}
52-
53-
// Generate an instrumented .asar file for all the files in lib/ and save it
54-
// to out/coverage/electron-instrumented.asar
55-
exports.instrument = () => {
56-
const instrumenter = new Instrumenter()
57-
58-
glob.sync('**/*.js', {cwd: libPath}).forEach(function (relativePath) {
59-
const rawPath = path.join(libPath, relativePath)
60-
const raw = fs.readFileSync(rawPath, 'utf8')
61-
62-
const generatedPath = path.join(outputPath, 'lib', relativePath)
63-
const generated = instrumenter.instrumentSync(raw, rawPath)
64-
mkdirp.sync(path.dirname(generatedPath))
65-
fs.writeFileSync(generatedPath, generated)
66-
})
67-
68-
const asarPath = path.join(outputPath, 'electron-instrumented.asar')
69-
asar.createPackageWithOptions(path.join(outputPath, 'lib'), asarPath, {}, function (error) {
70-
if (error) {
71-
console.error(error.stack || error)
72-
process.exit(1)
73-
}
74-
})
75-
}

spec/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
Mocha.utils.highlightTags('code');
8484
if (isCi)
8585
ipcRenderer.send('process.exit', runner.failures);
86-
require('./coverage').generate()
86+
require('../coverage/reporter').generateReport()
8787
});
8888
});
8989
})();

0 commit comments

Comments
 (0)