diff --git a/src/alr/alr-commands-config.adb b/src/alr/alr-commands-config.adb index b990a089e..00087dd3c 100644 --- a/src/alr/alr-commands-config.adb +++ b/src/alr/alr-commands-config.adb @@ -1,5 +1,3 @@ -with AAA.Enum_Tools; - with Alire.Config; with Alire.Config.Edit; with Alire.Root; @@ -109,21 +107,17 @@ package body Alr.Commands.Config is -- Check explicitly for booleans to store the proper TOML type -- regardless of the capitalization used by the user. - declare - function Is_Boolean is new AAA.Enum_Tools.Is_Valid (Boolean); - begin - if Is_Boolean (Val) then - Alire.Config.Edit.Set_Boolean - (Lvl, - Key, Boolean'Value (Val), - Check => Alire.Config.Edit.Valid_Builtin'Access); - else - Alire.Config.Edit.Set - (Lvl, - Key, Val, - Check => Alire.Config.Edit.Valid_Builtin'Access); - end if; - end; + if Is_Boolean (Val) then + Alire.Config.Edit.Set_Boolean + (Lvl, + Key, Boolean'Value (Val), + Check => Alire.Config.Edit.Valid_Builtin'Access); + else + Alire.Config.Edit.Set + (Lvl, + Key, Val, + Check => Alire.Config.Edit.Valid_Builtin'Access); + end if; end; elsif Cmd.Unset then diff --git a/src/alr/alr-commands-printenv.adb b/src/alr/alr-commands-printenv.adb index 6c089ee5e..89d06be86 100644 --- a/src/alr/alr-commands-printenv.adb +++ b/src/alr/alr-commands-printenv.adb @@ -4,6 +4,8 @@ with Alire.Platforms; package body Alr.Commands.Printenv is + Last_Build_Switch : constant String := "--last-build"; + ------------- -- Execute -- ------------- @@ -30,7 +32,7 @@ package body Alr.Commands.Printenv is Cmd.Requires_Workspace; - if Cmd.Last_Build then + if To_Boolean (Cmd.Last_Build, "--last-build", True) then Cmd.Root.Set_Build_Profiles (Alire.Crate_Configuration.Last_Build_Profiles); end if; @@ -97,8 +99,9 @@ package body Alr.Commands.Printenv is "Use a Windows CMD shell format for the export"); Define_Switch (Config, Cmd.Last_Build'Access, - "", "--last-build", - "Use last build profiles instead of manifest profiles"); + "", Last_Build_Switch & "?", + "Use last build profiles (default) or manifest profiles", + Argument => "=BOOLEAN"); end Setup_Switches; end Alr.Commands.Printenv; diff --git a/src/alr/alr-commands-printenv.ads b/src/alr/alr-commands-printenv.ads index 91c7e2864..4f2263c4d 100644 --- a/src/alr/alr-commands-printenv.ads +++ b/src/alr/alr-commands-printenv.ads @@ -36,6 +36,6 @@ private Unix_Shell : aliased Boolean := False; Power_Shell : aliased Boolean := False; Cmd_Shell : aliased Boolean := False; - Last_Build : aliased Boolean := False; + Last_Build : aliased GNAT_String := new String'(Unset); end record; end Alr.Commands.Printenv; diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 62facdfda..a6ee20eb4 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -579,6 +579,31 @@ package body Alr.Commands is Cmd.Optional_Root := Alire.Roots.Optional.Outcome_Success (Root); end Set; + ---------------- + -- To_Boolean -- + ---------------- + + function To_Boolean (Image : GNAT_String; + Switch : String; + Default : Boolean) + return Boolean + is + begin + if Image in null or else Image.all = "" or else Image.all = Unset then + return Default; + elsif Is_Boolean (Image.all) then + return Boolean'Value (Image.all); + elsif Image (Image'First) = '=' then + return To_Boolean (new String'(Image (Image'First + 1 .. Image'Last)), + Switch => Switch, + Default => Default); + else + Reportaise_Wrong_Arguments + ("Value for switch " & Switch & " is not a proper boolean: " + & Image.all); + end if; + end To_Boolean; + begin -- Commands -- diff --git a/src/alr/alr-commands.ads b/src/alr/alr-commands.ads index d5e329fa8..7e115185d 100644 --- a/src/alr/alr-commands.ads +++ b/src/alr/alr-commands.ads @@ -1,3 +1,4 @@ +private with AAA.Enum_Tools; with AAA.Strings; with Alire.Directories; @@ -8,6 +9,7 @@ with Alire.Version; with CLIC.Subcommand; private with GNAT.IO; +private with GNAT.Strings; private with CLIC.Subcommand.Instance; private with Alr.OS_Lib; -- For the benefit of many child packages that use it @@ -109,8 +111,8 @@ private -- Facilities for command/argument identification. These are available to -- commands. - procedure Reportaise_Command_Failed (Message : String); - procedure Reportaise_Wrong_Arguments (Message : String); + procedure Reportaise_Command_Failed (Message : String) with No_Return; + procedure Reportaise_Wrong_Arguments (Message : String) with No_Return; -- Report and Raise :P -- Folder guards conveniences for commands: @@ -153,4 +155,20 @@ private Unset : constant String := "unset"; -- Canary for when a string switch is given without value + subtype GNAT_String is GNAT.Strings.String_Access; + -- Convenience for commands that use string arguments + + function Is_Boolean is new AAA.Enum_Tools.Is_Valid (Boolean); + + function To_Boolean (Image : GNAT_String; + Switch : String; + Default : Boolean) + return Boolean + with Post => + (if Image in null or else Image.all = "" or else Image.all = Unset + then To_Boolean'Result = Default); + -- Convert a switch value to a boolean, if explicitly given, or use the + -- default otherwise. If not a valid boolean or empty, raise Checked_Error + -- with an appropriate error message. + end Alr.Commands; diff --git a/testsuite/drivers/driver/python_script.py b/testsuite/drivers/driver/python_script.py index 4e86be460..b646b301a 100644 --- a/testsuite/drivers/driver/python_script.py +++ b/testsuite/drivers/driver/python_script.py @@ -31,6 +31,9 @@ def prepare(self) -> dict: env = dict(os.environ) + # disable traceback from parent environment if it existed + env.pop('ALR_TRACEBACK_ENABLED', None) + config_dir = os.path.join(self.test_env['working_dir'], 'alr-config') prepare_env(config_dir, env) diff --git a/testsuite/tests/misc/env-traceback/test.py b/testsuite/tests/misc/env-traceback/test.py index fb2dceaea..8e4506e0a 100644 --- a/testsuite/tests/misc/env-traceback/test.py +++ b/testsuite/tests/misc/env-traceback/test.py @@ -23,6 +23,9 @@ def check_traceback(): # By default (no `-d` or ALR_TRACEBACK_ENABLED) we don't get a backtrace check_no_traceback() + +# Explicit disable + for val in ["", "0", "false", "no"]: os.environ['ALR_TRACEBACK_ENABLED'] = val check_no_traceback() diff --git a/testsuite/tests/printenv/last-build/test.py b/testsuite/tests/printenv/last-build/test.py index b05f7a09c..04006e93c 100644 --- a/testsuite/tests/printenv/last-build/test.py +++ b/testsuite/tests/printenv/last-build/test.py @@ -23,12 +23,19 @@ assert hash_release != hash_devel, "Hashes should be different" -# Check default printenv behavior +# Check default printenv behavior, which is to repeat last build settings p = run_alr("printenv") -assert_match(f".*LIBHELLO_ALIRE_PREFIX=[^\n]*{hash_release}", p.out) +assert_match(f".*LIBHELLO_ALIRE_PREFIX=[^\n]*{hash_devel}", p.out) + +# Check printenv --last-build behaviors -# Check printenv --last-build behavior p = run_alr("printenv", "--last-build") assert_match(f".*LIBHELLO_ALIRE_PREFIX=[^\n]*{hash_devel}", p.out) +p = run_alr("printenv", "--last-build=true") +assert_match(f".*LIBHELLO_ALIRE_PREFIX=[^\n]*{hash_devel}", p.out) + +p = run_alr("printenv", "--last-build=false") +assert_match(f".*LIBHELLO_ALIRE_PREFIX=[^\n]*{hash_release}", p.out) + print("SUCCESS") diff --git a/testsuite/tests/printenv/with-external/test.py b/testsuite/tests/printenv/with-external/test.py index 2528782b8..88c034c66 100644 --- a/testsuite/tests/printenv/with-external/test.py +++ b/testsuite/tests/printenv/with-external/test.py @@ -28,7 +28,7 @@ expected_gpr_path = '.*/libhello_0.9.0_filesystem' # Check the printenv output -assert_match('warn: Generating possibly incomplete environment' +assert_match('.*warn: Generating possibly incomplete environment' ' because of missing dependencies\n' # Note: this warning is via stderr so it's OK '.*'