Generate code coverage with Istanbul
[dygraphs.git] / scripts / generate-coverage.sh
diff --git a/scripts/generate-coverage.sh b/scripts/generate-coverage.sh
new file mode 100755 (executable)
index 0000000..e2343de
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+# Generate code coverage data for posting to Coveralls.
+# This requires dist/*.js to be in place.
+# Output is coverage/lcov.info
+
+set -o errexit
+set -x
+
+# Generate per-file ES6 --> ES5 transpilations
+babel src --out-dir dist/src
+babel auto_tests/tests --out-dir dist/auto_tests/tests
+
+# Instrument the source code with Istanbul's __coverage__ variable.
+rm -rf coverage/*  # Clear out everything to ensure a hermetic run.
+mkdir -p coverage
+istanbul instrument --output coverage/src dist/src
+cp -r dist/auto_tests coverage/
+
+# Build a combined file for running the tests in-browser
+browserify coverage/auto_tests/tests/*.js -o coverage/tests.js
+
+# Run http-server and save its PID for cleanup
+http-server > /dev/null &
+SERVER_PID=$!
+function finish() {
+  kill -TERM $SERVER_PID
+}
+trap finish EXIT
+
+# Give the server a chance to start up
+sleep 1
+
+# Run the tests using mocha-phantomjs & mocha-phantomjs-istanbul
+# This produces coverage/coverage.json
+phantomjs \
+  ./node_modules/mocha-phantomjs/lib/mocha-phantomjs.coffee \
+  http://localhost:8080/auto_tests/coverage.html \
+  spec '{"hooks": "mocha-phantomjs-istanbul", "coverageFile": "coverage/coverage.json"}'
+
+if [ $CI ]; then
+  # Convert the JSON coverage to LCOV for coveralls.
+  istanbul report --include coverage/*.json lcovonly
+
+  # Monkey patch in the untransformed source paths.
+  # perl -i -pe 's,dist/main,src/main,' coverage/lcov.info
+  echo ''  # reset exit code -- failure to post coverage shouldn't be an error.
+
+else
+
+  # Convert the JSON coverage to HTML for viewing
+  istanbul report --include coverage/*.json html
+  set +x
+
+  echo 'To browse coverage, run:'
+  echo
+  echo '  open coverage/index.html'
+  echo
+
+fi