From d0a0695fb5d34996850723f7d4b1b59c3df909c2 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 3 Nov 2016 00:47:22 +0100 Subject: test harness for both browser and node --- .gitignore | 8 ++- lib/emscripten/taler-emscripten-lib.d.ts | 4 +- lib/wallet/cryptoApi-test.ts | 6 ++ lib/wallet/cryptoWorker.ts | 4 +- lib/wallet/emscriptif-test.ts | 11 ++++ lib/wallet/emscriptif.ts | 6 +- package.json | 1 + scripts/prove-node | 2 +- scripts/prove-selenium | 3 + testlib/node/runtime.js | 70 ++++++++++++++++++++++ testlib/selenium/runtime.js | 100 +++++++++++++++++++++++++++++++ testlib/selenium/testhost.html | 19 ++++++ testlib/talertest.ts | 1 + testlib/testruntime.js | 70 ---------------------- 14 files changed, 226 insertions(+), 79 deletions(-) create mode 100644 lib/wallet/cryptoApi-test.ts create mode 100644 lib/wallet/emscriptif-test.ts create mode 100755 scripts/prove-selenium create mode 100644 testlib/node/runtime.js create mode 100644 testlib/selenium/runtime.js create mode 100644 testlib/selenium/testhost.html delete mode 100644 testlib/testruntime.js diff --git a/.gitignore b/.gitignore index 95fb987d3..8563e66cd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ pages/**/*.js.map pogen/**/*.js.map test/**/*.js.map content_scripts/**/*.js.map +testlib/**/*.js.map +testlib/talertest.js background/*.js pogen/*.js test/*.js @@ -20,4 +22,8 @@ build/ taler-wallet-*.tar.gz taler-wallet-*.directive taler-wallet-*.directive.asc -taler-wallet-*.sig \ No newline at end of file +taler-wallet-*.sig + +# Even though node_modules are tracked in git, +# per default we don't want them to show up in git status +node_modules diff --git a/lib/emscripten/taler-emscripten-lib.d.ts b/lib/emscripten/taler-emscripten-lib.d.ts index 0b180781a..97821d9ef 100644 --- a/lib/emscripten/taler-emscripten-lib.d.ts +++ b/lib/emscripten/taler-emscripten-lib.d.ts @@ -51,4 +51,6 @@ export declare namespace Module { function writeStringToMemory(s: string, buffer: number, dontAddNull?: boolean): void; -} \ No newline at end of file +} + +export default Module; diff --git a/lib/wallet/cryptoApi-test.ts b/lib/wallet/cryptoApi-test.ts new file mode 100644 index 000000000..6399367b1 --- /dev/null +++ b/lib/wallet/cryptoApi-test.ts @@ -0,0 +1,6 @@ +import {CryptoApi} from "./cryptoApi"; +import {test, TestLib} from "testlib/talertest"; + +test("string hashing", (t: TestLib) => { + +}); diff --git a/lib/wallet/cryptoWorker.ts b/lib/wallet/cryptoWorker.ts index 22feb8eb7..0689c910e 100644 --- a/lib/wallet/cryptoWorker.ts +++ b/lib/wallet/cryptoWorker.ts @@ -45,7 +45,7 @@ if ("object" !== typeof Module) { // we can use it from TypeScript by importing it. { - let mod = System.newModule({Module: Module}); + let mod = System.newModule({Module: Module, default: Module}); let modName = System.normalizeSync("../emscripten/taler-emscripten-lib"); console.log("registering", modName); System.set(modName, mod); @@ -58,4 +58,4 @@ System.import("./cryptoLib") .catch((e) => { console.log("crypto worker failed"); console.error(e.stack); - }); \ No newline at end of file + }); diff --git a/lib/wallet/emscriptif-test.ts b/lib/wallet/emscriptif-test.ts new file mode 100644 index 000000000..a3a9229bb --- /dev/null +++ b/lib/wallet/emscriptif-test.ts @@ -0,0 +1,11 @@ +import {test, TestLib} from "testlib/talertest"; +import * as native from "./emscriptif"; + +test("string hashing", (t: TestLib) => { + let x = native.ByteArray.fromStringWithNull("hello taler"); + let h = "8RDMADB3YNF3QZBS3V467YZVJAMC2QAQX0TZGVZ6Q5PFRRAJFT70HHN0QF661QR9QWKYMMC7YEMPD679D2RADXCYK8Y669A2A5MKQFR" + let hc = x.hash().toCrock(); + console.log(`# hc ${hc}`); + t.assert(h === hc, "must equal"); + t.pass(); +}); diff --git a/lib/wallet/emscriptif.ts b/lib/wallet/emscriptif.ts index aad5a12a2..3ae2db72a 100644 --- a/lib/wallet/emscriptif.ts +++ b/lib/wallet/emscriptif.ts @@ -15,7 +15,7 @@ */ import {AmountJson} from "./types"; -import * as EmscWrapper from "../emscripten/taler-emscripten-lib"; +import Module, {EmscFunGen} from "../emscripten/taler-emscripten-lib"; /** * High-level interface to emscripten-compiled modules used @@ -34,14 +34,12 @@ const GNUNET_YES = 1; const GNUNET_NO = 0; const GNUNET_SYSERR = -1; -let Module = EmscWrapper.Module; - function myCcall(name: string, ret: any, argTypes: any[], args: any[]) { return Module.ccall(name, ret, argTypes, args); } -let getEmsc: EmscWrapper.EmscFunGen = (name: string, ret: any, +let getEmsc: EmscFunGen = (name: string, ret: any, argTypes: any[]) => { return (...args: any[]) => { return myCcall(name, ret, argTypes, args); diff --git a/package.json b/package.json index 406a15858..cd08e3196 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "map-stream": "0.0.6", "mocha": "^2.4.5", "po2json": "git+https://github.com/mikeedwards/po2json", + "selenium-webdriver": "^3.0.0-beta-3", "systemjs": "^0.19.14", "through2": "^2.0.1", "typescript": "^2.0.3", diff --git a/scripts/prove-node b/scripts/prove-node index de9c8931a..ca8405324 100755 --- a/scripts/prove-node +++ b/scripts/prove-node @@ -1,3 +1,3 @@ #!/usr/bin/env bash -exec prove -e "node ./testlib/testruntime.js" "$@" +exec prove -e "node ./testlib/node/runtime.js" "$@" diff --git a/scripts/prove-selenium b/scripts/prove-selenium new file mode 100755 index 000000000..d2bd957a5 --- /dev/null +++ b/scripts/prove-selenium @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +exec prove -e "node ./testlib/selenium/runtime.js" "$@" diff --git a/testlib/node/runtime.js b/testlib/node/runtime.js new file mode 100644 index 000000000..29ee508cc --- /dev/null +++ b/testlib/node/runtime.js @@ -0,0 +1,70 @@ +/* + This file is part of TALER + (C) 2016 Inria + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +/** + * + * @author Florian Dold + */ + + +"use strict"; + +let vm = require("vm"); +let fs = require("fs"); +let process = require("process"); + +let emsc = require("../../lib/emscripten/taler-emscripten-lib.js"); + +// Do it here, since it breaks 'require'' for libwrapper +let System = require("systemjs"); + + +// When instrumenting code with istanbul, +// automatic module type detection fails, +// thus we specify it here manually. +System.config({ + defaultJSExtensions: true, + //meta: { + // './test/tests/taler.js': { + // format: 'register' + // }, + // './lib/wallet/*': { + // format: 'register' + // } + //} +}); + +console.log("TAP version 13"); + +let mod = System.newModule({Module: emsc, default: emsc}); +let modName = System.normalizeSync(__dirname + "/../../lib/emscripten/taler-emscripten-lib.js"); +System.set(modName, mod); + + +let testName = process.argv[2]; +System.import("testlib/talertest") + .then(tt => { + SystemJS.import(testName) + .then(() => { + return tt.run(); + }) + .catch((e) => console.error(e)); + }) + .catch((e) => { + console.error("can't locate talertest"); + console.error(e); + }); + diff --git a/testlib/selenium/runtime.js b/testlib/selenium/runtime.js new file mode 100644 index 000000000..0cf6c6d18 --- /dev/null +++ b/testlib/selenium/runtime.js @@ -0,0 +1,100 @@ +/* + This file is part of TALER + (C) 2016 Inria + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +/** + * + * @author Florian Dold + */ + +"use strict"; + +var webdriver = require('selenium-webdriver'); +var chrome = require('selenium-webdriver/chrome'); +var path = require("path"); +var process = require("process"); + +var p = "file://" + __dirname + "/testhost.html"; + +if (!process.argv[2]) { + console.log("no test script given"); + process.exit(1); +} + +var testScript = path.resolve(process.argv[2]); + +var script = ` + function f() { + System.import("testlib/talertest") + .then(tt => { + SystemJS.import("file://${testScript}") + .then(() => { + return tt.run(); + }) + .then(() => { + window.__test_over = true; + }) + .catch((e) => { + window.__test_over = true; + console.error(e) + }); + }) + .catch((e) => { + console.error("can't locate talertest"); + console.error(e); + }); + } + if (document.readyState == "complete") { + f(); + } else { + document.addEventListener("DOMContentLoaded", f); + } +`; + +function untilTestOver() { + return driver.executeScript("return window.__test_over"); +} + +console.log("TAP version 13"); + +var driver = new webdriver.Builder() + .setLoggingPrefs({browser: 'ALL'}) + .forBrowser('chrome') + .build(); + +driver.get(p); +driver.executeScript(script); +driver.wait(untilTestOver); + +driver.manage().logs().get("browser").then((logs) => { + for (let l of logs) { + if (l.level.name != "INFO") { + continue; + } + if (l.message.startsWith("{")) { + // format not understood, sometimes messages are logged + // with more structure, just pass it on + console.log(l.message); + continue; + } + let s1 = l.message.indexOf(" ") + 1; + let s2 = l.message.indexOf(" ", s1) + 1; + // Skip file url and LINE:COL + console.log(l.message.substring(s2)); + } +}); + +driver.quit(); + diff --git a/testlib/selenium/testhost.html b/testlib/selenium/testhost.html new file mode 100644 index 000000000..263fe6316 --- /dev/null +++ b/testlib/selenium/testhost.html @@ -0,0 +1,19 @@ + + + + Browser Test Host + + + + diff --git a/testlib/talertest.ts b/testlib/talertest.ts index 5dcaff457..14074620b 100644 --- a/testlib/talertest.ts +++ b/testlib/talertest.ts @@ -82,6 +82,7 @@ export async function run() { await p; console.log(`ok ${Number(i) + 1} ${lastMsg}`); } catch (e) { + console.error(e); console.log(`not ok ${Number(i) + 1} ${lastMsg}`); } } diff --git a/testlib/testruntime.js b/testlib/testruntime.js deleted file mode 100644 index 236807265..000000000 --- a/testlib/testruntime.js +++ /dev/null @@ -1,70 +0,0 @@ -/* - This file is part of TALER - (C) 2016 Inria - - TALER is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - TALER is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with - TALER; see the file COPYING. If not, see - */ - -/** - * - * @author Florian Dold - */ - - -"use strict"; - -let vm = require("vm"); -let fs = require("fs"); -let process = require("process"); - -let emsc = require("../lib/emscripten/taler-emscripten-lib.js"); - -// Do it here, since it breaks 'require'' for libwrapper -let System = require("systemjs"); - - -// When instrumenting code with istanbul, -// automatic module type detection fails, -// thus we specify it here manually. -System.config({ - defaultJSExtensions: true, - //meta: { - // './test/tests/taler.js': { - // format: 'register' - // }, - // './lib/wallet/*': { - // format: 'register' - // } - //} -}); - -console.log("TAP version 13"); - -let mod = System.newModule({Module: emsc}); -let modName = System.normalizeSync(__dirname + "/../lib/emscripten/taler-emscripten-lib.js"); -System.set(modName, mod); - - -let testName = process.argv[2]; -System.import("./testlib/talertest") - .then(tt => { - SystemJS.import(testName) - .then(() => { - return tt.run(); - }) - .catch((e) => console.error(e)); - }) - .catch((e) => { - console.error("can't locate talertest"); - console.error(e); - }); - -- cgit v1.2.3