Introduce the `--fail-early` option for `tools/test.py`.

Exit the test with the appropriate exit code as soon as the
first test or building stage fails.

Change-Id: I727aeef0afaf4d0b23d1d25911a08f24bfc28a7f
Signed-off-by: Daniel Díaz <daniel.diaz@linaro.org>
diff --git a/tools/test.py b/tools/test.py
index c9ca329..e844a9f 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -226,6 +226,8 @@
                                  help='Do not run clang-format.')
   general_arguments.add_argument('--notest', action='store_true',
                                  help='Do not run tests.')
+  general_arguments.add_argument('--fail-early', action='store_true',
+                                 help='Exit as soon as a test fails.')
   sim_default = 'off' if platform.machine() == 'aarch64' else 'on'
   general_arguments.add_argument(
     '--simulator', action='store', choices=['on', 'off'],
@@ -365,6 +367,11 @@
 
   args = BuildOptions()
 
+  def MaybeExitEarly(rc):
+    if args.fail_early and rc != 0:
+      PrintStatus(rc == 0)
+      sys.exit(rc)
+
   if args.under_valgrind:
     util.require_program('valgrind')
 
@@ -379,8 +386,11 @@
 
   if not args.nolint and not args.fast:
     rc |= RunLinter()
+    MaybeExitEarly(rc)
+
   if not args.noclang_format and not args.fast:
     rc |= RunClangFormat()
+    MaybeExitEarly(rc)
 
   # Don't try to test the debugger if we are not running with the simulator.
   if not args.simulator:
@@ -404,6 +414,7 @@
         # Don't run the tests for this configuration if the build failed.
         if build_rc != 0:
           rc |= build_rc
+          MaybeExitEarly(rc)
           continue
 
       # Use the realpath of the test executable so that the commands printed
@@ -423,9 +434,11 @@
                                         list(runtime_options),
                                         args.under_valgrind,
                                         jobs = args.jobs, prefix = prefix)
+          MaybeExitEarly(rc)
 
       if not args.nobench:
         rc |= RunBenchmarks()
+        MaybeExitEarly(rc)
 
   PrintStatus(rc == 0)