Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode Logging #506

Open
wants to merge 61 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
d42d176
Added Logging for Vscode
mj023 Nov 29, 2023
ef10aab
Fixed mistake in json
mj023 Nov 29, 2023
ed38f14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 29, 2023
a513fa5
Moved execution logging to live.py
mj023 Dec 6, 2023
b613a75
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Dec 6, 2023
5f7b80a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2023
7846dee
added requests as dependency
mj023 Dec 6, 2023
d051db6
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Dec 6, 2023
ceed57f
Add Func description
mj023 Dec 6, 2023
28a43e9
add requests to mypy
mj023 Dec 6, 2023
bb59416
Check for no exec info
mj023 Dec 6, 2023
bb1af38
suppress more exceptions
mj023 Dec 6, 2023
4ef51b0
Add new vscode module
mj023 Dec 13, 2023
945e933
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 13, 2023
24623fd
Suppress exceptions
mj023 Jan 17, 2024
8d5ca61
Remove old code
mj023 Jan 17, 2024
7db35cf
Remove Changes
mj023 Jan 17, 2024
1415d38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
d0087a6
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Jan 17, 2024
4ad1d36
Add vscode module
mj023 Jan 17, 2024
ec09e60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
1722f1c
Fix URL error
mj023 Jan 17, 2024
3c188b5
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 17, 2024
224331f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
c856264
Check URL
mj023 Jan 17, 2024
c7de595
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 17, 2024
7d7bf21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
e5d38f9
Disable qa
mj023 Jan 24, 2024
e6d1156
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
b9af139
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
b0aba34
Add noqa
mj023 Jan 24, 2024
208249b
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
5a28ed6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
2956580
change noqa
mj023 Jan 24, 2024
3166c8d
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
573d39a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
c1418c3
Merge branch 'main' into vscode-integration
mj023 Jan 24, 2024
f8945a7
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Feb 7, 2024
2a856b2
Add tests for vscode module
mj023 Feb 7, 2024
ba12e7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2024
a6c2dc9
Merge branch 'main' into vscode-integration
mj023 Feb 13, 2024
4d335d4
Send Logging to different urls
mj023 Mar 6, 2024
f2de23f
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Mar 6, 2024
a3db9ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 6, 2024
4c68723
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Apr 17, 2024
2e28de7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
1cd6281
Merge branch 'main' into vscode-integration
mj023 Jun 20, 2024
cde73fd
Make Port changeable
mj023 Jun 20, 2024
f4b1808
Fix typing mistake
mj023 Jul 3, 2024
57596b9
Merge branch 'main' into vscode-integration
mj023 Jul 3, 2024
c17208d
Merge branch 'main' into vscode-integration
mj023 Jul 23, 2024
698519b
Add Test for Wrong EnvVar
mj023 Aug 1, 2024
9ef1305
Add Comments for new functions
mj023 Aug 1, 2024
e3ef562
Update vscode.py
timmens Aug 2, 2024
9afa75b
Add Unit Test
mj023 Aug 6, 2024
686cdbe
Unfinished changes from discussion
timmens Aug 7, 2024
bef8004
Refactor Port Validation; Docstrings
mj023 Aug 9, 2024
a79c101
Add Mocking to Tests
mj023 Aug 14, 2024
450ab7e
Fix mypy error
timmens Aug 14, 2024
da70cca
Merge pull request #1 from mj023/updates-tim
mj023 Aug 16, 2024
e5f1f99
Merge branch 'main' into vscode-integration
mj023 Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/_pytask/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def pytask_add_hooks(pm: PluginManager) -> None:
"_pytask.skipping",
"_pytask.task",
"_pytask.warnings",
"_pytask.vscode",
)
register_hook_impls_from_modules(pm, builtin_hook_impl_modules)

Expand Down
124 changes: 124 additions & 0 deletions src/_pytask/vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"""Contains Code for VSCode Logging."""

from __future__ import annotations

import contextlib
import json
import os
from threading import Thread
from typing import TYPE_CHECKING
from typing import Any
from urllib.error import URLError
from urllib.request import urlopen

from _pytask.config import hookimpl
from _pytask.console import console
from _pytask.console import render_to_string
from _pytask.nodes import PTaskWithPath
from _pytask.outcomes import CollectionOutcome
from _pytask.outcomes import TaskOutcome
from _pytask.traceback import Traceback

if TYPE_CHECKING:
from _pytask.node_protocols import PTask
from _pytask.reports import CollectionReport
from _pytask.reports import ExecutionReport
from _pytask.session import Session


TIMEOUT = 0.00001
DEFAULT_VSCODE_PORT = 6000


def send_logging_info(url: str, data: dict[str, Any], timeout: float) -> None:
"""Send logging information to the provided port.

A response from the server is not needed, therefore a very low timeout is used to
essentially "fire-and-forget" the HTTP request. Because the HTTP protocol expects a
response, the urllib will throw an URLError or (rarely) a TimeoutError, which will
be suppressed.

"""
response = json.dumps(data).encode()
with contextlib.suppress(URLError, TimeoutError):
urlopen(url=url, data=response, timeout=timeout) # noqa: S310


def validate_and_return_port(port: str) -> int:
"""Validate the port number and return it as an integer.

The value of the environment variable is used as a direct input for the url, that
the logging info is sent to. To avoid security concerns the value is checked to
contain a valid port number and not an arbitrary string that could modify the url.

If the port cannot be converted to an integer, a ValueError is raised.

"""
try:
out = int(port)
except ValueError as e:
msg = (
"The value provided in the environment variable PYTASK_VSCODE must be an "
f"integer, got {port} instead."
)
raise ValueError(msg) from e
return out


@hookimpl(tryfirst=True)
def pytask_collect_log(
session: Session, reports: list[CollectionReport], tasks: list[PTask]
) -> None:
"""Start threads to send logging information for collected tasks."""
if (
os.environ.get("PYTASK_VSCODE") is not None
and session.config["command"] == "collect"
):
port = validate_and_return_port(os.environ["PYTASK_VSCODE"])

Check warning on line 77 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L77

Added line #L77 was not covered by tests

exitcode = "OK"
for report in reports:
if report.outcome == CollectionOutcome.FAIL:
exitcode = "COLLECTION_FAILED"

Check warning on line 82 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L79-L82

Added lines #L79 - L82 were not covered by tests

result = []
for task in tasks:
path = str(task.path) if isinstance(task, PTaskWithPath) else ""
result.append({"name": task.name, "path": path})

Check warning on line 87 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L84-L87

Added lines #L84 - L87 were not covered by tests

thread = Thread(

Check warning on line 89 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L89

Added line #L89 was not covered by tests
target=send_logging_info,
kwargs={
"url": f"http://localhost:{port}/pytask/collect",
"data": {"exitcode": exitcode, "tasks": result},
"timeout": TIMEOUT,
},
)
thread.start()

Check warning on line 97 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L97

Added line #L97 was not covered by tests


@hookimpl(tryfirst=True)
def pytask_execute_task_log_end(
session: Session, # noqa: ARG001
report: ExecutionReport,
) -> None:
"""Start threads to send logging information for executed tasks."""
if os.environ.get("PYTASK_VSCODE") is not None:
port = validate_and_return_port(os.environ["PYTASK_VSCODE"])

Check warning on line 107 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L107

Added line #L107 was not covered by tests

result = {

Check warning on line 109 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L109

Added line #L109 was not covered by tests
"name": report.task.name,
"outcome": str(report.outcome),
}
if report.outcome == TaskOutcome.FAIL and report.exc_info is not None:
result["exc_info"] = render_to_string(Traceback(report.exc_info), console)

Check warning on line 114 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L113-L114

Added lines #L113 - L114 were not covered by tests

thread = Thread(

Check warning on line 116 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L116

Added line #L116 was not covered by tests
target=send_logging_info,
kwargs={
"url": f"http://localhost:{port}/pytask/run",
"data": result,
"timeout": TIMEOUT,
},
)
thread.start()

Check warning on line 124 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L124

Added line #L124 was not covered by tests
92 changes: 92 additions & 0 deletions tests/test_vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from __future__ import annotations

import json
import os
import textwrap
from unittest.mock import MagicMock

import pytest
from _pytask.vscode import send_logging_info
from _pytask.vscode import validate_and_return_port
from pytask import ExitCode
from pytask import cli


@pytest.mark.unit()
def test_validate_and_return_port_valid_port():
assert validate_and_return_port("6000") == 6000


@pytest.mark.unit()
def test_validate_and_return_port_invalid_port():
with pytest.raises(
ValueError,
match="The value provided in the environment variable "
"PYTASK_VSCODE must be an integer, got not_an_integer instead.",
):
validate_and_return_port("not_an_integer")


@pytest.mark.unit()
def test_send_logging_info(monkeypatch):
mock_urlopen = MagicMock()
monkeypatch.setattr("_pytask.vscode.urlopen", mock_urlopen)

url = "http://localhost:6000/pytask/run"
data = {"test": "test"}
timeout = 0.00001
response = json.dumps(data).encode()

send_logging_info(url, data, timeout)
mock_urlopen.assert_called_with(url=url, data=response, timeout=timeout)


@pytest.mark.end_to_end()
def test_vscode_collect_failed(runner, tmp_path, monkeypatch):
mock_urlopen = MagicMock()
monkeypatch.setattr("_pytask.vscode.urlopen", mock_urlopen)
source = """

Check warning on line 48 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L46-L48

Added lines #L46 - L48 were not covered by tests
raise Exception
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

Check warning on line 52 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L51-L52

Added lines #L51 - L52 were not covered by tests

result = runner.invoke(cli, ["collect", tmp_path.as_posix()])
assert result.exit_code == ExitCode.COLLECTION_FAILED
mock_urlopen.assert_called_with(

Check warning on line 56 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L54-L56

Added lines #L54 - L56 were not covered by tests
url="http://localhost:6000/pytask/collect",
data=b'{"exitcode": "COLLECTION_FAILED", "tasks": []}',
timeout=0.00001,
)


@pytest.mark.end_to_end()
def test_vscode_collect(runner, tmp_path):
source = """

Check warning on line 65 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L65

Added line #L65 was not covered by tests
def task_example():
return
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

Check warning on line 70 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L69-L70

Added lines #L69 - L70 were not covered by tests

result = runner.invoke(cli, ["collect", tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK

Check warning on line 73 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L72-L73

Added lines #L72 - L73 were not covered by tests


@pytest.mark.end_to_end()
def test_vscode_build(runner, tmp_path, monkeypatch):
mock_urlopen = MagicMock()
monkeypatch.setattr("_pytask.vscode.urlopen", mock_urlopen)
source = """

Check warning on line 80 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L78-L80

Added lines #L78 - L80 were not covered by tests
def task_example():
return
def task_raises():
raise Exception
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

Check warning on line 87 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L86-L87

Added lines #L86 - L87 were not covered by tests

result = runner.invoke(cli, [tmp_path.as_posix()])

Check warning on line 89 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L89

Added line #L89 was not covered by tests

assert result.exit_code == ExitCode.FAILED
assert mock_urlopen.call_count == 2

Check warning on line 92 in tests/test_vscode.py

View check run for this annotation

Codecov / codecov/patch

tests/test_vscode.py#L91-L92

Added lines #L91 - L92 were not covered by tests
Loading