Improve test coverage harness
MODIFY
shim/src/realmFacade.js
shim/src/realmFacade.js
-// Note: do not import anything to this file to prevent using implicit
-// dependencies.
+import { cleanupSource } from './utilities';
// buildChildRealm is immediately turned into a string, and this function is
// never referenced again, because it closes over the wrong intrinsics
// todo: This function is stringified and evaluated outside of the primal
// realms and it currently can't contain code coverage metrics.
-/* istanbul ignore next */
-function buildChildRealm({ initRootRealm, initCompartment, getRealmGlobal, realmEvaluate }) {
+/* istanbul ignore file */
+export function buildChildRealm(BaseRealm) {
+ const { initRootRealm, initCompartment, getRealmGlobal, realmEvaluate } = BaseRealm;
+
// This Object and Reflect are brand new, from a new unsafeRec, so no user
// code has been run or had a chance to manipulate them. We extract these
// properties for brevity, not for security. Don't ever run this function
@@ -111,7 +112,7 @@ function buildChildRealm({ initRootRealm, initCompartment, getRealmGlobal, realm
// the parentheses means we don't bind the 'buildChildRealm' name inside the
// child's namespace. this would accept an anonymous function declaration.
// function expression (not a declaration) so it has a completion value.
-const buildChildRealmString = `'use strict'; (${buildChildRealm})`;
+const buildChildRealmString = cleanupSource(`'use strict'; (${buildChildRealm})`);
export function createRealmFacade(unsafeRec, BaseRealm) {
const { unsafeEval } = unsafeRec;

MODIFY
shim/src/unsafeRec.js
shim/src/unsafeRec.js
import { getSharedGlobalDescs } from './stdlib';
import { repairAccessors } from './accessors';
import { repairFunctions } from './functions';
+import { cleanupSource } from './utilities';
import { freeze } from './commons';
// A "context" is a fresh unsafe Realm as given to us by existing platforms.
@@ -70,21 +71,8 @@ function createUnsafeRec(unsafeGlobal, allShims) {
});
}
-function strip(src) {
- /* START_TESTS_ONLY */
-
- // Restore eval.
- src = src.replace(/\(0,[^)]+\)/g, '(0, eval)');
-
- // Remove code coverage.
- src = src.replace(/cov_[^+]+\+\+[;,]/g, '');
-
- /* END_TESTS_ONLY */
- return src;
-}
-
-const repairAccessorsShim = strip(`"use strict"; (${repairAccessors})();`);
-const repairFunctionsShim = strip(`"use strict"; (${repairFunctions})();`);
+const repairAccessorsShim = cleanupSource(`"use strict"; (${repairAccessors})();`);
+const repairFunctionsShim = cleanupSource(`"use strict"; (${repairFunctions})();`);
// Create a new unsafeRec from a brand new context, with new intrinsics and a
// new global object

MODIFY
shim/src/utilities.js
shim/src/utilities.js
@@ -27,3 +27,17 @@ export function assert(condition, message) {
throwTantrum(message);
}
}
+
+// Remove code modifications.
+export function cleanupSource(src) {
+ /* START_TESTS_ONLY */
+
+ // Restore eval which is modified by esm module.
+ src = src.replace(/\(0,[^)]+\)/g, '(0, eval)');
+
+ // Remove code coverage which is injexted by module nyc.
+ src = src.replace(/cov_[^+]+\+\+[;,]/g, '');
+
+ /* END_TESTS_ONLY */
+ return src;
+}

MODIFY
shim/test/module/utilities.js
shim/test/module/utilities.js
import test from 'tape';
import sinon from 'sinon';
-import { throwTantrum, assert } from '../../src/utilities';
+import { throwTantrum, assert, cleanupSource } from '../../src/utilities';
/* eslint-disable no-console */
@@ -45,3 +45,16 @@ test('assert', t => {
console.error.restore();
});
+
+test('cleanupSource', t => {
+ t.plan(2);
+
+ t.equals(
+ cleanupSource(`function() { cov_2kmyol0g2w[0]++;return true; }`),
+ 'function() { return true; }'
+ );
+ t.equals(
+ cleanupSource(`function() { return (0, _123)('true'); }`),
+ `function() { return (0, eval)('true'); }`
+ );
+});

Improve test coverage harness