1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import os
import pytest
import llnl.util.tty as tty
def test_get_timestamp(monkeypatch):
"""Ensure the results of get_timestamp are reasonable."""
# Debug disabled should return an empty string
monkeypatch.setattr(tty, "_debug", 0)
assert not tty.get_timestamp(False), "Expected an empty string"
# Debug disabled but force the timestamp should return a string
assert tty.get_timestamp(True), "Expected a timestamp/non-empty string"
pid_str = " {0}".format(os.getpid())
# Level 1 debugging should return a timestamp WITHOUT the pid
monkeypatch.setattr(tty, "_debug", 1)
out_str = tty.get_timestamp(False)
assert out_str and pid_str not in out_str, "Expected no PID in results"
# Level 2 debugging should also return a timestamp WITH the pid
monkeypatch.setattr(tty, "_debug", 2)
out_str = tty.get_timestamp(False)
assert out_str and pid_str in out_str, "Expected PID in results"
@pytest.mark.parametrize(
"msg,enabled,trace,newline",
[
("", False, False, False), # Nothing is output
(Exception(""), True, False, True), # Exception output
("trace", True, True, False), # stacktrace output
("newline", True, False, True), # newline in output
("no newline", True, False, False), # no newline output
],
)
def test_msg(capfd, monkeypatch, enabled, msg, trace, newline):
"""Ensure the output from msg with options is appropriate."""
# temporarily use the parameterized settings
monkeypatch.setattr(tty, "_msg_enabled", enabled)
monkeypatch.setattr(tty, "_stacktrace", trace)
expected = [msg if isinstance(msg, str) else "Exception: "]
if newline:
expected[0] = "{0}\n".format(expected[0])
if trace:
expected.insert(0, ".py")
tty.msg(msg, newline=newline)
out = capfd.readouterr()[0]
for msg in expected:
assert msg in out
@pytest.mark.parametrize(
"msg,trace,wrap",
[
(Exception(""), False, False), # Exception output
("trace", True, False), # stacktrace output
("wrap", False, True), # wrap in output
],
)
def test_info(capfd, monkeypatch, msg, trace, wrap):
"""Ensure the output from info with options is appropriate."""
# temporarily use the parameterized settings
monkeypatch.setattr(tty, "_stacktrace", trace)
expected = [msg if isinstance(msg, str) else "Exception: "]
if trace:
expected.insert(0, ".py")
extra = (
"This extra argument *should* make for a sufficiently long line"
" that needs to be wrapped if the option is enabled."
)
args = [msg, extra]
num_newlines = 3 if wrap else 2
tty.info(*args, wrap=wrap, countback=3)
out = capfd.readouterr()[0]
for msg in expected:
assert msg in out
assert out.count("\n") == num_newlines
|