3

I have a makefile for compiling a static library. This makefile has a rule for compiling a unit test suite associated with the static library. Once the test suite is compiled, a python script is invoked to run the tests and log the results. It looks like this:

unit:
    $(MAKE) -C cXbase/unit
    python $(TESTS_RUNNER) $(UNIT_TESTS_EXEC) $(UNIT_TESTS_LOG)

I use Python to make the tests invocation portable. Right now, everything works fine: the tests compile and the test runner is called and logs everything properly in Linux and Windows. Only, when a test fail, I would like to stop the whole make process and return an error. More precisely, I would like not to be able to make all or to make unit when a unit test fails (or many).

If a unit test fails, the Python script returns a specific exit code. I would like to be able to capture it in a portable way and have make fail if that exit code is captured.

Would you have any recommendations on how to do this? I have found nothing convincing or portable elsewhere.

Thanks

BobMorane
  • 3,870
  • 3
  • 20
  • 42
  • 1
    Are you talking about a *return value* or an *exit code*? – Beta May 13 '17 at 23:27
  • 1
    Make will abort if a command in a recipe gives a non-zero exit status. If that isn't good enough, we must capture the exit status *within the command*, and the question is **"how portable is portable enough?"** I know how to capture and test it in bash; are you interested in other shells? – Beta May 13 '17 at 23:59
  • @Beta I would like it to work in bash and cmd (Windows) – BobMorane May 14 '17 at 00:28
  • And what do you want in case the exit status is something other than the special value? – Beta May 14 '17 at 00:44
  • @Beta Then `make` can go on as usual, all tests have passed. – BobMorane May 14 '17 at 02:53
  • Use Cygwin instead of cmd, see http://cygwin.com – igagis May 14 '17 at 18:49

1 Answers1

2

It seems the solution was way simpler than I imagined. The python exit code reflects directly in make it's exit code. In other words, if the script fails (exit code other than 0), make sees this as a command error and stops.

I had an error in my Python script exit code handling upon tests failure which hid this from me. Now it is solved and it works perfectly.

I found out about this here: Handling exit code returned by python in shell script

Community
  • 1
  • 1
BobMorane
  • 3,870
  • 3
  • 20
  • 42