From 8c67a3e77e954c5a6342d7a440bbaaee256565d7 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 15 Dec 2014 14:58:15 -0800 Subject: [PATCH] test(travis): run unit tests on travis + saucelabs --- .travis.yml | 21 ++++++++++++ config/karma.sauce.conf.js | 21 ++++++++++++ config/karma.travis.conf.js | 24 +++++++++++++ karma.conf.js | 14 ++++++-- scripts/dev-dep.sh | 29 ++++++++++++++++ scripts/npm-dep.sh | 15 +++++++++ scripts/print_logs.sh | 11 ++++++ scripts/read-pkg-url.js | 8 +++++ scripts/sauce_connect_setup.sh | 50 ++++++++++++++++++++++++++++ scripts/test_on_sauce.sh | 8 +++++ scripts/wait_for_browser_provider.sh | 7 ++++ 11 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 .travis.yml create mode 100644 config/karma.sauce.conf.js create mode 100644 config/karma.travis.conf.js create mode 100755 scripts/dev-dep.sh create mode 100755 scripts/npm-dep.sh create mode 100755 scripts/print_logs.sh create mode 100755 scripts/read-pkg-url.js create mode 100755 scripts/sauce_connect_setup.sh create mode 100755 scripts/test_on_sauce.sh create mode 100755 scripts/wait_for_browser_provider.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d033385 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +language: node_js +node_js: + - "0.10" + +env: + global: + - BROWSER_PROVIDER_READY_FILE=/tmp/sauce-connect-ready + - LOGS_DIR=/tmp/angular-hint-build/logs + - SAUCE_USERNAME=angular-ci + - SAUCE_ACCESS_KEY=9b988f434ff8-fbca-8aa4-4ae3-35442987 + +install: + - mkdir -p $LOGS_DIR + - ./scripts/sauce_connect_setup.sh + - npm install + - npm install -g gulp + - npm install -g karma-cli + - ./scripts/wait_for_browser_provider.sh + +script: + - ./scripts/test_on_sauce.sh diff --git a/config/karma.sauce.conf.js b/config/karma.sauce.conf.js new file mode 100644 index 0000000..0b50cc2 --- /dev/null +++ b/config/karma.sauce.conf.js @@ -0,0 +1,21 @@ +/* + * karma.conf.js and karma.es5.conf.js optionally load this + */ + +var CUSTOM_LAUNCHERS = { + 'SL_Chrome': { + base: 'SauceLabs', + browserName: 'chrome', + version: '35' + } +}; + +module.exports = function(options) { + options.sauceLabs = { + testName: 'AngularJS Batarang Unit Tests', + startConnect: true + }; + options.customLaunchers = CUSTOM_LAUNCHERS; + options.browsers = Object.keys(CUSTOM_LAUNCHERS); + options.reporters = ['dots', 'saucelabs']; +}; diff --git a/config/karma.travis.conf.js b/config/karma.travis.conf.js new file mode 100644 index 0000000..8b1fffd --- /dev/null +++ b/config/karma.travis.conf.js @@ -0,0 +1,24 @@ +/* + * karma.conf.js optionally loads this + */ + +module.exports = function(options) { + if (!isTravis()) { + return; + } else if (!options.sauceLabs) { + throw new Error('This should be loaded after karma.sauce config'); + } + options.sauceLabs.build = 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER + ' (' + process.env.TRAVIS_BUILD_ID + ')'; + options.sauceLabs.tunnelIdentifier = process.env.TRAVIS_JOB_NUMBER; + options.sauceLabs.startConnect = false; + + // TODO(vojta): remove once SauceLabs supports websockets. + // This speeds up the capturing a bit, as browsers don't even try to use websocket. + options.transports = ['xhr-polling']; + + options.singleRun = true; +}; + +function isTravis() { + return !!process.env.TRAVIS; +} diff --git a/karma.conf.js b/karma.conf.js index 764f781..80a3761 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,8 +2,11 @@ * This karma conf tests just the panel app */ +var sauceConfig = require('./config/karma.sauce.conf'); +var travisConfig = require('./config/karma.travis.conf'); + module.exports = function(config) { - config.set({ + var options = { frameworks: ['browserify', 'jasmine'], files: [ 'node_modules/angular/angular.js', @@ -17,5 +20,12 @@ module.exports = function(config) { 'hint.js': [ 'browserify' ] }, browsers: ['Chrome'], - }); + }; + + if (process.argv.indexOf('--sauce') > -1) { + sauceConfig(options); + travisConfig(options); + } + + config.set(options); }; diff --git a/scripts/dev-dep.sh b/scripts/dev-dep.sh new file mode 100755 index 0000000..91362f2 --- /dev/null +++ b/scripts/dev-dep.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Switch a dependency to git repo. +# Remove the NPM package and link it to a repo in parent directory. +DEP_NAME=$1 +SCRIPT_DIR=$(dirname $0) +cd $SCRIPT_DIR/.. + +if [ -L ./node_modules/$DEP_NAME ]; then + echo "$DEP_NAME is already a symlink" +else + PKG_INFO=($($SCRIPT_DIR/read-pkg-url.js ./node_modules/$DEP_NAME/package.json)) + URL=${PKG_INFO[0]} + DIR_NAME=${PKG_INFO[1]} + + echo "Switching $DEP_NAME" + rm -rf ./node_modules/$DEP_NAME + + if [ -d ../$DIR_NAME ]; then + echo "Repo already cloned in ../$DIR_NAME" + else + cd .. + git clone $URL $DIR_NAME + cd - + fi + + echo "Link ./node_modules/$DEP_NAME -> ../$DIR_NAME" + ln -s ../../$DIR_NAME ./node_modules/$DEP_NAME +fi diff --git a/scripts/npm-dep.sh b/scripts/npm-dep.sh new file mode 100755 index 0000000..f7a638e --- /dev/null +++ b/scripts/npm-dep.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# +# Switch a dependency to NPM. +# Remove the symlink and install from NPM. + +DEP_NAME=$1 +SCRIPT_DIR=$(dirname $0) +cd $SCRIPT_DIR/.. + +if [ ! -L ./node_modules/$DEP_NAME ]; then + echo "$DEP_NAME is not a symlink" +else + rm ./node_modules/$DEP_NAME + npm install $DEP_NAME +fi diff --git a/scripts/print_logs.sh b/scripts/print_logs.sh new file mode 100755 index 0000000..923f80b --- /dev/null +++ b/scripts/print_logs.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +LOG_FILES=$LOGS_DIR/* + +for FILE in $LOG_FILES; do + echo -e "\n\n\n" + echo "==================================================================" + echo " $FILE" + echo "==================================================================" + cat $FILE +done \ No newline at end of file diff --git a/scripts/read-pkg-url.js b/scripts/read-pkg-url.js new file mode 100755 index 0000000..12474fa --- /dev/null +++ b/scripts/read-pkg-url.js @@ -0,0 +1,8 @@ +#!/usr/bin/env node + +var fs = require('fs'); +var pkg = JSON.parse(fs.readFileSync(process.argv[2])); +var url = pkg.repository.url; +var dirname = url.replace(/^.*\//, '').replace(/\.git$/, ''); + +console.log(url + ' ' + dirname); diff --git a/scripts/sauce_connect_setup.sh b/scripts/sauce_connect_setup.sh new file mode 100755 index 0000000..1685b5b --- /dev/null +++ b/scripts/sauce_connect_setup.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +set -e + +# Setup and start Sauce Connect for your TravisCI build +# This script requires your .travis.yml to include the following two private env variables: +# SAUCE_USERNAME +# SAUCE_ACCESS_KEY +# Follow the steps at https://saucelabs.com/opensource/travis to set that up. +# +# Curl and run this script as part of your .travis.yml before_script section: +# before_script: +# - curl https://gist.github.com/santiycr/5139565/raw/sauce_connect_setup.sh | bash + +CONNECT_URL="https://saucelabs.com/downloads/sc-4.3-linux.tar.gz" +CONNECT_DIR="/tmp/sauce-connect-$RANDOM" +CONNECT_DOWNLOAD="sc-latest-linux.tar.gz" + +CONNECT_LOG="$LOGS_DIR/sauce-connect" +CONNECT_STDOUT="$LOGS_DIR/sauce-connect.stdout" +CONNECT_STDERR="$LOGS_DIR/sauce-connect.stderr" + +# Get Connect and start it +mkdir -p $CONNECT_DIR +cd $CONNECT_DIR +curl $CONNECT_URL -o $CONNECT_DOWNLOAD 2> /dev/null 1> /dev/null +mkdir sauce-connect +tar --extract --file=$CONNECT_DOWNLOAD --strip-components=1 --directory=sauce-connect > /dev/null +rm $CONNECT_DOWNLOAD + +SAUCE_ACCESS_KEY=`echo $SAUCE_ACCESS_KEY | rev` + + +ARGS="" + +# Set tunnel-id only on Travis, to make local testing easier. +if [ ! -z "$TRAVIS_JOB_NUMBER" ]; then + ARGS="$ARGS --tunnel-identifier $TRAVIS_JOB_NUMBER" +fi +if [ ! -z "$BROWSER_PROVIDER_READY_FILE" ]; then + ARGS="$ARGS --readyfile $BROWSER_PROVIDER_READY_FILE" +fi + + +echo "Starting Sauce Connect in the background, logging into:" +echo " $CONNECT_LOG" +echo " $CONNECT_STDOUT" +echo " $CONNECT_STDERR" +sauce-connect/bin/sc -u $SAUCE_USERNAME -k $SAUCE_ACCESS_KEY -v $ARGS \ + --logfile $CONNECT_LOG 2> $CONNECT_STDERR 1> $CONNECT_STDOUT & \ No newline at end of file diff --git a/scripts/test_on_sauce.sh b/scripts/test_on_sauce.sh new file mode 100755 index 0000000..2ee482f --- /dev/null +++ b/scripts/test_on_sauce.sh @@ -0,0 +1,8 @@ +#! /bin/bash +SCRIPT_DIR=$(dirname $0) +cd $SCRIPT_DIR/.. + +SAUCE_ACCESS_KEY=`echo $SAUCE_ACCESS_KEY | rev` + +gulp +karma start --sauce diff --git a/scripts/wait_for_browser_provider.sh b/scripts/wait_for_browser_provider.sh new file mode 100755 index 0000000..cb21ccf --- /dev/null +++ b/scripts/wait_for_browser_provider.sh @@ -0,0 +1,7 @@ +#!/bin/bash + + +# Wait for Connect to be ready before exiting +while [ ! -f $BROWSER_PROVIDER_READY_FILE ]; do + sleep .5 +done \ No newline at end of file