| 1 | #!/bin/bash |
| 2 | # Generate code coverage data for posting to Coveralls. |
| 3 | # This requires dist/*.js to be in place. |
| 4 | # Output is coverage/lcov.info |
| 5 | |
| 6 | set -o errexit |
| 7 | set -x |
| 8 | |
| 9 | # Generate per-file ES6 --> ES5 transpilations |
| 10 | babel src --out-dir dist/src |
| 11 | babel auto_tests/tests --out-dir dist/auto_tests/tests |
| 12 | |
| 13 | # Instrument the source code with Istanbul's __coverage__ variable. |
| 14 | rm -rf coverage/* # Clear out everything to ensure a hermetic run. |
| 15 | mkdir -p coverage |
| 16 | istanbul instrument --output coverage/src dist/src |
| 17 | cp -r dist/auto_tests coverage/ |
| 18 | |
| 19 | # Build a combined file for running the tests in-browser |
| 20 | browserify coverage/auto_tests/tests/*.js -o coverage/tests.js |
| 21 | |
| 22 | # Run http-server and save its PID for cleanup |
| 23 | http-server > /dev/null & |
| 24 | SERVER_PID=$! |
| 25 | function finish() { |
| 26 | kill -TERM $SERVER_PID |
| 27 | } |
| 28 | trap finish EXIT |
| 29 | |
| 30 | # Give the server a chance to start up |
| 31 | sleep 1 |
| 32 | |
| 33 | # Run the tests using mocha-phantomjs & mocha-phantomjs-istanbul |
| 34 | # This produces coverage/coverage.json |
| 35 | phantomjs \ |
| 36 | ./node_modules/mocha-phantomjs/lib/mocha-phantomjs.coffee \ |
| 37 | http://localhost:8080/auto_tests/coverage.html \ |
| 38 | spec '{"hooks": "mocha-phantomjs-istanbul", "coverageFile": "coverage/coverage.json"}' |
| 39 | |
| 40 | if [ $CI ]; then |
| 41 | # Convert the JSON coverage to LCOV for coveralls. |
| 42 | istanbul report --include coverage/*.json lcovonly |
| 43 | |
| 44 | # Monkey patch in the untransformed source paths. |
| 45 | # perl -i -pe 's,dist/main,src/main,' coverage/lcov.info |
| 46 | echo '' # reset exit code -- failure to post coverage shouldn't be an error. |
| 47 | |
| 48 | else |
| 49 | |
| 50 | # Convert the JSON coverage to HTML for viewing |
| 51 | istanbul report --include coverage/*.json html |
| 52 | set +x |
| 53 | |
| 54 | echo 'To browse coverage, run:' |
| 55 | echo |
| 56 | echo ' open coverage/index.html' |
| 57 | echo |
| 58 | |
| 59 | fi |