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

Commit a06cbdf

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 65a32bc commit a06cbdf

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-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
)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Tests ensuring that an env-provided token can be found."""
2+
3+
from pathlib import Path
4+
5+
from click.testing import CliRunner
6+
from pytest import MonkeyPatch
7+
from pytest_mock import MockerFixture
8+
9+
from codecov_cli.commands import upload
10+
from codecov_cli.main import cli
11+
12+
13+
def test_no_token_anywhere(
14+
mocker: MockerFixture,
15+
monkeypatch: MonkeyPatch,
16+
tmp_path: Path,
17+
) -> None:
18+
"""Test that a missing token produces `NOTOKEN` in logs."""
19+
# NOTE: The pytest's `caplog` fixture is not used in this test as it
20+
# NOTE: doesn't play well with Click's testing CLI runner, and does
21+
# NOTE: not capture any log entries for mysterious reasons.
22+
#
23+
# Refs:
24+
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563
25+
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608
26+
monkeypatch.chdir(tmp_path)
27+
28+
mocker.patch.object(upload, "do_upload_logic")
29+
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic")
30+
debug_log_spy = mocker.spy(upload.logger, "debug")
31+
32+
cov_upload_cmd_output = (
33+
CliRunner()
34+
.invoke(
35+
cli,
36+
[
37+
"-v", # <- NOTE: this is the only way to turn on debug logger
38+
"do-upload",
39+
"--commit-sha=deadbeef",
40+
],
41+
obj={},
42+
)
43+
.output
44+
)
45+
46+
assert (
47+
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"]
48+
== "NOTOKEN"
49+
)
50+
assert '"token": "NOTOKEN"' in cov_upload_cmd_output
51+
assert do_upload_cmd_spy.call_args[-1]["token"] is None
52+
53+
54+
def test_cli_token_masked_in_logs(
55+
mocker: MockerFixture,
56+
monkeypatch: MonkeyPatch,
57+
tmp_path: Path,
58+
) -> None:
59+
"""Test that a present token is masked in logs."""
60+
# NOTE: The pytest's `caplog` fixture is not used in this test as it
61+
# NOTE: doesn't play well with Click's testing CLI runner, and does
62+
# NOTE: not capture any log entries for mysterious reasons.
63+
#
64+
# Refs:
65+
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563
66+
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608
67+
monkeypatch.chdir(tmp_path)
68+
69+
mocker.patch.object(upload, "do_upload_logic")
70+
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic")
71+
debug_log_spy = mocker.spy(upload.logger, "debug")
72+
73+
cov_upload_cmd_output = (
74+
CliRunner()
75+
.invoke(
76+
cli,
77+
[
78+
"-v", # <- NOTE: this is the only way to turn on debug logger
79+
"do-upload",
80+
"--commit-sha=deadbeef",
81+
"--token=sentinel-value",
82+
],
83+
obj={},
84+
)
85+
.output
86+
)
87+
88+
assert (
89+
debug_log_spy.call_args[1]["extra"]["extra_log_attributes"]["token"]
90+
== "s" + "*" * 18
91+
)
92+
assert f'"token": "s{"*" * 18}"' in cov_upload_cmd_output
93+
assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value"

0 commit comments

Comments
 (0)