Skip to content

Commit 8899dce

Browse files
committed
relink.py: Timing now shows in quiet mode.
1 parent b3dd56f commit 8899dce

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

relink.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
# Set up logger
2020
logger = logging.getLogger(__name__)
2121

22+
# Define a custom log level that always prints
23+
ALWAYS = logging.CRITICAL * 2
24+
logging.addLevelName(ALWAYS, "ALWAYS")
25+
26+
27+
def always(self, message, *args, **kwargs):
28+
"""Log message that always appears regardless of log level."""
29+
if self.isEnabledFor(ALWAYS):
30+
# pylint: disable=protected-access
31+
self._log(ALWAYS, message, args, **kwargs)
32+
33+
34+
logging.Logger.always = always
35+
2236

2337
def find_and_replace_owned_files(source_dir, target_dir, username, dry_run=False):
2438
"""
@@ -234,7 +248,7 @@ def main():
234248

235249
if args.timing:
236250
elapsed_time = time.time() - start_time
237-
logger.info("Execution time: %.2f seconds", elapsed_time)
251+
logger.always("Execution time: %.2f seconds", elapsed_time)
238252

239253

240254
if __name__ == "__main__":

tests/relink/test_timing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,40 @@ def test_timing_logging(tmp_path, caplog, use_timing, should_log_timing):
5656
assert "seconds" in caplog.text
5757
else:
5858
assert "Execution time:" not in caplog.text
59+
60+
61+
def test_timing_shows_in_quiet_mode(tmp_path, caplog):
62+
"""Test that timing message is shown even when --quiet flag is used."""
63+
# Create real directories
64+
source_dir = tmp_path / "source"
65+
target_dir = tmp_path / "target"
66+
source_dir.mkdir()
67+
target_dir.mkdir()
68+
69+
# Create a file
70+
source_file = source_dir / "test_file.txt"
71+
target_file = target_dir / "test_file.txt"
72+
source_file.write_text("source")
73+
target_file.write_text("target")
74+
75+
# Build argv with both --timing and --quiet flags
76+
test_argv = [
77+
"relink.py",
78+
"--source-root",
79+
str(source_dir),
80+
"--target-root",
81+
str(target_dir),
82+
"--timing",
83+
"--quiet",
84+
]
85+
86+
with patch("sys.argv", test_argv):
87+
with caplog.at_level(logging.WARNING):
88+
# Call main() which includes the timing logic
89+
relink.main()
90+
91+
# Verify timing message appears even in quiet mode
92+
assert "Execution time:" in caplog.text
93+
assert "seconds" in caplog.text
94+
# Verify that INFO messages are suppressed
95+
assert "Searching for files owned by" not in caplog.text

0 commit comments

Comments
 (0)