From 7a16a1d65c7b267a0742a7bad4ac46024b95945a Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 13 Mar 2016 13:37:58 +0000 Subject: [PATCH] I think this fixes tests, and makes them runnable from continuous integration. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3161 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/Task.cpp | 5 +- src/Task.h | 2 +- src/TaskTryCatch.cpp | 5 ++ src/TaskTryCatch.h | 1 + src/main.cpp | 18 +++++-- tests/01-nop | 2 +- tests/02-valgrind | 2 +- tests/03-child | 2 +- tests/04-promise | 2 +- tests/05-promise-remote-throw | 2 +- tests/06-restartTask | 80 ----------------------------- tests/07-promise-remote-reject | 2 +- tests/08-database | 2 +- tests/09-this | 2 +- tools/run-tests | 94 +++++++++++++++++++++++----------- 15 files changed, 97 insertions(+), 124 deletions(-) delete mode 100755 tests/06-restartTask diff --git a/src/Task.cpp b/src/Task.cpp index e39c3055..91cf712a 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -257,7 +257,8 @@ void Task::kill() { } } -void Task::execute(const char* fileName) { +bool Task::execute(const char* fileName) { + bool executed = false; v8::Isolate::Scope isolateScope(_isolate); v8::HandleScope handleScope(_isolate); v8::Context::Scope contextScope(v8::Local::New(_isolate, _context)); @@ -274,6 +275,7 @@ void Task::execute(const char* fileName) { if (!script.IsEmpty()) { script->Run(); std::cout << "Script " << fileName << " completed\n"; + executed = true; } else { std::cerr << "Failed to compile: " << fileName << ".\n"; } @@ -283,6 +285,7 @@ void Task::execute(const char* fileName) { message += fileName; _isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(_isolate, message.c_str()))); } + return executed; } void Task::invokeExport(const v8::FunctionCallbackInfo& args) { diff --git a/src/Task.h b/src/Task.h index d21d0bee..8bb7b486 100644 --- a/src/Task.h +++ b/src/Task.h @@ -74,7 +74,7 @@ public: void configureFromStdin(); void setTrusted(bool trusted) { _trusted = trusted; } - void execute(const char* fileName); + bool execute(const char* fileName); void activate(); void run(); diff --git a/src/TaskTryCatch.cpp b/src/TaskTryCatch.cpp index 03d59df4..0e23a3fc 100644 --- a/src/TaskTryCatch.cpp +++ b/src/TaskTryCatch.cpp @@ -55,3 +55,8 @@ TaskTryCatch::~TaskTryCatch() { } } } + +bool TaskTryCatch::hasCaught() +{ + return _tryCatch.HasCaught(); +} diff --git a/src/TaskTryCatch.h b/src/TaskTryCatch.h index 41bdd1e7..15e4d4bf 100644 --- a/src/TaskTryCatch.h +++ b/src/TaskTryCatch.h @@ -9,6 +9,7 @@ class TaskTryCatch { public: TaskTryCatch(Task* task); ~TaskTryCatch(); + bool hasCaught(); private: v8::TryCatch _tryCatch; diff --git a/src/main.cpp b/src/main.cpp index 128223d9..3f05b48d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ v8::Platform* gPlatform = 0; int main(int argc, char* argv[]) { + int result = 0; uv_setup_args(argc, argv); TaskStub::initialize(); v8::V8::InitializeICU(); @@ -64,12 +65,23 @@ int main(int argc, char* argv[]) { v8::HandleScope handleScope(task.getIsolate()); v8::Context::Scope contextScope(task.getContext()); TaskTryCatch tryCatch(&task); - task.execute(coreTask); + if (!task.execute(coreTask)) + { + result = -1; + } + if (tryCatch.hasCaught()) + { + result = -2; + } + } + + if (result == 0) + { + task.run(); } - task.run(); } v8::V8::Dispose(); - return 0; + return result; } diff --git a/tests/01-nop b/tests/01-nop index f929cc49..3ee6e5ca 100755 --- a/tests/01-nop +++ b/tests/01-nop @@ -4,4 +4,4 @@ cat > test.js << EOF print("hi"); EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/02-valgrind b/tests/02-valgrind index 32744c56..64c2d6cf 100755 --- a/tests/02-valgrind +++ b/tests/02-valgrind @@ -4,4 +4,4 @@ cat > test.js << EOF print("hi"); EOF -valgrind --log-file=$LOGDIR/valgrind.log $SANDBOXOS test.js +valgrind --log-file=$LOGDIR/valgrind.log $TILDEFRIENDS test.js diff --git a/tests/03-child b/tests/03-child index 49472cbd..162ee9c9 100755 --- a/tests/03-child +++ b/tests/03-child @@ -16,4 +16,4 @@ print("I am the child process."); exit(0); EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/04-promise b/tests/04-promise index 33f55df7..3acd0dc2 100755 --- a/tests/04-promise +++ b/tests/04-promise @@ -24,4 +24,4 @@ exports = { } EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/05-promise-remote-throw b/tests/05-promise-remote-throw index ce674f94..73b23099 100755 --- a/tests/05-promise-remote-throw +++ b/tests/05-promise-remote-throw @@ -23,4 +23,4 @@ exports = { } EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/06-restartTask b/tests/06-restartTask deleted file mode 100755 index 9bb344e0..00000000 --- a/tests/06-restartTask +++ /dev/null @@ -1,80 +0,0 @@ -#!/bin/bash - -mkdir -p packages -for i in filesystem packager; do - ln -s $ROOT/packages/$i packages/$i -done -cp -R $ROOT/packages/system packages/system - -mkdir -p packages/test - -cat > packages/test/test.js << EOF -print("Hello!"); - -File.writeFile("packages/hello/hello.js", "this will fail to run!$^!U#%^#$%#%"); - -var p = imports.system.restartTask("hello"); -print("here is our promise: " + p.toString()); -print(p); - -p.then(function(r) { - print("restart succeeded when it should not have: " + r); - imports.system.finishTest(1); -}).catch(function(e) { - print("restart failed: " + e); - print(e.toString()); - for (var i in e) { - print(i); - print(e[i]); - } - imports.system.finishTest(0); -}); -EOF - -cat > packages/test/package.json << EOF -{ - "name": "test", - "start": "test.js", - "trusted": true, - "imports": ["packager", "system"] -} -EOF - -cat >> packages/system/system.js << EOF -exports.finishTest = function(result) { - exit(result); -} -EOF - -mkdir -p packages/hello - -cat > packages/hello/hello.js << EOF -print("Hi."); -EOF - -cat > packages/hello/package.json << EOF -{ - "name": "hello", - "start": "hello.js" -} -EOF - - -mkdir -p packages/auth - -cat > packages/auth/auth.js << EOF -exports = { - query: function() { return null; }, - getCredentials: function() { return {user: 'test', token: 'token'}; }, - verifyCredentials: function() { return {permissions: []}; }, -}; -EOF - -cat > packages/auth/package.json << EOF -{ - "name": "auth", - "start": "auth.js" -} -EOF - -$SANDBOXOS packages/system/system.js diff --git a/tests/07-promise-remote-reject b/tests/07-promise-remote-reject index d958fe5a..dedac2fa 100755 --- a/tests/07-promise-remote-reject +++ b/tests/07-promise-remote-reject @@ -26,4 +26,4 @@ exports = { } EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/08-database b/tests/08-database index 907e19de..ffa331b9 100755 --- a/tests/08-database +++ b/tests/08-database @@ -31,4 +31,4 @@ if (expected.length) { } EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tests/09-this b/tests/09-this index 3d992b9f..610c5c40 100755 --- a/tests/09-this +++ b/tests/09-this @@ -6,4 +6,4 @@ task.activate.bind(null).apply(); exit(0); EOF -$SANDBOXOS test.js +$TILDEFRIENDS test.js diff --git a/tools/run-tests b/tools/run-tests index 40474175..482c7181 100755 --- a/tools/run-tests +++ b/tools/run-tests @@ -1,36 +1,68 @@ -#!/bin/bash +#!/usr/bin/python -export ROOT=$(cd $(dirname ${BASH_SOURCE[0]})/..; pwd) -TMP=$ROOT/tmp -LOGS=$ROOT/logs -TESTS=$ROOT/tests -export SANDBOXOS=$ROOT/sandboxos +import argparse +import glob +import os +import shutil +import subprocess +import sys -mkdir $TMP 2> /dev/null -mkdir $LOGS 2> /dev/null +if sys.platform != 'linux2': + print 'Tests are only enabled on Linux.' + exit(0) -REQUESTED_TESTS=$* -if [[ -z $REQUESTED_TESTS ]]; then - REQUESTED_TESTS=$(ls $TESTS/* | xargs basename -a) -fi +parser = argparse.ArgumentParser() +parser.add_argument('tests', nargs=argparse.REMAINDER) +arguments = parser.parse_args() -for NAME in $REQUESTED_TESTS; do - TEST=$TESTS/$NAME - echo -n "$(basename $TEST) " - rm -rf $TMP/* - pushd $TMP > /dev/null - export LOGDIR=$LOGS/$NAME - mkdir $LOGDIR 2> /dev/null - unbuffer $TEST > $LOGDIR/stdout.log 2> $LOGDIR/stderr.log - RESULT=$? - popd > /dev/null - if [[ $RESULT != 0 ]]; then - echo "FAILED (with exit code $RESULT)" - exit $RESULT - else - echo "SUCCESS" - fi -done +root = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(__file__))) +tmp = os.path.join(root, 'tmp') +logs = os.path.join(root, 'logs') +tests = os.path.join(root, 'tests') +executable = os.path.join(root, 'tildefriends') -echo -echo "All tests completed successfully." +if not os.path.isdir(logs): + os.makedirs(logs) + +selectedTests = set() +if not arguments.tests: + for test in glob.glob(os.path.join(tests, '*')): + selectedTests.add(test) +for pattern in arguments.tests: + for match in glob.glob(os.path.join(tests, '*' + pattern + '*')): + selectedTests.add(match) + +env = os.environ.copy() +env['TILDEFRIENDS'] = executable +env['LOGDIR'] = logs + +def indent(text): + return '\n'.join('\t' + line for line in text.split('\n')) + +passCount = 0 +failCount = 0 + +for test in selectedTests: + if os.path.isdir(tmp): + shutil.rmtree(tmp) + if not os.path.isdir(tmp): + os.makedirs(tmp) + + process = subprocess.Popen(['bash', test], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=tmp, env=env) + stdout, stderr = process.communicate() + if process.returncode == 0: + print 'PASSED', test + passCount += 1 + else: + print 'FAILED', test + print 'RETURNED:', process.returncode + print 'STDOUT:' + print indent(stdout) + print 'STDERR:' + print indent(stderr) + failCount += 1 + + if os.path.isdir(tmp): + shutil.rmtree(tmp) + +print passCount, 'tests passed. ', failCount, 'tests failed.'