Skip to content

Commit eb56534

Browse files
authored
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
1 parent 32e217a commit eb56534

File tree

7 files changed

+86
-3
lines changed

7 files changed

+86
-3
lines changed

src/alire/alire-utils-user_input.adb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ package body Alire.Utils.User_Input is
6565
end if;
6666
end Confirm_Solution_Changes;
6767

68+
---------------------------
69+
-- Enable_Silent_Running --
70+
---------------------------
71+
72+
procedure Enable_Silent_Running is
73+
begin
74+
-- If we are already at debug log level, we want to preserve it as we do
75+
-- not want to miss any messages.
76+
77+
if Alire.Log_Level < Simple_Logging.Debug then
78+
Trace.Detail ("Enabling silent running");
79+
-- If we are running with -v, it's too late to remain silent anyway
80+
81+
Alire.Log_Level := Simple_Logging.Error;
82+
CLIC.User_Input.Not_Interactive := True;
83+
else
84+
Trace.Debug ("Cannot enable silent running when log level is debug");
85+
end if;
86+
end Enable_Silent_Running;
87+
6888
-------------------------------
6989
-- To_Absolute_From_Portable --
7090
-------------------------------

src/alire/alire-utils-user_input.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ with Alire.Solutions.Diffs;
22

33
package Alire.Utils.User_Input is
44

5+
procedure Enable_Silent_Running;
6+
-- Configure for non-interactive silent output, only Trace.Always goes to
7+
-- console. If current log level is already Debug, this will silently do
8+
-- nothing, to allow debug logs of parts that would normally be silent.
9+
510
function Confirm_Solution_Changes
611
(Changes : Solutions.Diffs.Diff;
712
Changed_Only : Boolean := not Alire.Detailed;

src/alr/alr-commands-printenv.adb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
with Alire.Crate_Configuration;
22
with Alire.Environment;
33
with Alire.Platforms;
4+
with Alire.Utils.User_Input;
45

56
package body Alr.Commands.Printenv is
67

@@ -16,6 +17,7 @@ package body Alr.Commands.Printenv is
1617
is
1718
Enabled : Natural := 0;
1819
begin
20+
Alire.Utils.User_Input.Enable_Silent_Running;
1921
Cmd.Forbids_Structured_Output;
2022

2123
if Args.Count /= 0 then

testsuite/tests/misc/sync-manual-edit-indirect/test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ def set_up():
5858
p = run_alr(cmd, quiet=False)
5959

6060
# If no error was reported, then we should be okay. Still, check that the
61-
# update happened as expected:
62-
assert_substring("Changes detected in pinned dependencies", p.out)
61+
# update happened as expected (except for printenv, that syncs silently):
62+
if cmd != "printenv":
63+
assert_substring("Changes detected in pinned dependencies", p.out)
6364

6465
# Go to where we started
6566
os.chdir("..")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Verify that no extraneous output is printed during `alr printenv` even when
3+
sync/update needed.
4+
"""
5+
6+
import os
7+
import shutil
8+
from drivers.alr import alr_lockfile, alr_with, init_local_crate, run_alr, run_alr_interactive
9+
from drivers.asserts import assert_eq
10+
11+
12+
def check_output(output : str):
13+
# Split in lines and verify that every line is an export
14+
for line in output.splitlines():
15+
assert line.startswith("export ")
16+
17+
18+
# We create a new crate with some dependencies and delete the `alire` forder.
19+
# This forces a sync that normally would print stuff that is unwanted during
20+
# `alr printenv`.
21+
22+
init_local_crate()
23+
alr_with("hello")
24+
25+
shutil.rmtree("alire")
26+
27+
# This one will perform a silent sync
28+
p1 = run_alr("printenv", "--unix", quiet=False) # This one would fail <2.1
29+
30+
# Verify the sync happened
31+
assert os.path.isfile(alr_lockfile())
32+
33+
# A second, quiet printenv should always work properly
34+
p2 = run_alr("printenv", "--unix", quiet=True)
35+
36+
# Output should match
37+
assert_eq(p1.out, p2.out)
38+
39+
# Also check that every line is an export and not something else.
40+
# We do not check the specific contents as they vary between OSes and build modes.
41+
check_output(p1.out)
42+
43+
# Test that a non-interactive run also completes without trying to interact or
44+
# with unexpected output
45+
46+
p3 = run_alr_interactive(["printenv", "--unix"], [], [])
47+
assert_eq(p2.out, p3)
48+
49+
50+
print("SUCCESS")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
driver: python-script
2+
indexes:
3+
basic_index:
4+
in_fixtures: true

testsuite/tests/toolchain/missing-tool-redeploy/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
rmtree(os.path.join(alr_settings_dir(), "cache"))
2020

2121
# This should not fail. A message should warn of redeployments happening.
22-
p = run_alr("printenv", quiet=False)
22+
# Verbose level required to work-around silent sync during `printenv`.
23+
p = run_alr("-vv", "printenv", quiet=False)
2324
assert_match(".*Tool .* is missing, redeploying", p.out)
2425

2526
print("SUCCESS")

0 commit comments

Comments
 (0)