From eb56534d703b3e331068a4cdfc04380021b49128 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Thu, 27 Feb 2025 13:02:56 +0100 Subject: [PATCH] feat: proper `alr printenv` output even when sync needed (#1868) * feat: enable silent running for `alr printenv` * New test * Fix collateral damage * Silently skip if already debugging * Simplify test --- src/alire/alire-utils-user_input.adb | 20 ++++++++ src/alire/alire-utils-user_input.ads | 5 ++ src/alr/alr-commands-printenv.adb | 2 + .../misc/sync-manual-edit-indirect/test.py | 5 +- testsuite/tests/printenv/always-quiet/test.py | 50 +++++++++++++++++++ .../tests/printenv/always-quiet/test.yaml | 4 ++ .../toolchain/missing-tool-redeploy/test.py | 3 +- 7 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 testsuite/tests/printenv/always-quiet/test.py create mode 100644 testsuite/tests/printenv/always-quiet/test.yaml diff --git a/src/alire/alire-utils-user_input.adb b/src/alire/alire-utils-user_input.adb index ed7913c1..f576ec89 100644 --- a/src/alire/alire-utils-user_input.adb +++ b/src/alire/alire-utils-user_input.adb @@ -65,6 +65,26 @@ package body Alire.Utils.User_Input is end if; end Confirm_Solution_Changes; + --------------------------- + -- Enable_Silent_Running -- + --------------------------- + + procedure Enable_Silent_Running is + begin + -- If we are already at debug log level, we want to preserve it as we do + -- not want to miss any messages. + + if Alire.Log_Level < Simple_Logging.Debug then + Trace.Detail ("Enabling silent running"); + -- If we are running with -v, it's too late to remain silent anyway + + Alire.Log_Level := Simple_Logging.Error; + CLIC.User_Input.Not_Interactive := True; + else + Trace.Debug ("Cannot enable silent running when log level is debug"); + end if; + end Enable_Silent_Running; + ------------------------------- -- To_Absolute_From_Portable -- ------------------------------- diff --git a/src/alire/alire-utils-user_input.ads b/src/alire/alire-utils-user_input.ads index 88e166b3..6e0d64c0 100644 --- a/src/alire/alire-utils-user_input.ads +++ b/src/alire/alire-utils-user_input.ads @@ -2,6 +2,11 @@ with Alire.Solutions.Diffs; package Alire.Utils.User_Input is + procedure Enable_Silent_Running; + -- Configure for non-interactive silent output, only Trace.Always goes to + -- console. If current log level is already Debug, this will silently do + -- nothing, to allow debug logs of parts that would normally be silent. + function Confirm_Solution_Changes (Changes : Solutions.Diffs.Diff; Changed_Only : Boolean := not Alire.Detailed; diff --git a/src/alr/alr-commands-printenv.adb b/src/alr/alr-commands-printenv.adb index d77b6a8f..d22cf5bd 100644 --- a/src/alr/alr-commands-printenv.adb +++ b/src/alr/alr-commands-printenv.adb @@ -1,6 +1,7 @@ with Alire.Crate_Configuration; with Alire.Environment; with Alire.Platforms; +with Alire.Utils.User_Input; package body Alr.Commands.Printenv is @@ -16,6 +17,7 @@ package body Alr.Commands.Printenv is is Enabled : Natural := 0; begin + Alire.Utils.User_Input.Enable_Silent_Running; Cmd.Forbids_Structured_Output; if Args.Count /= 0 then diff --git a/testsuite/tests/misc/sync-manual-edit-indirect/test.py b/testsuite/tests/misc/sync-manual-edit-indirect/test.py index 230bbe1a..9920ab1e 100644 --- a/testsuite/tests/misc/sync-manual-edit-indirect/test.py +++ b/testsuite/tests/misc/sync-manual-edit-indirect/test.py @@ -58,8 +58,9 @@ for cmd in ['build', 'pin', 'run', 'show', 'with', 'printenv']: p = run_alr(cmd, quiet=False) # If no error was reported, then we should be okay. Still, check that the - # update happened as expected: - assert_substring("Changes detected in pinned dependencies", p.out) + # update happened as expected (except for printenv, that syncs silently): + if cmd != "printenv": + assert_substring("Changes detected in pinned dependencies", p.out) # Go to where we started os.chdir("..") diff --git a/testsuite/tests/printenv/always-quiet/test.py b/testsuite/tests/printenv/always-quiet/test.py new file mode 100644 index 00000000..d17ab51e --- /dev/null +++ b/testsuite/tests/printenv/always-quiet/test.py @@ -0,0 +1,50 @@ +""" +Verify that no extraneous output is printed during `alr printenv` even when +sync/update needed. +""" + +import os +import shutil +from drivers.alr import alr_lockfile, alr_with, init_local_crate, run_alr, run_alr_interactive +from drivers.asserts import assert_eq + + +def check_output(output : str): + # Split in lines and verify that every line is an export + for line in output.splitlines(): + assert line.startswith("export ") + + +# We create a new crate with some dependencies and delete the `alire` forder. +# This forces a sync that normally would print stuff that is unwanted during +# `alr printenv`. + +init_local_crate() +alr_with("hello") + +shutil.rmtree("alire") + +# This one will perform a silent sync +p1 = run_alr("printenv", "--unix", quiet=False) # This one would fail <2.1 + +# Verify the sync happened +assert os.path.isfile(alr_lockfile()) + +# A second, quiet printenv should always work properly +p2 = run_alr("printenv", "--unix", quiet=True) + +# Output should match +assert_eq(p1.out, p2.out) + +# Also check that every line is an export and not something else. +# We do not check the specific contents as they vary between OSes and build modes. +check_output(p1.out) + +# Test that a non-interactive run also completes without trying to interact or +# with unexpected output + +p3 = run_alr_interactive(["printenv", "--unix"], [], []) +assert_eq(p2.out, p3) + + +print("SUCCESS") diff --git a/testsuite/tests/printenv/always-quiet/test.yaml b/testsuite/tests/printenv/always-quiet/test.yaml new file mode 100644 index 00000000..8929d590 --- /dev/null +++ b/testsuite/tests/printenv/always-quiet/test.yaml @@ -0,0 +1,4 @@ +driver: python-script +indexes: + basic_index: + in_fixtures: true diff --git a/testsuite/tests/toolchain/missing-tool-redeploy/test.py b/testsuite/tests/toolchain/missing-tool-redeploy/test.py index 0eed229c..eb681283 100644 --- a/testsuite/tests/toolchain/missing-tool-redeploy/test.py +++ b/testsuite/tests/toolchain/missing-tool-redeploy/test.py @@ -19,7 +19,8 @@ init_local_crate() rmtree(os.path.join(alr_settings_dir(), "cache")) # This should not fail. A message should warn of redeployments happening. -p = run_alr("printenv", quiet=False) +# Verbose level required to work-around silent sync during `printenv`. +p = run_alr("-vv", "printenv", quiet=False) assert_match(".*Tool .* is missing, redeploying", p.out) print("SUCCESS") -- 2.39.5