Skip to content

Commit

Permalink
chore: update 'import-bundle' package to use AVA for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Aug 21, 2020
1 parent 4ad6154 commit 15c233e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 77 deletions.
15 changes: 12 additions & 3 deletions packages/import-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"node": ">=10.15.1"
},
"scripts": {
"test": "tap --no-coverage --jobs=1 'test/**/test*.js'",
"test": "ava",
"build": "exit 0",
"lint-fix": "eslint --fix '**/*.js'",
"lint-check": "eslint '**/*.js'"
},
"devDependencies": {
"@agoric/bundle-source": "^1.1.6",
"@agoric/install-ses": "^0.2.0",
"esm": "^3.2.5",
"tap": "^14.10.5"
"ava": "^3.11.1",
"esm": "^3.2.5"
},
"peerDependencies": {
"ses": "^0.10.1"
Expand Down Expand Up @@ -79,5 +79,14 @@
},
"dependencies": {
"ses": "^0.10.1"
},
"ava": {
"files": [
"test/**/test-*.js"
],
"require": [
"esm"
],
"timeout": "2m"
}
}
42 changes: 19 additions & 23 deletions packages/import-bundle/test/test-compartment-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* global Compartment */
import '@agoric/install-ses';
import tap from 'tap';
import test from 'ava';
import { wrapInescapableCompartment } from '../src/compartment-wrapper.js';

const { test } = tap;

// We build a transform that allows oldSrc to increment the odometer, but not
// read it. Note, of course, that SES provides a far easier way to accomplish
// this (pass in a hardened `addMilage` endowment), but there are metering
Expand Down Expand Up @@ -72,55 +70,55 @@ const attemptResetByTransform = `() => {
}`;

function check(t, c, odometer, n) {
t.equal(odometer.read(), 0, `${n}.start`);
t.is(odometer.read(), 0, `${n}.start`);
c.evaluate(doAdd);
t.equal(odometer.read(), 1, `${n}.doAdd`);
t.is(odometer.read(), 1, `${n}.doAdd`);
odometer.reset();

c.evaluate(doAddInChild)(doAdd);
t.equal(odometer.read(), 1, `${n}.doAddInChild`);
t.is(odometer.read(), 1, `${n}.doAddInChild`);
odometer.reset();

odometer.add(5);
t.throws(
() => c.evaluate(attemptReset)(),
/forbidden access/,
{ message: /forbidden access/ },
`${n}.attemptReset`,
);
t.equal(odometer.read(), 5, `${n} not reset`);
t.is(odometer.read(), 5, `${n} not reset`);

t.throws(
() => c.evaluate(attemptResetInChild)(attemptReset),
/forbidden access/,
{ message: /forbidden access/ },
`${n}.attemptResetInChild`,
);
t.equal(odometer.read(), 5, `${n} not reset`);
t.is(odometer.read(), 5, `${n} not reset`);
odometer.reset();

const fakeCalled = c.evaluate(attemptResetByShadow)(doAdd);
t.notOk(fakeCalled, `${n}.attemptResetByShadow`);
t.equal(odometer.read(), 1, `${n} called anyway`);
t.falsy(fakeCalled, `${n}.attemptResetByShadow`);
t.is(odometer.read(), 1, `${n} called anyway`);
odometer.reset();

odometer.add(5);
t.throws(
() => c.evaluate(attemptResetByTransform)(),
/forbidden access/,
{ message: /forbidden access/ },
`${n}.attemptResetByTransform`,
);
t.equal(odometer.read(), 5, `${n} not reset`);
t.is(odometer.read(), 5, `${n} not reset`);
odometer.reset();

t.equal(
c.evaluate('Compartment.name'),
'Compartment',
`${n}.Compartment.name`,
);
t.is(c.evaluate('Compartment.name'), 'Compartment', `${n}.Compartment.name`);

t.ok(c instanceof Compartment, `${n} instanceof`);
t.truthy(c instanceof Compartment, `${n} instanceof`);

const Con = Object.getPrototypeOf(c.globalThis.Compartment).constructor;
t.throws(() => new Con(), /Not available/, `${n} .constructor is tamed`);
t.throws(
() => new Con(),
{ message: /Not available/ },
`${n} .constructor is tamed`,
);
}

test('wrap', t => {
Expand All @@ -145,6 +143,4 @@ test('wrap', t => {

const c3 = c2.evaluate(createChild)();
check(t, c3, odometer, 'c3');

t.end();
});
89 changes: 38 additions & 51 deletions packages/import-bundle/test/test-import-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import '@agoric/install-ses';

import bundleSource from '@agoric/bundle-source';
import tap from 'tap';
import test from 'ava';
import { importBundle } from '../src/index.js';

function transform1(src) {
Expand All @@ -11,48 +11,44 @@ function transform1(src) {
.replace('two foot wide', 'sixty feet wide');
}

async function testBundle1(b1, mode, ew) {
async function testBundle1(t, b1, mode, ew) {
const ns1 = await importBundle(b1, { endowments: ew });
tap.equal(ns1.f1(1), 2, `ns1.f1 ${mode} ok`);
tap.equal(ns1.f2(1), 3, `ns1.f2 ${mode} ok`);
t.is(ns1.f1(1), 2, `ns1.f1 ${mode} ok`);
t.is(ns1.f2(1), 3, `ns1.f2 ${mode} ok`);

const endowments = { endow1: 3, ...ew };
const ns2 = await importBundle(b1, { endowments });
tap.equal(ns2.f3(1), 4, `ns2.f1 ${mode} ok`);
t.is(ns2.f3(1), 4, `ns2.f1 ${mode} ok`);
// untransformed
tap.equal(
ns2.f4('is unreplaced'),
'replaceme is unreplaced',
`ns2.f4 ${mode} ok`,
);
tap.equal(
t.is(ns2.f4('is unreplaced'), 'replaceme is unreplaced', `ns2.f4 ${mode} ok`);
t.is(
ns2.f5('the bed'),
'Mr. Lambert says the bed is two foot wide',
`ns2.f5 ${mode} ok`,
);

const ns3 = await importBundle(b1, { endowments, transforms: [transform1] });
tap.equal(ns3.f4('is ok'), 'substitution is ok', `ns3.f4 ${mode} ok`);
tap.equal(
t.is(ns3.f4('is ok'), 'substitution is ok', `ns3.f4 ${mode} ok`);
t.is(
ns3.f5('the bed'),
'Mr. Lambert says the bed is sixty feet wide',
`ns3.f5 ${mode} ok`,
);

const endowments4 = { sneakyChannel: 3, ...ew };
const ns4 = await importBundle(b1, { endowments: endowments4 });
tap.equal(ns4.f6ReadGlobal(), 3, `ns3.f6 ${mode} ok`);
tap.equal(ns4.f8ReadGlobalSubmodule(), 3, `ns3.f8 ${mode} ok`);
tap.throws(
t.is(ns4.f6ReadGlobal(), 3, `ns3.f6 ${mode} ok`);
t.is(ns4.f8ReadGlobalSubmodule(), 3, `ns3.f8 ${mode} ok`);
t.throws(
() => ns4.f7WriteGlobal(5),
'Cannot assign to read only property',
{ message: /Cannot assign to read only property/ },
`ns4.f7 ${mode} ok`,
);
tap.equal(ns4.f6ReadGlobal(), 3, `ns4.f6 ${mode} ok`);
tap.equal(ns4.f8ReadGlobalSubmodule(), 3, `ns3.f8 ${mode} ok`);
t.is(ns4.f6ReadGlobal(), 3, `ns4.f6 ${mode} ok`);
t.is(ns4.f8ReadGlobalSubmodule(), 3, `ns3.f8 ${mode} ok`);
}

tap.test('test import', async function testImport(t) {
test('test import', async function testImport(t) {
// nestedEvaluate requires a 'require' endowment, but doesn't call it
function req(what) {
console.log(`require(${what})`);
Expand All @@ -69,19 +65,17 @@ tap.test('test import', async function testImport(t) {
require.resolve('./bundle1.js'),
'getExport',
);
await testBundle1(b1getExport, 'getExport', endowments);
await testBundle1(t, b1getExport, 'getExport', endowments);
}

const b1NestedEvaluate = await bundleSource(
require.resolve('./bundle1.js'),
'nestedEvaluate',
);
await testBundle1(b1NestedEvaluate, 'nestedEvaluate', endowments);

t.end();
await testBundle1(t, b1NestedEvaluate, 'nestedEvaluate', endowments);
});

tap.test('test missing sourceMap', async function testImport(t) {
test('test missing sourceMap', async function testImport(t) {
function req(what) {
console.log(`require(${what})`);
}
Expand All @@ -94,11 +88,10 @@ tap.test('test missing sourceMap', async function testImport(t) {
);
delete b1.sourceMap;
const ns1 = await importBundle(b1, { endowments });
tap.equal(ns1.f1(1), 2, `missing sourceMap ns.f1 ok`);
t.end();
t.is(ns1.f1(1), 2, `missing sourceMap ns.f1 ok`);
});

tap.test('inescapable transforms', async function testInescapableTransforms(t) {
test('inescapable transforms', async function testInescapableTransforms(t) {
const b1 = await bundleSource(
require.resolve('./bundle1.js'),
'nestedEvaluate',
Expand All @@ -113,29 +106,23 @@ tap.test('inescapable transforms', async function testInescapableTransforms(t) {
endowments,
inescapableTransforms: [transform1],
});
tap.equal(ns.f4('is ok'), 'substitution is ok', `iT ns.f4 ok`);
t.end();
t.is(ns.f4('is ok'), 'substitution is ok', `iT ns.f4 ok`);
});

tap.test(
'inescapable globalLexicals',
async function testInescapableGlobalLexicals(t) {
const b1 = await bundleSource(
require.resolve('./bundle1.js'),
'nestedEvaluate',
);
function req(what) {
console.log(`require(${what})`);
}
harden(Object.getPrototypeOf(console));
const endowments = { require: req, console };

const ns = await importBundle(b1, {
endowments,
inescapableGlobalLexicals: { endow1: 3 },
});
tap.equal(ns.f3(1), 4, `iGL ns.f3 ok`);
test('inescapable globalLexicals', async function testInescapableGlobalLexicals(t) {
const b1 = await bundleSource(
require.resolve('./bundle1.js'),
'nestedEvaluate',
);
function req(what) {
console.log(`require(${what})`);
}
harden(Object.getPrototypeOf(console));
const endowments = { require: req, console };

t.end();
},
);
const ns = await importBundle(b1, {
endowments,
inescapableGlobalLexicals: { endow1: 3 },
});
t.is(ns.f3(1), 4, `iGL ns.f3 ok`);
});

0 comments on commit 15c233e

Please sign in to comment.