From e6cc26c642c0bb9b37aa66e614362401c4705821 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Sun, 10 Aug 2014 15:52:37 +0200 Subject: Created unified system for running tests in travis for Piwik Core and any Piwik plugin. System includes a console command (generate:travis-yml) that generates a .travis.yml file for a plugin or core. The travis file will check in travis if it needs to be updated, and if so it will fail the build and tell the user. If a specific command line option is specified supplying a github user token, the .travis.yml file will be updated within travis automatically. Notes: - comments and non-travis sections will be preserved - includes logic for plugins to test themselves against latest stable version instead of just on master - the system is primarily controlled by environment variables. developers can change/add variables in .travis.yml and the auto-updating will preserve the changes. --- tests/PHPUnit/Core/ReleaseCheckListTest.php | 15 ++++-- tests/PHPUnit/travis-helper.sh | 57 --------------------- tests/PHPUnit/travis.sh | 63 ----------------------- tests/travis/autoupdate_travis_yml.sh | 56 +++++++++++++++++++++ tests/travis/configure_git.sh | 14 ++++++ tests/travis/initiate_ui_tests.sh | 3 -- tests/travis/travis-helper.sh | 78 +++++++++++++++++++++++++++++ tests/travis/travis.sh | 68 +++++++++++++++++++++++++ 8 files changed, 227 insertions(+), 127 deletions(-) delete mode 100644 tests/PHPUnit/travis-helper.sh delete mode 100755 tests/PHPUnit/travis.sh create mode 100755 tests/travis/autoupdate_travis_yml.sh create mode 100755 tests/travis/configure_git.sh create mode 100644 tests/travis/travis-helper.sh create mode 100755 tests/travis/travis.sh (limited to 'tests') diff --git a/tests/PHPUnit/Core/ReleaseCheckListTest.php b/tests/PHPUnit/Core/ReleaseCheckListTest.php index 0d8acb94e2..8177a63e36 100644 --- a/tests/PHPUnit/Core/ReleaseCheckListTest.php +++ b/tests/PHPUnit/Core/ReleaseCheckListTest.php @@ -1,14 +1,17 @@ assertFalse(strpos($content, $patternFailIfFound), 'found in ' . $file); } diff --git a/tests/PHPUnit/travis-helper.sh b/tests/PHPUnit/travis-helper.sh deleted file mode 100644 index 23e3943481..0000000000 --- a/tests/PHPUnit/travis-helper.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -# Sourced from https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/script/templates/header.sh -# + Tweaked to display output and not show the status line -travis_wait() { - local timeout=40 - local cmd="$@" - local log_file=travis_wait_$$.log - - $cmd & - local cmd_pid=$! - - travis_jigger $! $timeout $cmd & - local jigger_pid=$! - local result - - { - wait $cmd_pid 2>/dev/null - result=$? - ps -p$jigger_pid &>/dev/null && kill $jigger_pid - } || return 1 - - if [ $result -eq 0 ]; then -echo -e "\n${GREEN}The command \"$TRAVIS_CMD\" exited with $result.${RESET}" - else -echo -e "\n${RED}The command \"$TRAVIS_CMD\" exited with $result.${RESET}" - fi - -echo -e "\n${GREEN}Log:${RESET}\n" - cat $log_file - - return $result -} - -travis_jigger() { - # helper method for travis_wait() - local cmd_pid=$1 - shift -local timeout=40 - shift -local count=0 - - # clear the line - echo -e "\n" - - while [ $count -lt $timeout ]; do -count=$(($count + 1)) - #echo -ne "Still running ($count of $timeout): $@\r" - - # print invisible character - echo -ne "\xE2\x80\x8B" - sleep 60 - done - -echo -e "\n${RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${RESET}\n" - kill -9 $cmd_pid -} \ No newline at end of file diff --git a/tests/PHPUnit/travis.sh b/tests/PHPUnit/travis.sh deleted file mode 100755 index 0c6631b550..0000000000 --- a/tests/PHPUnit/travis.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/bin/bash - -# for travis_wait function -source travis-helper.sh - -if [ "$TEST_SUITE" != "UITests" ] && [ "$TEST_SUITE" != "AngularJSTests" ] -then - if [ `phpunit --group __nogroup__ | grep "No tests executed" | wc -l` -ne 1 ] - then - echo "=====> There are some tests functions which do not have a @group set. " - echo " Please add the @group phpdoc comment to the following tests: <=====" - phpunit --group __nogroup__ --testdox | grep "[x]" - exit 1 - fi -fi - -if [ -n "$TEST_SUITE" ] -then - echo "Executing tests in test suite $TEST_SUITE..." - - if [ -n "$PLUGIN_NAME" ] - then - echo " [ plugin name = $PLUGIN_NAME ]" - fi - - if [ "$TEST_SUITE" = "AngularJSTests" ] - then - sh ./../angularjs/scripts/travis.sh - elif [ "$TEST_SUITE" = "JavascriptTests" ] - then - touch ../javascript/enable_sqlite - phantomjs ../javascript/testrunner.js - elif [ "$TEST_SUITE" = "UITests" ] - then - if [ -n "$PLUGIN_NAME" ] - then - artifacts_folder="protected/ui-tests.master.$PLUGIN_NAME" - else - artifacts_folder="ui-tests.$TRAVIS_BRANCH" - fi - - echo "" - echo "View UI failures (if any) here http://builds-artifacts.piwik.org/$artifacts_folder/$TRAVIS_JOB_NUMBER/screenshot-diffs/diffviewer.html" - echo "If the new screenshots are valid, then you can copy them over to tests/PHPUnit/UI/expected-ui-screenshots/." - echo "" - - if [ -n "$PLUGIN_NAME" ] - then - phantomjs ../lib/screenshot-testing/run-tests.js --assume-artifacts --persist-fixture-data --screenshot-repo=$TRAVIS_REPO_SLUG --plugin=$PLUGIN_NAME - else - phantomjs ../lib/screenshot-testing/run-tests.js --store-in-ui-tests-repo --persist-fixture-data --assume-artifacts - fi - else - if [ -n "$PLUGIN_NAME" ] - then - travis_wait phpunit --configuration phpunit.xml --colors --testsuite $TEST_SUITE --group $PLUGIN_NAME - else - travis_wait phpunit --configuration phpunit.xml --testsuite $TEST_SUITE --colors - fi - fi -else - travis_wait phpunit --configuration phpunit.xml --coverage-text --colors -fi \ No newline at end of file diff --git a/tests/travis/autoupdate_travis_yml.sh b/tests/travis/autoupdate_travis_yml.sh new file mode 100755 index 0000000000..ae5b504eeb --- /dev/null +++ b/tests/travis/autoupdate_travis_yml.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# if we're on master, check if .travis.yml is out of date. if github token is supplied we will try to auto-update, +# otherwise we just print a message and exit. +GENERATE_TRAVIS_YML_COMMAND="$GENERATE_TRAVIS_YML_COMMAND --dump=./generated.travis.yml" +$GENERATE_TRAVIS_YML_COMMAND + +diff .travis.yml generated.travis.yml > /dev/null +DIFF_RESULT=$? + +if [ "$DIFF_RESULT" -eq "1" ]; then + if [ "$GITHUB_USER_TOKEN" != "" ]; then + cp generated.travis.yml .travis.yml + + LAST_COMMIT_MESSAGE=$(git log -1 HEAD --pretty=format:%s) + + grep ".travis.yml file is out of date" <<< "$LAST_COMMIT_MESSAGE" > /dev/null + LAST_COMMIT_IS_NOT_UPDATE=$? + + if [ "$LAST_COMMIT_MESSAGE" == "" ] || [ "$LAST_COMMIT_IS_NOT_UPDATE" -eq "0" ]; then + echo "Last commit message was '$LAST_COMMIT_MESSAGE', possible recursion or error in auto-update, aborting." + else + # only run auto-update for first travis job and if not a pull request + if [ "$TRAVIS_PULL_REQUEST" == "false" ] && [[ "$TRAVIS_JOB_NUMBER" == *.1 ]]; then + SCRIPT_DIR=$( dirname "$0" ) + $SCRIPT_DIR/configure_git.sh # re-configure in case git hasn't been configured yet + + git add .travis.yml + git commit -m ".travis.yml file is out of date, auto-updating .travis.yml file." + + git remote set-url origin "https://$GITHUB_USER_TOKEN:@github.com/$TRAVIS_REPO_SLUG.git" + + if ! git push origin $TRAVIS_BRANCH 2> /dev/null; then + echo "Failed to push!" + exit 1 + fi + else + echo "Building for pull request or not first job, skipping .travis.yml out of date check." + echo "" + echo "TRAVIS_PULL_REQUEST=$TRAVIS_PULL_REQUEST" + echo "TRAVIS_JOB_NUMBER=$TRAVIS_JOB_NUMBER" + fi + fi + + echo "" + echo "Generated .travis.yml:" + echo "" + cat generated.travis.yml + else + echo "${RED}Your .travis.yml file is out of date! Please update it using the generate:travis-yml command.${RESET}" + fi + + exit 1 +else + echo ".travis.yml file is up-to-date." +fi \ No newline at end of file diff --git a/tests/travis/configure_git.sh b/tests/travis/configure_git.sh new file mode 100755 index 0000000000..d9faa80d2d --- /dev/null +++ b/tests/travis/configure_git.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +if [ "$TRAVIS_COMMITTER_EMAIL" == "" ]; then + TRAVIS_COMMITTER_EMAIL="hello@piwik.org" +fi + +if [ "$TRAVIS_COMMITTER_NAME" == "" ]; then + TRAVIS_COMMITTER_NAME="Piwik Automation" +fi + +echo "Configuring git [email = $TRAVIS_COMMITTER_EMAIL, user = $TRAVIS_COMMITTER_NAME]..." + +git config --global user.email "$TRAVIS_COMMITTER_EMAIL" +git config --global user.name "$TRAVIS_COMMITTER_NAME" \ No newline at end of file diff --git a/tests/travis/initiate_ui_tests.sh b/tests/travis/initiate_ui_tests.sh index a65a5f6ac5..e44395938b 100755 --- a/tests/travis/initiate_ui_tests.sh +++ b/tests/travis/initiate_ui_tests.sh @@ -19,9 +19,6 @@ COMMIT_MESSAGE=$(git log "$TRAVIS_COMMIT" -1 --pretty=%B) cd tests/PHPUnit/UI -git config --global user.email "hello@piwik.org" -git config --global user.name "Piwik Automation" - UI_BRANCH="master" git checkout $UI_BRANCH git pull --rebase origin $UI_BRANCH diff --git a/tests/travis/travis-helper.sh b/tests/travis/travis-helper.sh new file mode 100644 index 0000000000..58ebc4cd55 --- /dev/null +++ b/tests/travis/travis-helper.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +# Sourced from https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/script/templates/header.sh +# + Tweaked to display output and not show the status line +travis_wait() { + local timeout=40 + local cmd="$@" + local log_file=travis_wait_$$.log + + $cmd & + local cmd_pid=$! + + travis_jigger $! $timeout $cmd & + local jigger_pid=$! + local result + + { + wait $cmd_pid 2>/dev/null + result=$? + ps -p$jigger_pid &>/dev/null && kill $jigger_pid + } || return 1 + + if [ $result -eq 0 ]; then +echo -e "\n${GREEN}The command \"$TRAVIS_CMD\" exited with $result.${RESET}" + else +echo -e "\n${RED}The command \"$TRAVIS_CMD\" exited with $result.${RESET}" + fi + +echo -e "\n${GREEN}Log:${RESET}\n" + cat $log_file + + return $result +} + +travis_jigger() { + # helper method for travis_wait() + local cmd_pid=$1 + shift +local timeout=40 + shift +local count=0 + + # clear the line + echo -e "\n" + + while [ $count -lt $timeout ]; do +count=$(($count + 1)) + #echo -ne "Still running ($count of $timeout): $@\r" + + # print invisible character + echo -ne "\xE2\x80\x8B" + sleep 60 + done + +echo -e "\n${RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${RESET}\n" + kill -9 $cmd_pid +} + +travis_retry() { + local result=0 + local count=1 + while [ $count -le 3 ]; do + [ $result -ne 0 ] && { + echo -e "\n${RED}The command \"$@\" failed. Retrying, $count of 3.${RESET}\n" >&2 + } + "$@" + result=$? + [ $result -eq 0 ] && break +count=$(($count + 1)) + sleep 1 + done + + [ $count -gt 3 ] && { + echo "\n${RED}The command \"$@\" failed 3 times.${RESET}\n" >&2 + } + + return $result +} \ No newline at end of file diff --git a/tests/travis/travis.sh b/tests/travis/travis.sh new file mode 100755 index 0000000000..a9aa252b18 --- /dev/null +++ b/tests/travis/travis.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +SCRIPT_DIR=$( dirname "$0" ) + +# for travis_wait function +source $SCRIPT_DIR/travis-helper.sh + +# go to tests directory +cd ../PHPUnit + +if [ "$TEST_SUITE" != "UITests" ] && [ "$TEST_SUITE" != "AngularJSTests" ] +then + if [ `phpunit --group __nogroup__ | grep "No tests executed" | wc -l` -ne 1 ] + then + echo "=====> There are some tests functions which do not have a @group set. " + echo " Please add the @group phpdoc comment to the following tests: <=====" + phpunit --group __nogroup__ --testdox | grep "[x]" + exit 1 + fi +fi + +if [ -n "$TEST_SUITE" ] +then + echo "Executing tests in test suite $TEST_SUITE..." + + if [ -n "$PLUGIN_NAME" ] + then + echo " [ plugin name = $PLUGIN_NAME ]" + fi + + if [ "$TEST_SUITE" = "AngularJSTests" ] + then + sh ./../angularjs/scripts/travis.sh + elif [ "$TEST_SUITE" = "JavascriptTests" ] + then + touch ../javascript/enable_sqlite + phantomjs ../javascript/testrunner.js + elif [ "$TEST_SUITE" = "UITests" ] + then + if [ -n "$PLUGIN_NAME" ] + then + artifacts_folder="protected/ui-tests.master.$PLUGIN_NAME" + else + artifacts_folder="ui-tests.$TRAVIS_BRANCH" + fi + + echo "" + echo "View UI failures (if any) here http://builds-artifacts.piwik.org/$artifacts_folder/$TRAVIS_JOB_NUMBER/screenshot-diffs/diffviewer.html" + echo "If the new screenshots are valid, then you can copy them over to tests/PHPUnit/UI/expected-ui-screenshots/." + echo "" + + if [ -n "$PLUGIN_NAME" ] + then + phantomjs ../lib/screenshot-testing/run-tests.js --assume-artifacts --persist-fixture-data --screenshot-repo=$TRAVIS_REPO_SLUG --plugin=$PLUGIN_NAME + else + phantomjs ../lib/screenshot-testing/run-tests.js --store-in-ui-tests-repo --persist-fixture-data --assume-artifacts + fi + else + if [ -n "$PLUGIN_NAME" ] + then + travis_wait phpunit --configuration phpunit.xml --colors --testsuite $TEST_SUITE --group $PLUGIN_NAME + else + travis_wait phpunit --configuration phpunit.xml --testsuite $TEST_SUITE --colors + fi + fi +else + travis_wait phpunit --configuration phpunit.xml --coverage-text --colors +fi \ No newline at end of file -- cgit v1.2.3