Skip to content

Commit

Permalink
Browser testcases work again.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Nov 23, 2019
1 parent 3d75c52 commit c11c2e2
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 131 deletions.
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
"auto-build": "node ./admin/cmds/reset-build.js && npm run build -- -w",
"bootstrap": "node ./admin/cmds/reset-build.js && node ./admin/cmds/update-depgraph && lerna bootstrap --hoist",
"build": "tsc --build ./tsconfig.project.json",
"build-cjs": "node ./admin/cmds/set-option cjs && npm run build",
"build-esm": "node ./admin/cmds/set-option esm && npm run build",
"build-all": "npm run build-esm && npm run build-cjs",
"clean": "node ./admin/cmds/reset-build.js && tsc --build --clean ./tsconfig.project.json",
"_dist_prepare": "npm run clean && npm run bootstrap && npm run build && node ./admin/cmds/update-exports.js",
"_dist_ethers": "rollup -c && rollup -c --configMinify && rollup -c --configModule && rollup -c --configModule --configMinify",
"_dist_ethers": "rollup -c && rollup -c --configMinify && rollup -c --configModule && rollup -c --configModule --configMinify && rollup -c rollup-wordlists.config.js",
"_dist_tests": "rollup -c --configTest && rollup -c --configTest --configMinify && rollup -c --configTest --configModule && rollup -c --configTest --configModule --configMinify",
"_test_prepare": "npm run _dist_prepare && npm run _dist_tests",
"_test_node": "cd packages/tests && mocha --no-colors --reporter ./reporter ./lib/test-*.js",
Expand All @@ -25,6 +28,12 @@
"old-test-aion-node": "cd packages/aion-tests && mocha --no-colors --reporter ../tests/tests/reporter ./tests/test-*.js",
"update-versions": "npm run _dist_prepare && node ./admin/cmds/update-versions",
"publish-all": "node ./admin/cmds/publish",
"_distMinLangAll": "node ./admin/cmds/set-option.js browser-lang-all && rollup -c --configMinify && rollup -c --configMinify --configModule && mv ./packages/ethers/dist/ethers.umd.min.js ./packages/ethers/dist/ethers-all.umd.min.js && mv ./packages/ethers/dist/ethers.esm.min.js ./packages/ethers/dist/ethers-all.esm.min.js",
"_distMinLangEn": "node ./admin/cmds/set-option.js browser-lang-en && rollup -c --configMinify && rollup -c --configMinify --configModule",
"_dist": "npm run build-all && npm run _distMinLangAll && npm run _distMinLangEn && rollup -c && rollup -c --configModule",
"_distTestBrowserEsm": "rollup -c rollup-tests.config.js --configModule",
"_distTestBrowserUmd": "rollup -c rollup-tests.config.js",
"_distTestBrowser": "npm run _distTestBrowserEsm && npm run _distTestBrowserUmd",
"lock-versions": "node ./admin/cmds/lock-versions",
"build-docs": "flatworm docs.wrm docs",
"sync-github": "node ./admin/cmds/cache-github"
Expand All @@ -41,12 +50,14 @@
"libnpm": "2.0.1",
"mocha": "^5.2.0",
"mocha-phantomjs-core": "2.1.2",
"scrypt-js": "2.0.4",
"scrypt-js": "3.0.0",
"semver": "^5.6.0",
"tar": "4.4.8",
"typescript": "3.6.2",
"rollup": "1.20.1",
"rollup-plugin-node-builtins": "2.1.2",
"rollup-plugin-commonjs": "10.0.2",
"rollup-plugin-node-globals": "1.4.0",
"rollup-plugin-json": "4.0.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-terser": "5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/testcases/src.ts/browser-fs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "dirs": [ "./easyseed-bip39", "./testcases", "./wordlists" ] }
{ "dirs": [ "./input/easyseed-bip39", "./testcases", "./input/wordlists" ] }
12 changes: 9 additions & 3 deletions packages/testcases/src.ts/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import path from 'path';

import zlib from "browserify-zlib";

import * as data from "./browser-fs.json";
import { randomBytes, randomHexString, randomNumber } from "./random";
export { randomBytes, randomHexString, randomNumber };

import * as data from "./browser-fs.json";

const Cache: { [ filename: string ]: any } = { };
export function loadTests(tag: string): any {
let filename = 'testcases/' + tag + ".json.gz";
console.log("loading:", filename);
return JSON.parse(zlib.gunzipSync(new Buffer((<any>data)[filename], "base64")).toString());
if (Cache[filename] == null) {
console.log("loading:", filename);
Cache[filename] = JSON.parse(zlib.gunzipSync(new Buffer((<any>data)[filename], "base64")).toString());
}
return Cache[filename];
}

export function loadData(filename: string): Buffer {
Expand Down
30 changes: 2 additions & 28 deletions packages/testcases/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import fs from 'fs';
import path from 'path';
import zlib from 'browserify-zlib';

import { arrayify, concat, hexlify } from "@ethersproject/bytes";
import { keccak256 } from "@ethersproject/keccak256";
import { toUtf8Bytes } from "@ethersproject/strings";
import { randomBytes, randomHexString, randomNumber } from "./random";
export { randomBytes, randomHexString, randomNumber };

export module TestCase {
export type HDWalletNode = {
Expand Down Expand Up @@ -93,28 +92,3 @@ export function loadData(filename: string): Buffer {
return fs.readFileSync(path.resolve(__dirname, filename));
}

export function randomBytes(seed: string, lower: number, upper?: number): Uint8Array {
if (!upper) { upper = lower; }

if (upper === 0 && upper === lower) { return new Uint8Array(0); }

let result = arrayify(keccak256(toUtf8Bytes(seed)));
while (result.length < upper) {
result = concat([result, keccak256(result)]);
}

let top = arrayify(keccak256(result));
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;

return result.slice(0, lower + Math.floor((upper - lower) * percent));
}

export function randomHexString(seed: string, lower: number, upper?: number): string {
return hexlify(randomBytes(seed, lower, upper));
}

export function randomNumber(seed: string, lower: number, upper: number): number {
let top = randomBytes(seed, 3);
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
return lower + Math.floor((upper - lower) * percent);
}
29 changes: 29 additions & 0 deletions packages/testcases/src.ts/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

import { ethers } from "ethers";

export function randomBytes(seed: string, lower: number, upper?: number): Uint8Array {
if (!upper) { upper = lower; }

if (upper === 0 && upper === lower) { return new Uint8Array(0); }

let result = ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(seed)));
while (result.length < upper) {
result = ethers.utils.concat([result, ethers.utils.keccak256(result)]);
}

let top = ethers.utils.arrayify(ethers.utils.keccak256(result));
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;

return result.slice(0, lower + Math.floor((upper - lower) * percent));
}

export function randomHexString(seed: string, lower: number, upper?: number): string {
return ethers.utils.hexlify(randomBytes(seed, lower, upper));
}

export function randomNumber(seed: string, lower: number, upper: number): number {
let top = randomBytes(seed, 3);
let percent = ((top[0] << 16) | (top[1] << 8) | top[2]) / 0x01000000;
return lower + Math.floor((upper - lower) * percent);
}
9 changes: 0 additions & 9 deletions packages/tests/lib.esm/browser.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/tests/lib.esm/browser.js

This file was deleted.

9 changes: 0 additions & 9 deletions packages/tests/lib/browser.d.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/tests/lib/browser.js

This file was deleted.

27 changes: 12 additions & 15 deletions packages/tests/package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
{
"name": "@ethersproject/tests",
"version": "5.0.0-beta.142",
"description": "Testing package for ethers.",
"main": "./lib/index.js",
"author": "Richard Moore <me@ricmoo.com>",
"browser": {
"@ethersproject/ethers": "./tests/browser-ethers.js"
},
"scripts": {
"dist-phantomjs": "browserify -g @ethersproject/testcases/scripts/browser-fs.js -s tests ./tests/browser.js -o ./dist/tests.js",
"test": "if [ \"$RUN_PHANTOMJS\" = \"1\" ]; then npm run-script test-phantomjs; else npm run-script test-node; fi",
"test-node": "mocha --no-colors --reporter tests/reporter tests/test-*.js",
"test-phantomjs": "npm run dist-phantomjs && phantomjs --web-security=false ../../node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js ./test.html ./tests/reporter.js"
"ethers": "./lib/browser-ethers.js"
},
"dependencies": {
"@ethersproject/testcases": ">=5.0.0-beta.131",
"@types/mocha": "^5.2.0",
"ethers": ">=5.0.0-beta.156",
"mocha-phantomjs-core": "2.1.2"
},
"description": "Testing package for ethers.",
"devDependencies": {
"browserify": "16.2.3",
"mocha": "^5.2.0",
"web3-providers-http": "1.0.0-beta.35"
},
"ethereum": "donations.ethers.eth",
"keywords": [
"Ethereum",
"ethers"
],
"author": "Richard Moore <me@ricmoo.com>",
"license": "MIT",
"main": "./lib/index.js",
"module": "./lib.esm/index.js",
"name": "@ethersproject/tests",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git://github.com/ethers-io/ethers.js.git"
},
"module": "./lib.esm/index.js",
"scripts": {
"test": "exit 1"
},
"tarballHash": "0xe35d27339acf100689bdafbc29234a5e2469164ae6badd271dab620931d2c2d0",
"types": "./lib/index.d.ts",
"ethereum": "donations.ethers.eth",
"tarballHash": "0x016eddde4135966b3c9a3343aa29579816661d677a01dbc3933db9ee392de4e5"
"version": "5.0.0-beta.143"
}
7 changes: 5 additions & 2 deletions packages/tests/src.ts/browser-ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

console.log("Using global.ethers");

module.exports = (<any>global).ethers;
console.log("FOO");
const anyGlobal = (window as any);

const ethers = anyGlobal.ethers;

export { ethers }
23 changes: 0 additions & 23 deletions packages/tests/src.ts/browser.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/tests/src.ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

import "./test-account";
import "./test-contract";
import "./test-contract-interface";
import "./test-hdnode";
import "./test-providers";
import "./test-utils";
import "./test-wallet";
import "./test-wordlists";

import { Reporter } from "./reporter";

export { Reporter }
Loading

0 comments on commit c11c2e2

Please sign in to comment.