Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data to Coveralls only from Travis, not when testing locally

I have an app (https://github.com/idmillington/dendry) that uses Travis CI to monitor build status. I use Istanbul to general a coverage report, and I'd like to send this to Coveralls, to generate a coverage button for the README.

All of this I can get working. But...

When I run npm test locally, I don't want to send coveralls the coverage data. I'm typically running npm test dozens of times for each commit. But when I push and Travis does its thing, I'd like Travis to update the coverage for me.

I could have something like this in my package.json:

"scripts": {
    "test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha",
}

Which is fine for locally, and doesn't update Coveralls, but Travis won't update Coveralls either. Or I could do:

"scripts": {
    "test": "./node_modules/.bin/istanbul test ./node_modules/.bin/_mocha && ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info",
}

Which is perfect for Travis, but tries to push data to Coveralls every time I run npm test locally.

As far as I can tell, I can't ask Travis to run something other than npm test.

I am unwilling to ask any potential users or contributors to remember to test using

$ npm run-script test-local

or some such, especially as running npm test would generate an upload error without the correct private key for coveralls.

Is there a way to get the right behavior here?

like image 671
Ian Avatar asked Aug 03 '14 19:08

Ian


1 Answers

The answer, as it turns out, was frighteningly simple. Travis does allow you to call whatever script you like as it runs, so I added this to my .travis.yml file:

script: npm run-script test-on-travis

so in package.json I could define:

"scripts": {
    "test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha",
    "test-on-travis": "./node_modules/.bin/istanbul cover --report lcovonly ./node_modules/.bin/_mocha && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
}

and everything works fine.

like image 105
Ian Avatar answered Sep 23 '22 21:09

Ian