This Week I Learned: Failing Pipe Operations

Noam Tenne
2 min readFeb 7, 2019

Failing Tests That Pass

I have Go project and I run its test suite using Travis CI.

Among all the setup procedure, the command that’s being executed is straightforward:

“Run the Go test suite and pipe the output to a text report”. That’s all that’s going on here.

Every now and then I noticed that even though tests are failing, Travis CI reports that the build has passed.

Looking At The Script Carefully

It’s two operations combined with a pipe |. This means that all standard output will be sent from the first command to the second command.

The problem is that when running commands with pipes, we are given an exit status that represents the state of the rightmost command. In our case — tee, which won’t necessarily fail when the tests will.

We can easily demonstrate this in the shell:

There Are Many Solutions

Including smart scripting, loops, creating temp files to store the state and such but the best and most elegant solution in my opinion is using a special bash option — pipefail.

Placing

In your bash script will change the default behavior. With this option set, the pipe will return the rightmost non-zero status as the result of the operation, or zero if all actions have completed successfully.

--

--