Skip to content

Commit

Permalink
make new behavior the default with fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Oct 2, 2023
1 parent c5f3756 commit 4f0df6b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 27 deletions.
28 changes: 11 additions & 17 deletions src/alr/alr-commands-config.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
with AAA.Enum_Tools;

with Alire.Config;
with Alire.Config.Edit;
with Alire.Root;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/alr/alr-commands-printenv.adb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ with Alire.Platforms;

package body Alr.Commands.Printenv is

Last_Build_Switch : constant String := "--last-build";

-------------
-- Execute --
-------------
Expand All @@ -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;
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/alr/alr-commands-printenv.ads
Original file line number Diff line number Diff line change
Expand Up @@ -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;
25 changes: 25 additions & 0 deletions src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
22 changes: 20 additions & 2 deletions src/alr/alr-commands.ads
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
private with AAA.Enum_Tools;
with AAA.Strings;

with Alire.Directories;
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
3 changes: 3 additions & 0 deletions testsuite/drivers/driver/python_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions testsuite/tests/misc/env-traceback/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 10 additions & 3 deletions testsuite/tests/printenv/last-build/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion testsuite/tests/printenv/with-external/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'.*'
Expand Down

0 comments on commit 4f0df6b

Please sign in to comment.