summaryrefslogtreecommitdiff
path: root/lib/internal/coverage-gen/with_instrumentation.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/coverage-gen/with_instrumentation.js')
-rw-r--r--lib/internal/coverage-gen/with_instrumentation.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/internal/coverage-gen/with_instrumentation.js b/lib/internal/coverage-gen/with_instrumentation.js
new file mode 100644
index 0000000000..711a3c3f12
--- /dev/null
+++ b/lib/internal/coverage-gen/with_instrumentation.js
@@ -0,0 +1,36 @@
+'use strict';
+
+// This file contains hooks for nyc instrumented lib/ files to collect
+// JS coverage for core.
+// See `make coverage-build`.
+function writeCoverage() {
+ if (!global.__coverage__) {
+ return;
+ }
+
+ const path = require('path');
+ const { mkdirSync, writeFileSync } = require('fs');
+
+ const dirname = path.join(path.dirname(process.execPath), '.coverage');
+ const filename = `coverage-${process.pid}-${Date.now()}.json`;
+ try {
+ mkdirSync(dirname);
+ } catch (err) {
+ if (err.code !== 'EEXIST') {
+ console.error(err);
+ return;
+ }
+ }
+
+ const target = path.join(dirname, filename);
+ const coverageInfo = JSON.stringify(global.__coverage__);
+ try {
+ writeFileSync(target, coverageInfo);
+ } catch (err) {
+ console.error(err);
+ }
+}
+
+module.exports = {
+ writeCoverage
+};