From 182ebf37ec9209ebb7fdf9aca45990dd8da116fc Mon Sep 17 00:00:00 2001 From: Kevin van Zonneveld Date: Thu, 7 Apr 2016 08:58:36 +0200 Subject: [PATCH] Boot & kill servers in a separate script, offer npm run scripts for both ways --- .travis.yml | 2 +- bin/bootandkill-servers | 61 +++++++++++++++++++++++++++++++++++++++++ bin/test-acceptance | 55 ++++++------------------------------- package.json | 3 +- 4 files changed, 73 insertions(+), 48 deletions(-) create mode 100755 bin/bootandkill-servers diff --git a/.travis.yml b/.travis.yml index c703de08c..629e383f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ before_script: script: - npm run build - npm run test -- npm run test:acceptance || true +- npm run test:acceptance:handleservers || true - git config --global user.name 'Uppy Bot' - git config --global user.email 'uppybot@uppy.io' - if [ "${TRAVIS_PULL_REQUEST}" == "false" ] && [ "${TRAVIS_BRANCH}" == "master" ]; diff --git a/bin/bootandkill-servers b/bin/bootandkill-servers new file mode 100755 index 000000000..d7f59bded --- /dev/null +++ b/bin/bootandkill-servers @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# How to run: +# +# ./bootandkill-servers ./test-acceptance +# +# this will boot hexo & uppy-server, run the script that you provided as an argument, +# and tear down the servers + +set -o pipefail +set -o errexit +set -o nounset +# set -o xtrace + +# Set magic variables for current file & dir +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" +__base="$(basename ${__file} .sh)" +__root="$(cd "$(dirname "${__dir}")" && pwd)" + +if [ ! -f "${__root}/env.sh" ]; then + cp "${__root}/env.example.sh" "${__root}/env.sh" +fi +if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then + source "${__root}/env.sh" +fi +if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then + echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. " + echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again" + exit 1 +fi + +function killProcessListeningOnPort () { + local port="${1}" + lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs kill -9 +} + +function cleanup_servers () { + echo "[${__base}] --> Killing any server listening on port 4000" + killProcessListeningOnPort 4000 || true + + echo "[${__base}] --> Killing any server listening on port 8080" + killProcessListeningOnPort 8080 || true + + kill -9 ${tailPid} +} + +echo "[${__base}] --> Killing any server listening on port 4000" +killProcessListeningOnPort 4000 || true +echo "[${__base}] --> Killing any server listening on port 8080" +killProcessListeningOnPort 8080 || true + +echo "[${__base}] --> Start webserver and uppy-server in the background" +rm -f nohup.out || true +touch nohup.out +nohup npm run start & +tail -f nohup.out & +tailPid=${!} + +trap cleanup_servers EXIT + +${@} diff --git a/bin/test-acceptance b/bin/test-acceptance index 639d64c7e..c877a0f83 100755 --- a/bin/test-acceptance +++ b/bin/test-acceptance @@ -1,9 +1,16 @@ #!/usr/bin/env bash # How to run: # -# - When using `./bin/test-acceptance handle-servers` this script boots and kills servers too (handy for Travis and quick access) -# - When using `./bin/test-acceptance` this script assumes you have your own servers on localhost running (handy for going in depth) +# ./bootandkill-servers ./test-acceptance # +# this will boot hexo & uppy-server, run the acceptance tests, +# and tear down the servers. +# +# To run just the acceptance tests: +# +# ./test-acceptance +# + set -o pipefail set -o errexit @@ -16,24 +23,6 @@ __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" __base="$(basename ${__file} .sh)" __root="$(cd "$(dirname "${__dir}")" && pwd)" -mode="${1:-}" - -if [ ! -f "${__root}/env.sh" ]; then - cp "${__root}/env.example.sh" "${__root}/env.sh" -fi -if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then - source "${__root}/env.sh" -fi -if [ "${UPPYSERVER_DROPBOX_KEY:-}" = "" ] || [ "${UPPYSERVER_DROPBOX_KEY:-***}" = "***" ]; then - echo "[${__base}] Env var UPPYSERVER_DROPBOX_KEY still had the example value '${UPPYSERVER_DROPBOX_KEY:-}'. " - echo "[${__base}] Please save the actual secrets in '${__root}/env.sh' and try again" - exit 1 -fi - -function killProcessListeningOnPort () { - local port="${1}" - lsof -n -i4TCP:${port} | awk '/LISTEN/ {print $2}' |xargs kill -9 -} function waitForPortOpen () { local port="${1}" @@ -51,32 +40,6 @@ function waitForPortOpen () { done } -function cleanup_servers () { - echo "[${__base}] --> Killing any server listening on port 4000" - killProcessListeningOnPort 4000 || true - - echo "[${__base}] --> Killing any server listening on port 8080" - killProcessListeningOnPort 8080 || true - - kill -9 ${tailPid} -} - -if [ "${mode}" = "handle-servers" ]; then - echo "[${__base}] --> Killing any server listening on port 4000" - killProcessListeningOnPort 4000 || true - echo "[${__base}] --> Killing any server listening on port 8080" - killProcessListeningOnPort 8080 || true - - echo "[${__base}] --> Start webserver and uppy-server in the background" - rm -f nohup.out || true - touch nohup.out - nohup npm run start & - tail -f nohup.out & - tailPid=${!} - - trap cleanup_servers EXIT -fi - echo "[${__base}] --> Wait for hexo webserver to be online" waitForPortOpen 4000 diff --git a/package.json b/package.json index 2ba6122d2..807437135 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "release": "npm version ${SEMANTIC:-patch} -m \"Release %s\" && git push && git push --tags && npm publish", "start:server": "uppy-server --port 8080", "start": "parallelshell 'npm run watch' 'npm run start:server' 'npm run web:preview'", - "test:acceptance": "bin/test-acceptance handle-servers", + "test:acceptance:handleservers": "bin/bootandkill-servers bin/test-acceptance", + "test:acceptance": "bin/test-acceptance", "test:unit": "node test/index.js", "test": "npm run lint && npm run test:unit", "travis:deletecache": "travis cache --delete",