Repository: reframe-hpc/reframe

Issue:
Issue a warning if an execution mode requested with `--mode` is not found
Currently no warning is issued: `reframe --mode=foo -l`.

Discussion:

Patch:
diff --git a/reframe/frontend/cli.py b/reframe/frontend/cli.py
index 9a465ea2..54436755 100644
--- a/reframe/frontend/cli.py
+++ b/reframe/frontend/cli.py
@@ -737,16 +737,22 @@ def main():
 
         # Update options from the selected execution mode
         if options.mode:
-            mode_args = site_config.get(f'modes/@{options.mode}/options')
-
-            # We lexically split the mode options, because otherwise spaces
-            # will be treated as part of the option argument; see GH bug #1554
-            mode_args = list(itertools.chain.from_iterable(shlex.split(m)
-                                                           for m in mode_args))
-            # Parse the mode's options and reparse the command-line
-            options = argparser.parse_args(mode_args)
-            options = argparser.parse_args(namespace=options.cmd_options)
-            options.update_config(site_config)
+            mode = site_config.get(f'modes/@{options.mode}')
+            if mode is None:
+                printer.warning(f'invalid mode: {options.mode!r}; ignoring...')
+            else:
+                mode_args = site_config.get(f'modes/@{options.mode}/options')
+
+                # We lexically split the mode options, because otherwise spaces
+                # will be treated as part of the option argument;
+                # see GH bug #1554
+                mode_args = list(
+                    itertools.chain.from_iterable(shlex.split(m)
+                                                  for m in mode_args))
+                # Parse the mode's options and reparse the command-line
+                options = argparser.parse_args(mode_args)
+                options = argparser.parse_args(namespace=options.cmd_options)
+                options.update_config(site_config)
 
         logging.configure_logging(site_config)
     except (OSError, errors.ConfigError) as e:


Test patch:
diff --git a/unittests/test_cli.py b/unittests/test_cli.py
index bfdb59f3..adfe5d0f 100644
--- a/unittests/test_cli.py
+++ b/unittests/test_cli.py
@@ -478,6 +478,20 @@ def test_execution_modes(run_reframe):
     assert 'Ran 2/2 test case' in stdout
 
 
+def test_invalid_mode_warning(run_reframe):
+    mode = 'invalid'
+    returncode, stdout, stderr = run_reframe(
+        action='list',
+        checkpath=[],
+        environs=[],
+        local=False,
+        mode=mode
+    )
+    assert 'Traceback' not in stdout
+    assert 'Traceback' not in stderr
+    assert f'invalid mode: {mode!r}; ignoring' in stdout
+
+
 def test_timestamp_option(run_reframe):
     timefmt = time.strftime('xxx_%F')
     returncode, stdout, _ = run_reframe(