From c56d728e1f8702db02cf7e7e3cdc567b3df47ac3 Mon Sep 17 00:00:00 2001
From: Zubin Duggal <zubin.duggal@gmail.com>
Date: Tue, 23 Apr 2024 17:22:18 +0530
Subject: [PATCH] testsuite: Handle exceptions in framework_fail when testdir
 is not initialised

When `framework_fail` is called before initialising testdir, it would fail with
an exception reporting the testdir not being initialised instead of the actual failure.

Ensure we report the actual reason for the failure instead of failing in this way.

One way this can manifest is when trying to run a test that doesn't exist using `--only`
---
 testsuite/driver/testlib.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/testsuite/driver/testlib.py b/testsuite/driver/testlib.py
index 2af559075713..b73518679685 100644
--- a/testsuite/driver/testlib.py
+++ b/testsuite/driver/testlib.py
@@ -1542,7 +1542,13 @@ def override_options(pre_cmd):
 
 def framework_fail(name: Optional[TestName], way: Optional[WayName], reason: str) -> None:
     opts = getTestOpts()
-    directory = re.sub(r'^\.[/\\]', '', str(opts.testdir))
+    # framework_fail can be called before testdir is initialised,
+    # so we need to take care not to blow up with the wrong way
+    # and report the actual reason for the failure.
+    try:
+      directory = re.sub(r'^\.[/\\]', '', str(opts.testdir))
+    except:
+      directory = ''
     full_name = '%s(%s)' % (name, way)
     if_verbose(1, '*** framework failure for %s %s ' % (full_name, reason))
     name2 = name if name is not None else TestName('none')
-- 
GitLab