Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 1772372

Browse files
committed
🐛 Ensure that no-token case is actually logged
Prior to this patch, there was an unreachable code path that seems to have been intended for marking the case when no token is provided with the `"NOTOKEN"` string in the debug log output. However the logic behind choosing to show it contained two opposite checks. Said place in the logging utils, didn't have any coverage either. This change updates the code to make all of its branches reachable. Moreover, it adds test cases for all the branches that remain in the implementation.
1 parent 992826d commit 1772372

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

codecov_cli/helpers/logging_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ def format(self, record):
4242
f"{prefix} - {asctime} -- {x}" for x in msg.splitlines()
4343
)
4444
if hasattr(record, "extra_log_attributes"):
45-
token = record.extra_log_attributes.get("token")
46-
if token:
47-
record.extra_log_attributes["token"] = (
48-
"NOTOKEN" if not token else (str(token)[:1] + 18 * "*")
49-
)
45+
passed_token_attribute = record.extra_log_attributes.get("token")
46+
record.extra_log_attributes["token"] = (
47+
"NOTOKEN"
48+
if passed_token_attribute is None
49+
else (str(passed_token_attribute)[:1] + 18 * "*")
50+
)
5051
msg += " --- " + json.dumps(
5152
record.extra_log_attributes, cls=JsonEncoder
5253
)

tests/commands/test_upload_token_discovery.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,86 @@ def test_no_cli_token_config_fallback(
4444
CliRunner().invoke(cli, ["do-upload", "--commit-sha=deadbeef"], obj={})
4545

4646
assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value"
47+
48+
49+
def test_no_token_anywhere(
50+
mocker: MockerFixture,
51+
monkeypatch: MonkeyPatch,
52+
tmp_path: Path,
53+
) -> None:
54+
"""Test that a missing token produces `NOTOKEN` in logs."""
55+
# NOTE: The pytest's `caplog` fixture is not used in this test as it
56+
# NOTE: doesn't play well with Click's testing CLI runner, and does
57+
# NOTE: not capture any log entries for mysterious reasons.
58+
#
59+
# Refs:
60+
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563
61+
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608
62+
monkeypatch.chdir(tmp_path)
63+
64+
mocker.patch.object(upload, "do_upload_logic")
65+
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic")
66+
debug_log_spy = mocker.spy(upload.logger, "debug")
67+
68+
cov_upload_cmd_output = (
69+
CliRunner()
70+
.invoke(
71+
cli,
72+
[
73+
"-v", # <- NOTE: this is the only way to turn on debug logger
74+
"do-upload",
75+
"--commit-sha=deadbeef",
76+
],
77+
obj={},
78+
)
79+
.output
80+
)
81+
82+
assert (
83+
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"]
84+
== "NOTOKEN"
85+
)
86+
assert '"token": "NOTOKEN"' in cov_upload_cmd_output
87+
assert do_upload_cmd_spy.call_args[-1]["token"] is None
88+
89+
90+
def test_cli_token_masked_in_logs(
91+
mocker: MockerFixture,
92+
monkeypatch: MonkeyPatch,
93+
tmp_path: Path,
94+
) -> None:
95+
"""Test that a present token is masked in logs."""
96+
# NOTE: The pytest's `caplog` fixture is not used in this test as it
97+
# NOTE: doesn't play well with Click's testing CLI runner, and does
98+
# NOTE: not capture any log entries for mysterious reasons.
99+
#
100+
# Refs:
101+
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563
102+
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608
103+
monkeypatch.chdir(tmp_path)
104+
105+
mocker.patch.object(upload, "do_upload_logic")
106+
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic")
107+
debug_log_spy = mocker.spy(upload.logger, "debug")
108+
109+
cov_upload_cmd_output = (
110+
CliRunner()
111+
.invoke(
112+
cli,
113+
[
114+
"-v", # <- NOTE: this is the only way to turn on debug logger
115+
"do-upload",
116+
"--commit-sha=deadbeef",
117+
"--token=sentinel-value",
118+
],
119+
obj={},
120+
)
121+
.output
122+
)
123+
124+
assert (
125+
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"]
126+
== "s" + "*" * 18
127+
)
128+
assert f'"token": "s{"*" * 18}"' in cov_upload_cmd_output
129+
assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value"

0 commit comments

Comments
 (0)