June 21, 2014

Testing Multiple Subdirectories with Travis-CI

Sometimes, you’ll want to test a repo with multiple projects in subdirectories. A recent project I’ve been working on with Lita Cho in Node has two sub projects — one server folder, and one client folder. Each have their own package.json, dependencies, code, etc.

Screenshot of tilt-js repository

The repository containing multiple projects.

Testing a Single Subdirectory

Testing a single subdirectory is pretty simple. In our travis.yml, we’ll put a cd command in the script key.

language: node_js
node_js:
  - "0.11"
  - "0.10"
script: cd tilt-client && npm install && npm test

Obviously, if you’re using something besides node, you’ll need to put whatever test command you need instead of npm install && npm test.

script is the shell command travis runs to run tests, so adding a cd command will of course change the directory the tests are run in.

But I Want Multiple Directories

So we have multiple directories. We could change script to test more folders after we’re done with the first.

script: cd tilt-client && npm install && npm test && cd ../tilt-server && npm install && npm test

However, this is gross and stupid. There’s a much cleaner solution using the build matrix. That article explains it better than I can, but to put it simply, the build matrix tests all possible permutations of the build. In our case, we want to make “all possible permutations” include permutating through both subprojects of our repo. To do this, we’ll use environment variables.

language: node_js
node_js:
  - "0.11"
  - "0.10"
env:
  - TEST_DIR=tilt-client
  - TEST_DIR=tilt-server
script: cd $TEST_DIR && npm install && npm test

The build matrix for the config file above will run four tests:

  • Node 0.10, with $TEST_DIR set to tilt-client
  • Node 0.11, with $TEST_DIR set to tilt-client
  • Node 0.10, with $TEST_DIR set to tilt-server
  • Node 0.11, with $TEST_DIR set to tilt-server

Our cd command in script is now pointed at $TEST_DIR, so the build matrix will be testing both the client and the server. This is also better than the loooong script method because it can run in parallel on more machines.

Yay! Travis is fun.