Skip to content

Commit 7f6d8ae

Browse files
committed
Split coverage action into two
1 parent 38aa0f6 commit 7f6d8ae

7 files changed

Lines changed: 67 additions & 39 deletions

File tree

.github/workflows/coverage.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
name: Post Coverage Commit
3+
4+
on:
5+
workflow_run:
6+
workflows: ["Pytest"]
7+
types:
8+
- completed
9+
10+
jobs:
11+
coverage:
12+
runs-on: ubuntu-latest
13+
if: github.event.workflow_run.event == "pull_request" && github.event.workflow_run.conclusion == "success"
14+
permissions:
15+
pull-requests: write
16+
contents: write
17+
actions: read
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- uses: actions/download-artifact@v4
22+
id: download
23+
with:
24+
pattern: coverage-*
25+
merge-multiple: true
26+
- name: Re-add relative so the action is happy
27+
run: |
28+
echo "[run]" >> .coveragerc
29+
echo "relative_files = true" >> .coveragerc
30+
- name: Python Coverage Comment
31+
uses: py-cov-action/python-coverage-comment-action@v3.24
32+
with:
33+
GITHUB_TOKEN: ${{ github.token }}
34+
MERGE_COVERAGE_FILES: true
35+
GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}

.github/workflows/pytest.yaml

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
pytest:
1111

1212
runs-on: ${{ matrix.os }}
13+
permissions:
1314
strategy:
1415
matrix:
1516
os: [ubuntu-latest, macos-latest, windows-latest]
@@ -40,32 +41,10 @@ jobs:
4041
hatch run dev:pytest
4142
env:
4243
COVERAGE_FILE: ".coverage.${{ matrix.os }}.${{ matrix.python-version }}"
43-
- name: Store coverage file
44-
uses: actions/upload-artifact@v4
45-
with:
46-
name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
47-
path: .coverage.${{ matrix.os }}.${{ matrix.python-version }}
44+
- name: Store Pull Request comment to be posted
45+
uses: actions/upload-artifact@v4
46+
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
47+
with:
48+
name: python-coverage-comment-action
49+
path: python-coverage-comment-action.txt
4850

49-
coverage:
50-
runs-on: ubuntu-latest
51-
needs: pytest
52-
permissions:
53-
pull-requests: write
54-
contents: write
55-
steps:
56-
- uses: actions/checkout@v4
57-
58-
- uses: actions/download-artifact@v4
59-
id: download
60-
with:
61-
pattern: coverage-*
62-
merge-multiple: true
63-
- name: Re-add relative so the action is happy
64-
run: |
65-
echo "[run]" >> .coveragerc
66-
echo "relative_files = true" >> .coveragerc
67-
- name: Python Coverage Comment
68-
uses: py-cov-action/python-coverage-comment-action@v3.24
69-
with:
70-
GITHUB_TOKEN: ${{ github.token }}
71-
MERGE_COVERAGE_FILES: true

annotated_logger/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ def __init__( # noqa: PLR0913
340340
self.formatter = formatter or JsonFormatter(
341341
"%(created)s %(levelname)s %(name)s %(message)s" # pragma: no mutate
342342
)
343+
self.handlers = [handler]
343344
handler.setFormatter(self.formatter)
344345
self.max_length = max_length
345346

@@ -356,7 +357,8 @@ def _generate_logger(
356357
f"{self.logger_root_name}.{uuid.uuid4()}" # pragma: no mutate
357358
)
358359

359-
logger.addHandler(handler)
360+
for handler_item in self.handlers:
361+
logger.addHandler(handler_item)
360362

361363
annotated_filter = self.generate_filter(function=function, cls=cls)
362364

annotated_logger/mocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ def assert_logged(
244244

245245

246246
@pytest.fixture()
247-
def annotated_logger_mock(mocker: MagicMock) -> AnnotatedLogMock:
247+
def annotated_logger_mock(mocker: MagicMock, annotated_logger_object: AnnotatedLogger) -> AnnotatedLogMock:
248248
"""Fixture for a mock of the annotated logger."""
249+
import pdb;pdb.set_trace()
249250
return mocker.patch(
250251
"annotated_logger.handler",
251252
new_callable=AnnotatedLogMock,

example/calculator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def runtime(_record: logging.LogRecord) -> str:
1919
return "this function is called every time"
2020

2121

22-
annotated_logger = AnnotatedLogger(
22+
ann_logger = AnnotatedLogger(
2323
annotations={
2424
"extra": "new data",
2525
"nested_extra": {"nested_key": {"double_nested_key": "value"}},
@@ -32,10 +32,10 @@ def runtime(_record: logging.LogRecord) -> str:
3232
RemoverPlugin("taskName"),
3333
NestedRemoverPlugin(["double_nested_key"]),
3434
],
35-
name="annotated_logger.example",
35+
name="annotated_logger.calculator",
3636
)
3737

38-
annotate_logs = annotated_logger.annotate_logs
38+
annotate_logs = ann_logger.annotate_logs
3939

4040
Number = int | float
4141

@@ -90,7 +90,7 @@ def check_prediction_crashed_correctly(
9090
"Prediction result", extra={"result": success != prediction}
9191
)
9292

93-
@annotated_logger.annotate_logs(
93+
@ann_logger.annotate_logs(
9494
success_info=False,
9595
pre_call=check_zero_division,
9696
_typing_requested=True,
@@ -102,6 +102,7 @@ def divide(self, annotated_logger: AnnotatedAdapter) -> Number:
102102
"If you divide by zero you'll create a singularity in the fabric of space-time!", # noqa: E501
103103
extra={"joke": True},
104104
)
105+
import pdb;pdb.set_trace()
105106
try:
106107
return self.first / self.second
107108
except ZeroDivisionError:

notes.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Should support the ability to configure via a logging config.
2+
3+
The AnnotatedLogger class would take params that let it build/override parts of the config.
4+
5+
Need to sort out how we do the mock on the handler then. Have the mock fixture pull in a fixture for what handler to do and then if you don't do that you can override that fixture? How does it work with multiple handlers?
6+
7+
8+
The overall goal is to be able to support the case where you want to configure multiple output streams (handlers) with different filters. AKA, output with a different log level that filters/formats for actions notifications at the same time as a normal logger
9+
10+
Also, move base_attribute support into the plugin base and also store the values we add in the annotated (annotated, action and so on). Then make a function that lets a plugin ask for the bits of a record that were added by the caller or the caller and the annotated_logger.

test/test_decorator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import test.demo
55
from annotated_logger.mocks import AnnotatedLogMock
66
from example.api import ApiClient
7-
from example.calculator import BoomError, Calculator, annotated_logger
7+
from example.calculator import BoomError, Calculator, ann_logger
88
from example.default import DefaultExample, var_args_and_kwargs_provided_outer
99

1010

@@ -209,8 +209,8 @@ def test_debug(self, annotated_logger_mock):
209209
def test_runtime_not_cached(self, annotated_logger_mock, mocker):
210210
runtime_mock = mocker.Mock(name="runtime_not_cached")
211211
runtime_mock.side_effect = ["first", "second", "third", "fourth"]
212-
runtime_annotations = annotated_logger.runtime_annotations
213-
annotated_logger.runtime_annotations = {"runtime": runtime_mock}
212+
runtime_annotations = ann_logger.runtime_annotations
213+
ann_logger.runtime_annotations = {"runtime": runtime_mock}
214214
calc = Calculator(12, 13)
215215
calc.subtract()
216216
annotated_logger_mock.assert_logged(
@@ -231,7 +231,7 @@ def test_runtime_not_cached(self, annotated_logger_mock, mocker):
231231
"runtime": "second",
232232
},
233233
)
234-
annotated_logger.runtime_annotations = runtime_annotations
234+
ann_logger.runtime_annotations = runtime_annotations
235235

236236
def test_raises_type_error_with_too_few_args(self):
237237
calc = Calculator(12, 13)
@@ -364,7 +364,7 @@ class Weird:
364364
def __len__(self):
365365
return 999
366366

367-
@annotated_logger.annotate_logs(_typing_self=False)
367+
@ann_logger.annotate_logs(_typing_self=False)
368368
def test_me():
369369
return Weird()
370370

0 commit comments

Comments
 (0)