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

Commit ce8c3ea

Browse files
Merge pull request #91 from codecov/thiago/logging/adding-asctime-to-logging
Adding asctime to logging
2 parents 7b27b28 + cf059ff commit ce8c3ea

File tree

9 files changed

+81
-53
lines changed

9 files changed

+81
-53
lines changed

codecov_cli/helpers/logging_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ class ColorFormatter(logging.Formatter):
3434
def format(self, record):
3535
if not record.exc_info:
3636
level = record.levelname.lower()
37+
asctime = self.formatTime(record, self.datefmt)
3738
msg = record.getMessage()
3839
if level in self.colors:
39-
prefix = click.style("{}: ".format(level), **self.colors[level])
40-
msg = "\n".join(prefix + x for x in msg.splitlines())
40+
prefix = click.style("{}".format(level), **self.colors[level])
41+
msg = "\n".join(
42+
f"{prefix} - {asctime} -- {x}" for x in msg.splitlines()
43+
)
4144
if hasattr(record, "extra_log_attributes"):
4245
msg += " --- " + json.dumps(
4346
record.extra_log_attributes, cls=JsonEncoder

tests/plugins/test_xcode.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from codecov_cli.plugins.xcode import XcodePlugin
7+
from tests.test_helpers import parse_outstreams_into_log_lines
78

89

910
class TestXcode(object):
@@ -24,10 +25,10 @@ def test_no_swift_data_found(self, mocker, tmp_path, capsys, use_verbose_option)
2425
xcode_plugin = XcodePlugin(derived_data_folder=tmp_path).run_preparation(
2526
collector=None
2627
)
27-
output = capsys.readouterr().err.splitlines()
28+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
2829
assert xcode_plugin is None
29-
assert f"debug: DerivedData folder: {tmp_path}" in output
30-
assert "warning: No swift data found." in output
30+
assert ("debug", f"DerivedData folder: {tmp_path}") in output
31+
assert ("warning", "No swift data found.") in output
3132

3233
def test_run_preparation_xcrun_not_installed(self, mocker, tmp_path, capsys):
3334
self.act_like_xcrun_is_not_installed(mocker)
@@ -43,11 +44,12 @@ def test_swift_data_found(self, mocker, tmp_path, capsys):
4344
dir.mkdir()
4445
(dir / "cov_data.profdata").touch()
4546
XcodePlugin(derived_data_folder=tmp_path).run_preparation(collector=None)
46-
output = capsys.readouterr().err.splitlines()
47+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
4748
expected = (
48-
'info: Running swift coverage on the following list of files: --- {"matched_paths": ["'
49+
"info",
50+
'Running swift coverage on the following list of files: --- {"matched_paths": ["'
4951
+ f"{dir}/cov_data.profdata"
50-
+ '"]}'
52+
+ '"]}',
5153
)
5254
assert expected in output
5355

tests/test_commit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from codecov_cli.services.commit import create_commit_logic, send_commit_data
66
from codecov_cli.types import RequestError, RequestResult, RequestResultWarning
7+
from tests.test_helpers import parse_outstreams_into_log_lines
78

89

910
def test_commit_command_with_warnings(mocker):
@@ -28,10 +29,10 @@ def test_commit_command_with_warnings(mocker):
2829
service="service",
2930
)
3031

31-
out_bytes = outstreams[0].getvalue().decode().splitlines()
32+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
3233
assert out_bytes == [
33-
"info: Commit creating process had 1 warning",
34-
"warning: Warning 1: somewarningmessage",
34+
("info", "Commit creating process had 1 warning"),
35+
("warning", "Warning 1: somewarningmessage"),
3536
]
3637
assert res == mock_send_commit_data.return_value
3738
mock_send_commit_data.assert_called_with(
@@ -71,8 +72,8 @@ def test_commit_command_with_error(mocker):
7172
service="service",
7273
)
7374

74-
out_bytes = outstreams[0].getvalue().decode().splitlines()
75-
assert out_bytes == ["error: Commit creating failed: Permission denied"]
75+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
76+
assert out_bytes == [("error", "Commit creating failed: Permission denied")]
7677
assert res == mock_send_commit_data.return_value
7778
mock_send_commit_data.assert_called_with(
7879
commit_sha="commit_sha",

tests/test_do-upload_command.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from codecov_cli.main import cli
44
from codecov_cli.services.legacy_upload import UploadSender
55
from codecov_cli.types import RequestError, RequestResult
6+
from tests.test_helpers import parse_outstreams_into_log_lines
67

78

89
def test_upload_missing_commit_sha(mocker):
@@ -34,5 +35,7 @@ def test_upload_raise_Z_option(mocker):
3435
cli, ["do-upload", "--fail-on-error", "--use-new-uploader=True"], obj={}
3536
)
3637
upload_sender.assert_called
37-
assert "error: Upload failed: Unauthorized" in result.output.splitlines()
38+
assert ("error", "Upload failed: Unauthorized") in parse_outstreams_into_log_lines(
39+
result.output
40+
)
3841
assert str(result) == "<Result SystemExit(1)>"

tests/test_helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def parse_outstreams_into_log_lines(this_data):
2+
return list(do_parse_outstreams_into_log_lines(this_data))
3+
4+
5+
def do_parse_outstreams_into_log_lines(this_data):
6+
if isinstance(this_data, bytes):
7+
this_data = this_data.decode()
8+
for x in this_data.splitlines():
9+
prefix_and_asctime, content = x.split(" -- ", 1)
10+
prefix, asctime = prefix_and_asctime.split(" - ")
11+
yield (prefix, content)

tests/test_report.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from codecov_cli.services.report import create_report_logic, send_create_report_request
66
from codecov_cli.types import RequestError, RequestResult, RequestResultWarning
7+
from tests.test_helpers import parse_outstreams_into_log_lines
78

89

910
def test_send_create_report_request_200(mocker):
@@ -55,10 +56,10 @@ def test_create_report_command_with_warnings(mocker):
5556
token="token",
5657
)
5758

58-
out_bytes = outstreams[0].getvalue().decode().splitlines()
59+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
5960
assert out_bytes == [
60-
"info: Report creating process had 1 warning",
61-
"warning: Warning 1: somewarningmessage",
61+
("info", "Report creating process had 1 warning"),
62+
("warning", "Warning 1: somewarningmessage"),
6263
]
6364
assert res == RequestResult(
6465
error=None,
@@ -99,8 +100,8 @@ def test_create_report_command_with_error(mocker):
99100
token="token",
100101
)
101102

102-
out_bytes = outstreams[0].getvalue().decode().splitlines()
103-
assert out_bytes == ["error: Report creating failed: Permission denied"]
103+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
104+
assert out_bytes == [("error", "Report creating failed: Permission denied")]
104105
assert res == RequestResult(
105106
error=RequestError(
106107
code="HTTP Error 403",

tests/test_report_results.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
send_reports_result_request,
1010
)
1111
from codecov_cli.types import RequestError, RequestResult, RequestResultWarning
12+
from tests.test_helpers import parse_outstreams_into_log_lines
1213

1314

1415
def test_report_results_command_with_warnings(mocker):
@@ -31,11 +32,12 @@ def test_report_results_command_with_warnings(mocker):
3132
token="token",
3233
)
3334

34-
out_bytes = outstreams[0].getvalue().decode().splitlines()
35+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
3536
assert out_bytes == [
36-
"info: Report results creating process had 1 warning",
37-
"warning: Warning 1: somewarningmessage",
37+
("info", "Report results creating process had 1 warning"),
38+
("warning", "Warning 1: somewarningmessage"),
3839
]
40+
3941
assert res == mock_send_reports_result_request.return_value
4042
mock_send_reports_result_request.assert_called_with(
4143
commit_sha="commit_sha",
@@ -70,8 +72,8 @@ def test_report_results_command_with_error(mocker):
7072
token="token",
7173
)
7274

73-
out_bytes = outstreams[0].getvalue().decode().splitlines()
74-
assert out_bytes == ["error: Report results creating failed: Permission denied"]
75+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
76+
assert out_bytes == [("error", "Report results creating failed: Permission denied")]
7577
assert res == mock_send_reports_result_request.return_value
7678
mock_send_reports_result_request.assert_called_with(
7779
commit_sha="commit_sha",
@@ -125,18 +127,19 @@ def test_get_report_results_200_completed(mocker, capsys):
125127
res = send_reports_result_get_request(
126128
"commit_sha", "report_code", "encoded_slug", "service", token
127129
)
128-
output = capsys.readouterr().err.splitlines()
130+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
129131
assert res.error is None
130132
assert res.warnings == []
131133
mocked_response.assert_called_once()
132134
assert (
133-
'info: Finished processing report results --- {"state": "failure", "message": "33.33% of diff hit (target 77.77%)"}'
134-
in output
135-
)
135+
"info",
136+
'Finished processing report results --- {"state": "failure", "message": "33.33% of diff hit (target 77.77%)"}',
137+
) in output
136138

137139

138140
@patch("codecov_cli.services.report.MAX_NUMBER_TRIES", 1)
139141
def test_get_report_results_200_pending(mocker, capsys):
142+
mocker.patch("codecov_cli.services.report.time.sleep")
140143
mocked_response = mocker.patch(
141144
"codecov_cli.services.report.requests.get",
142145
return_value=mocker.MagicMock(
@@ -147,11 +150,11 @@ def test_get_report_results_200_pending(mocker, capsys):
147150
res = send_reports_result_get_request(
148151
"commit_sha", "report_code", "encoded_slug", "service", token
149152
)
150-
output = capsys.readouterr().err.splitlines()
153+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
151154
assert res.error is None
152155
assert res.warnings == []
153156
mocked_response.assert_called_once()
154-
assert "info: Report with the given code is still being processed." in output
157+
assert ("info", "Report with the given code is still being processed.") in output
155158

156159

157160
def test_get_report_results_200_error(mocker, capsys):
@@ -165,14 +168,14 @@ def test_get_report_results_200_error(mocker, capsys):
165168
res = send_reports_result_get_request(
166169
"commit_sha", "report_code", "encoded_slug", "service", token
167170
)
168-
output = capsys.readouterr().err.splitlines()
171+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
169172
assert res.error is None
170173
assert res.warnings == []
171174
mocked_response.assert_called_once()
172175
assert (
173-
'error: An error occured while processing the report. Please try again later. --- {"response_status_code": 200, "state": "error", "result": {}}'
174-
in output
175-
)
176+
"error",
177+
'An error occured while processing the report. Please try again later. --- {"response_status_code": 200, "state": "error", "result": {}}',
178+
) in output
176179

177180

178181
def test_get_report_results_200_undefined_state(mocker, capsys):
@@ -186,11 +189,11 @@ def test_get_report_results_200_undefined_state(mocker, capsys):
186189
res = send_reports_result_get_request(
187190
"commit_sha", "report_code", "encoded_slug", "service", token
188191
)
189-
output = capsys.readouterr().err.splitlines()
192+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
190193
assert res.error is None
191194
assert res.warnings == []
192195
mocked_response.assert_called_once()
193-
assert "error: Please try again later." in output
196+
assert ("error", "Please try again later.") in output
194197

195198

196199
def test_get_report_results_401(mocker, capsys):
@@ -204,14 +207,14 @@ def test_get_report_results_401(mocker, capsys):
204207
res = send_reports_result_get_request(
205208
"commit_sha", "report_code", "encoded_slug", "service", token
206209
)
207-
output = capsys.readouterr().err.splitlines()
210+
output = parse_outstreams_into_log_lines(capsys.readouterr().err)
208211
assert res.error == RequestError(
209212
code="HTTP Error 401",
210213
description='{"detail": "Invalid token."}',
211214
params={},
212215
)
213216
mocked_response.assert_called_once()
214-
print(output)
215217
assert (
216-
'error: Getting report results failed: {"detail": "Invalid token."}' in output
217-
)
218+
"error",
219+
'Getting report results failed: {"detail": "Invalid token."}',
220+
) in output

tests/test_samples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def test_sample_analysis(input_filename, output_filename):
2626
with open(output_filename, "r") as file:
2727
expected_result = json.load(file)
2828
json_res = json.dumps(res.asdict())
29-
print(json_res)
3029
res_dict = json.loads(json_res)
3130
assert sorted(res_dict["result"].keys()) == sorted(expected_result["result"].keys())
3231
res_dict["result"]["functions"] = sorted(

tests/test_upload_service.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
UploadSendingResultWarning,
1313
)
1414
from codecov_cli.types import RequestResult
15+
from tests.test_helpers import parse_outstreams_into_log_lines
1516

1617

1718
def test_do_upload_logic_happy_path(mocker):
@@ -63,11 +64,12 @@ def test_do_upload_logic_happy_path(mocker):
6364
slug="slug",
6465
pull_request_number="pr",
6566
)
66-
out_bytes = outstreams[0].getvalue().decode().splitlines()
67+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
6768
assert out_bytes == [
68-
"info: Upload process had 1 warning",
69-
"warning: Warning 1: somewarningmessage",
69+
("info", "Upload process had 1 warning"),
70+
("warning", "Warning 1: somewarningmessage"),
7071
]
72+
7173
assert res == LegacyUploadSender.send_upload_data.return_value
7274
mock_select_preparation_plugins.assert_called_with(
7375
cli_config, ["first_plugin", "another", "forth"]
@@ -139,7 +141,7 @@ def test_do_upload_logic_dry_run(mocker):
139141
pull_request_number="pr",
140142
dry_run=True,
141143
)
142-
out_bytes = outstreams[0].getvalue().decode().splitlines()
144+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
143145
mock_select_coverage_file_finder.assert_called_with(None, None, None)
144146
mock_select_network_finder.assert_called_with(versioning_system)
145147
assert mock_generate_upload_data.call_count == 1
@@ -148,7 +150,7 @@ def test_do_upload_logic_dry_run(mocker):
148150
cli_config, ["first_plugin", "another", "forth"]
149151
)
150152
assert out_bytes == [
151-
"info: dry-run option activated. NOT sending data to Codecov.",
153+
("info", "dry-run option activated. NOT sending data to Codecov.")
152154
]
153155
assert res == RequestResult(
154156
error=None,
@@ -196,14 +198,17 @@ def test_do_upload_logic_verbose(mocker, use_verbose_option):
196198
pull_request_number="pr",
197199
dry_run=True,
198200
)
199-
out_bytes = outstreams[0].getvalue().decode().splitlines()
201+
out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue())
200202
assert out_bytes == [
201-
"debug: Selected uploader to use: <class "
202-
"'codecov_cli.services.legacy_upload.upload_sender.LegacyUploadSender'>",
203-
"info: dry-run option activated. NOT sending data to Codecov.",
204-
'debug: Process Upload complete. --- {"result": "RequestResult(error=None, '
205-
"warnings=None, status_code=200, text='Data NOT sent to Codecov because of "
206-
"dry-run option')\"}",
203+
(
204+
"debug",
205+
"Selected uploader to use: <class 'codecov_cli.services.legacy_upload.upload_sender.LegacyUploadSender'>",
206+
),
207+
("info", "dry-run option activated. NOT sending data to Codecov."),
208+
(
209+
"debug",
210+
'Process Upload complete. --- {"result": "RequestResult(error=None, warnings=None, status_code=200, text=\'Data NOT sent to Codecov because of dry-run option\')"}',
211+
),
207212
]
208213
assert res == RequestResult(
209214
error=None,

0 commit comments

Comments
 (0)