Skip to content

Commit

Permalink
Handle config switch missing argument exceptions (#1619)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rocher authored Mar 9, 2024
1 parent d302de2 commit fe485e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/alire/alire_early_elaboration.adb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,27 @@ 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 --
---------------------

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
Expand Down Expand Up @@ -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 '*' =>
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions testsuite/tests/config/missing-config-path/test.py
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 3 additions & 0 deletions testsuite/tests/config/missing-config-path/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
driver: python-script
indexes:
basic_index: {}

0 comments on commit fe485e3

Please sign in to comment.