From fe485e3a5c22eaa5e7cb85344430b223b396403c Mon Sep 17 00:00:00 2001 From: Francesc Rocher Date: Sat, 9 Mar 2024 14:47:22 +0100 Subject: [PATCH] Handle config switch missing argument exceptions (#1619) Both 'alr -c' and 'alr --config' throw exceptions because a required parameter is missing. This fix handle missing parameter of config switch in the same way as, for example, 'alr -C' and 'alr --chdir': an error message informs the user that a required parameter is missing. --- src/alire/alire_early_elaboration.adb | 23 +++++++++++++++++-- .../tests/config/missing-config-path/test.py | 18 +++++++++++++++ .../config/missing-config-path/test.yaml | 3 +++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 testsuite/tests/config/missing-config-path/test.py create mode 100644 testsuite/tests/config/missing-config-path/test.yaml diff --git a/src/alire/alire_early_elaboration.adb b/src/alire/alire_early_elaboration.adb index b09b7858..94765f2e 100644 --- a/src/alire/alire_early_elaboration.adb +++ b/src/alire/alire_early_elaboration.adb @@ -71,6 +71,17 @@ package body Alire_Early_Elaboration is procedure Check_Switches is + ------------------------- + -- Config_Switch_Error -- + ------------------------- + + procedure Config_Switch_Error (Switch : String) is + begin + GNAT.IO.Put_Line + ("ERROR: Switch " & Switch & " requires argument (global)."); + Early_Error ("try ""alr --help"" for more information."); + end Config_Switch_Error; + --------------------- -- Set_Config_Path -- --------------------- @@ -78,7 +89,9 @@ package body Alire_Early_Elaboration is procedure Set_Config_Path (Path : String) is package Adirs renames Ada.Directories; begin - if not Adirs.Exists (Path) then + if Path = "" then + Config_Switch_Error ("--config"); + elsif not Adirs.Exists (Path) then Early_Error ("Invalid non-existing configuration path: " & Path); elsif Adirs.Kind (Path) not in Adirs.Directory then @@ -107,11 +120,13 @@ package body Alire_Early_Elaboration is end if; end Check_Long_Switch; + Option : Character; begin loop -- We use the simpler Getopt form to avoid built-in help and other -- shenanigans. - case Getopt ("* d? --debug? q v c= --config=") is + Option := Getopt ("* d? --debug? q v c= --config="); + case Option is when ASCII.NUL => exit; when '*' => @@ -148,6 +163,10 @@ package body Alire_Early_Elaboration is end case; end loop; exception + when GNAT.Command_Line.Invalid_Parameter => + if Option = 'c' then + Config_Switch_Error ("-c"); + end if; when Exit_From_Command_Line => -- Something unexpected happened but it will be properly dealt -- with later on, in the regular command-line parser. diff --git a/testsuite/tests/config/missing-config-path/test.py b/testsuite/tests/config/missing-config-path/test.py new file mode 100644 index 00000000..0047db81 --- /dev/null +++ b/testsuite/tests/config/missing-config-path/test.py @@ -0,0 +1,18 @@ +""" +Verify that errors are properly handled when no config path is given +""" + +from drivers.alr import run_alr +from drivers.asserts import assert_match + +import re + +p = run_alr("--config", complain_on_error=False) +assert p.status != 0, "command should fail" +assert_match("ERROR: Switch --config requires argument.*", p.out, flags=re.S) + +p = run_alr("-c", complain_on_error=False) +assert p.status != 0, "command should fail" +assert_match("ERROR: Switch -c requires argument.*", p.out, flags=re.S) + +print('SUCCESS') diff --git a/testsuite/tests/config/missing-config-path/test.yaml b/testsuite/tests/config/missing-config-path/test.yaml new file mode 100644 index 00000000..872fc127 --- /dev/null +++ b/testsuite/tests/config/missing-config-path/test.yaml @@ -0,0 +1,3 @@ +driver: python-script +indexes: + basic_index: {} -- 2.39.5