From 1a67a0d52f2e3fae9a44ee6c287bffd9eba93e9a Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Mon, 8 Jun 2020 17:32:15 +0200 Subject: [PATCH] Add a -f, --force global switch (#435) This global modifier is available in Alire.Force. Other candidate locations, like Alire.Config, Alire.Errors, Alire.Utils.User_Input cannot be used because they bring in non-preelaborable units and it snowballs from there. The recommended usage is through Alire.Recoverable_Error, which will warn or raise appropriately depending on the flag. The boolean Alire.Force is also available for less straightforward situations (interaction defaults for example). --- src/alire/alire.adb | 37 ++++++++++++++------- src/alire/alire.ads | 11 ++++++ src/alr/alr-commands-version.adb | 7 +++- src/alr/alr-commands.adb | 6 ++++ testsuite/tests/misc/force-switch/test.py | 18 ++++++++++ testsuite/tests/misc/force-switch/test.yaml | 1 + 6 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 testsuite/tests/misc/force-switch/test.py create mode 100644 testsuite/tests/misc/force-switch/test.yaml diff --git a/src/alire/alire.adb b/src/alire/alire.adb index 42a72be8..de84e018 100644 --- a/src/alire/alire.adb +++ b/src/alire/alire.adb @@ -110,18 +110,6 @@ package body Alire is return +Err = ""; end Is_Valid_Name; - ------------------- - -- Raise_Checked_Error -- - ------------------- - - procedure Raise_Checked_Error (Msg : String) is - begin - if Log_Debug then - Err_Log (Msg); - end if; - raise Checked_Error with Errors.Set (Msg); - end Raise_Checked_Error; - --------------------- -- Outcome_Failure -- --------------------- @@ -172,4 +160,29 @@ package body Alire is end if; end Outcome_From_Exception; + ------------------------- + -- Raise_Checked_Error -- + ------------------------- + + procedure Raise_Checked_Error (Msg : String) is + begin + if Log_Debug then + Err_Log (Msg); + end if; + raise Checked_Error with Errors.Set (Msg); + end Raise_Checked_Error; + + ----------------------- + -- Recoverable_Error -- + ----------------------- + + procedure Recoverable_Error (Msg : String) is + begin + if Force then + Trace.Warning (Msg); + else + Raise_Checked_Error (Msg); + end if; + end Recoverable_Error; + end Alire; diff --git a/src/alire/alire.ads b/src/alire/alire.ads index f9aa24be..cfce65d3 100644 --- a/src/alire/alire.ads +++ b/src/alire/alire.ads @@ -172,6 +172,13 @@ package Alire with Preelaborate is -- The exception stack trace will be dumped at debug level. -- If message is empty, message will be Ex exception message. + ---------------------- + -- Error generation -- + ---------------------- + + Force : aliased Boolean := False; + -- When True, recoverable errors are demoted to warnings and we keep going + procedure Assert (Result : Outcome'Class); -- Does nothing for successful outcomes. Raises Checked_Error with the -- corresponding message set in Alire.Errors otherwise. @@ -181,6 +188,10 @@ package Alire with Preelaborate is -- message (Msg) and raise Checked_Error. There is no limitation on the -- length of Msg. + procedure Recoverable_Error (Msg : String); + -- When Force, emit a warning and return normally. When not Force call + -- Raise_Checked_Error instead. + --------------- -- LOGGING -- --------------- diff --git a/src/alr/alr-commands-version.adb b/src/alr/alr-commands-version.adb index 79df13c9..b14a6b8c 100644 --- a/src/alr/alr-commands-version.adb +++ b/src/alr/alr-commands-version.adb @@ -1,5 +1,5 @@ with Alire.Properties; -with Alire.Utils; +with Alire.Utils.User_Input; with Alr.Files; with Alr.OS_Lib; @@ -25,6 +25,11 @@ package body Alr.Commands.Version is Trace.Always ("config folder is " & Paths.Alr_Config_Folder); Trace.Always ("source folder is " & Paths.Alr_Source_Folder); + Trace.Always + ("interaction flags are:" + & " force:" & Alire.Force'Img + & " not-interactive:" & Alire.Utils.User_Input.Not_Interactive'Img); + if not Root.Current.Is_Valid then Trace.Always ("alr root is empty"); else diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 8b2a47a0..791682da 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -180,6 +180,12 @@ package body Alr.Commands is Command_Line_Config_Path'Access, "-c=", "--config=", "Override configuration folder location"); + + Define_Switch (Config, + Alire.Force'Access, + "-f", "--force", + "Keep going after a recoverable troublesome situation"); + Define_Switch (Config, Help_Switch'Access, "-h", "--help", diff --git a/testsuite/tests/misc/force-switch/test.py b/testsuite/tests/misc/force-switch/test.py new file mode 100644 index 00000000..c778c754 --- /dev/null +++ b/testsuite/tests/misc/force-switch/test.py @@ -0,0 +1,18 @@ +""" +Verify the global --force switch is in effect +""" + +from drivers.alr import run_alr +from drivers.asserts import assert_match + +import re + +assert_match('.*interaction flags are:.*force:TRUE.*', + run_alr('version', '--force').out, + flags=re.S) + +assert_match('.*interaction flags are:.*force:TRUE.*', + run_alr('version', '-f').out, + flags=re.S) + +print('SUCCESS') diff --git a/testsuite/tests/misc/force-switch/test.yaml b/testsuite/tests/misc/force-switch/test.yaml new file mode 100644 index 00000000..32c747b3 --- /dev/null +++ b/testsuite/tests/misc/force-switch/test.yaml @@ -0,0 +1 @@ +driver: python-script -- 2.39.5