Making Travis-CI test multiple components in one single repository.

Recently i've been using component in one of my projects. This has to be the hardest-to-google piece of software ever, but that's beside the point. I'm not very convinced by it, but as always, I try to use it on a project to be familiarise myself with it in case I ever land a project that uses it.

Initially, I was given the kind of code that most recovering flash-addicted agencies create: bunch of global variables here, no unit tests there, etc. The project is made up of four parts that fitted really well with component's philosophy, but It needed to put them in just one repository, since the number of private git repos the organisation I'm currently working for is limited.

In an ideal world I'd have separate repos for each component, but that was not an option here. Still, I wanted each component to have its own Gruntfile, package.json,, and its own set of unit tests, etc. However, working out the CI setup for the repo was proving difficult - every time I commited Travis was sending me an angry email.

Then I realised you can add a script property on your .travis.yml file, that tells it how to run tests. That gave me an idea: I'd just write a script that goes inside each component and runs the tests individually:

set -e
components="bulbasaur squirtle charmander"

for component in $components
    echo "Testing component: $component"
    cd $component
    npm install
    npm test
    cd ..

Notice the line that says set -e. This makes the script exit with a non-zero error code if any of the commands fails. This is what Travis looks for on your scripts, so make sure you add this at the top of your scripts.

Then, on my .travis.yml, I just put the following one liner:

script: "./"

And all my tests started to run. If I wanna add any other components, I can just add them to the components array in my script.