Test to pass if a specific exit code

When running some tests and am actually checking for specific exit codes to ensure it fails with the exit code expected.

For example, if the code exits with exit code 1 then the test has passed.

However with Actions when this happens then it is classed as a failure.

Is there a way to state if a specific exitcode is output then it is a pass.?


That would be possible by wrapping your test in something that checks the exit code, e.g. a Python script:

import subprocess
import sys
p = subprocess.run(command)
p.returncode != 1:
    print(f'Unexpected return code from {command}: {p.returncode}', file=sys.stderr)

However, you should only do that if you’re specifically testing error conditions. A successful run of your program should always lead to an error code of zero.

If you want, you can integrate the Python script into the workflow file by setting shell: python on a run step.

Thanks for this. It is the sort of thing that I thought I might have to do. It is a shame Actions doesn’t have something built-in yet.

For the majority of tests, I am looking for a 0 exit code, so this would only apply for some tests where I specifically want a failure.

My script only outputs the following 0 (pass), 1 (fatal), 2 (error), 3 (warn) and depending upon the test I will be looking for one of these.

My scripts are all BASH so I will likely create what you have posted in BASH.

Maybe a wrapper script that I can pass parameters that will then run the script and if the script provides the expected exit code with the wrapper will output exit code 0 and exit code 1 for unexpected code.


./exitcode.sh [command] [exitcode]
./exitcode.sh "./script.sh" "1"

Hopefully, there will be an inbuilt option in the future. But for now, I think this should do the trick.

1 Like