I wanted to implement a unit testing shell script file that runs a command, compares the expected and actual output, and also add timeout constraints.
I used actual=$($cmd)
to capture the stdout and the cmd might look something like this:
(timeout $SHORT_PROCESS_TIMEOUT ./bf --file ./test/inc-dec.bf)
However I noticed that it hangs for around 2s (which is the short process timeout).
To investigate the issue, I created a new script file called test.sh with the following content and ran it with ./test.sh
command.
#! /bin/bash
timeout 2s ./bf
And it exitted with a 124 exit code (which is the exit code set when there’s a timeout). Then I ran the same command from the terminal and it worked fine with the expected output and the correct exit code. The program I passed in should definitely terminate within a few milliseconds so the issue might not be with the actual time.
Here’s the output of file bf
if anyone is interested:
bf: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
I’d like to know if there’s a difference when executing the same command from terminal vs a shell script file, or if I’m missing anything in general.