From 3d1e11fe8126719c78f20866a7175069871567d0 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 7 Dec 2015 01:44:33 +0100 Subject: [PATCH] Support a BROWSER_TESTS env variable to build tests for browsers --- dist-build/emscripten.sh | 56 ++++++++++++++++++++++++++++------------ test/default/cmptest.h | 23 ++++++++++++++++- test/default/pre.js.inc | 20 +++++++++----- 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/dist-build/emscripten.sh b/dist-build/emscripten.sh index e2e2a8a7..43466eb8 100755 --- a/dist-build/emscripten.sh +++ b/dist-build/emscripten.sh @@ -9,11 +9,15 @@ export LDFLAGS="-s TOTAL_MEMORY=${TOTAL_MEMORY} -s RESERVED_FUNCTION_POINTERS=8 rm -f test/js.done +if [ "x$BROWSER_TESTS" != "x" ]; then + echo "Tests will be built to be run in a web browser" +fi + emconfigure ./configure --enable-minimal --disable-shared --prefix="$PREFIX" \ CFLAGS="-O3" && \ emmake make clean && \ emmake make $MAKE_FLAGS install V=1 && \ -emcc -O3 --llvm-lto 1 --memory-init-file 0 $LDFLAGS $JS_EXPORTS_FLAGS \ +emcc -O3 --llvm-lto 1 --memory-init-file 0 $CPPFLAGS $LDFLAGS $JS_EXPORTS_FLAGS \ "${PREFIX}/lib/libsodium.a" -o "${PREFIX}/lib/libsodium.js" || exit 1 if test "x$NODE" = x; then @@ -27,25 +31,45 @@ if test "x$NODE" = x; then fi if test "x$NODE" = x; then - echo 'node.js not found - test suite skipped.' >&2 + echo 'node.js not found - test suite skipped' >&2 exit 1 fi -echo "Using [${NODE}] as a Javascript runtime." +echo "Using [${NODE}] as a Javascript runtime" -echo 'Compiling the test suite...' && \ -emmake make $MAKE_FLAGS check > /dev/null 2>&1 +if [ "x$BROWSER_TESTS" != "x" ]; then + echo 'Compiling the test suite for browsers...' && \ + emmake make $MAKE_FLAGS CPPFLAGS="$CPPFLAGS -DBROWSER_TESTS=1" check \ + > /dev/null 2>&1 +else + echo 'Compiling the test suite...' && \ + emmake make $MAKE_FLAGS check > /dev/null 2>&1 +fi + +if [ "x$BROWSER_TESTS" != "x" ]; then + echo 'Creating the test suite for browsers' + ( + cd test/default && \ + mkdir -p browser && \ + for file in *.js; do + fgrep -v "#! /usr/bin/env {NODE}" "$file" > "browser/${file}.tmp" + chmod -x "browser/${file}.tmp" + mv -f "browser/${file}.tmp" "browser/${file}" + done + ) +else + echo 'Running the test suite' + ( + cd test/default && \ + for file in *.js; do + echo "#! /usr/bin/env ${NODE}" > "${file}.tmp" + fgrep -v "#! /usr/bin/env {NODE}" "$file" >> "${file}.tmp" + chmod +x "${file}.tmp" + mv -f "${file}.tmp" "$file" + done + ) + make $MAKE_FLAGS check || exit 1 +fi -echo 'Running the test suite.' -( - cd test/default && \ - for file in *.js; do - echo "#! /usr/bin/env ${NODE}" > "${file}.tmp" - fgrep -v "#! /usr/bin/env {NODE}" "$file" >> "${file}.tmp" - chmod +x "${file}.tmp" - mv -f "${file}.tmp" "$file" - done -) -make $MAKE_FLAGS check || exit 1 echo 'Done.' touch -r "${PREFIX}/lib/libsodium.js" test/js.done diff --git a/test/default/cmptest.h b/test/default/cmptest.h index ee849790..24f5ff2a 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -27,8 +27,11 @@ # define rand(X) arc4random(X) #endif +int xmain(void); + +#ifndef BROWSER_TESTS + FILE *fp_res; -int xmain(void); int main(void) { @@ -61,6 +64,24 @@ int main(void) #undef printf #define printf(...) fprintf(fp_res, __VA_ARGS__) + +#else + +int main(void) +{ + if (sodium_init() != 0) { + return 99; + } + if (xmain() != 0) { + return 99; + } + printf("--- SUCCESS ---\n"); + + return 0; +} + +#endif + #define main xmain #endif diff --git a/test/default/pre.js.inc b/test/default/pre.js.inc index 428f475d..130c1371 100755 --- a/test/default/pre.js.inc +++ b/test/default/pre.js.inc @@ -4,9 +4,17 @@ try { } catch(e) { this['Module'] = Module = {}; } -Module['preRun'] = Module['preRun'] || []; -Module['preRun'].push(function(){ - FS.init(); - FS.mkdir('/test-data'); - FS.mount(NODEFS, { root: '.' }, '/test-data'); -}); +if (typeof process === 'object') { + Module['preRun'] = Module['preRun'] || []; + Module['preRun'].push(function() { + FS.init(); + FS.mkdir('/test-data'); + FS.mount(NODEFS, { root: '.' }, '/test-data'); + }); +} else { + Module['print'] = function(x) { + var event = new Event('test-output'); + event.data = x; + window.dispatchEvent(event); + }; +}