Testing Shellscripts

Feb 28, 2019 07:05 · 309 words · 2 minute read Shell Tools CI

At some point, I found a bug in my build pipeline script that I had most recently built. I really wanted to make sure that the mistake I had found would not make its way back up and rear its ugly head once more. The cool thing about git repos is that I get to keep a history of the “bad behavior”, so I could use the diff between the references where the build script was misbehaving, and then making sure the output was correct.

I wrote another script (called test-.sh, same directory) to test my script. I ended up with a very crappy “assert” function that looks like this:

assert () {
	if ! eval $1; then
		echo "Failed: " $2;
		echo $3;
		cat $results;
		exit 1;

Parameter #1 is what I test for after I run the build script once. Parameter #2 is a “friendly name” for the test, and Parameter #3 is what I expected, also in words humans can understand.

If my test fails, in theory, I get useful output on the command line, and it exits, and I’m happy. In effect, what usually happens is that I feed the eval line a thing that looks for a file that doesn’t exist because the tests got broken and therefore it never got to building the file I’m looking for; this is still okay for now, because while I don’t get useful output out of my tests, I definitely know my code got broken.

It’s not a super elegant strategy, but it works, and have saved me a few breakages from going live; this is especially important as the breakages would have prevented builds from more than one team from being built. I AM PROUD OF IT, COLIN. SHUSH.

This library is much better and I should switch my stuff out to that: https://github.com/internap/sbtest