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

Annotate warnings #68

Merged
merged 3 commits into from
Oct 22, 2024
Merged

Conversation

edgarrmondragon
Copy link
Collaborator

@edgarrmondragon edgarrmondragon commented Feb 8, 2023

  • Supported only in pytest >= 6.0.0 (docs)
  • Adds an --exclude-warning-annotations option to exclude warning annotations

Closes #45

@edgarrmondragon edgarrmondragon force-pushed the feat/annotate-warnings branch 4 times, most recently from 20f27ab to a70c269 Compare February 8, 2023 23:31
@edgarrmondragon edgarrmondragon marked this pull request as ready for review February 8, 2023 23:34
@edgarrmondragon edgarrmondragon marked this pull request as draft February 9, 2023 00:25
@edgarrmondragon edgarrmondragon force-pushed the feat/annotate-warnings branch 2 times, most recently from 87145ee to ae9b957 Compare February 9, 2023 01:03
@edgarrmondragon edgarrmondragon marked this pull request as ready for review February 9, 2023 01:06
@henryiii
Copy link
Contributor

Yeah, I think --exclude-warning-annotations or something else specific to this plugin only would be better.

@edgarrmondragon
Copy link
Collaborator Author

Yeah, I think --exclude-warning-annotations or something else specific to this plugin only would be better.

Done 494dca7!

@edgarrmondragon
Copy link
Collaborator Author

this is ready for another review @henryiii 🙂

@henryiii
Copy link
Contributor

FYI, we need to hide our warnings to get though CI. Mostly on pytest 6.

Comment on lines +115 to +116
with contextlib.suppress(ValueError):
filesystempath = os.path.relpath(filesystempath)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henryiii this lets Windows tests pass now

@henryiii
Copy link
Contributor

Rebase post #86?

- Refactor workflow command generation
- Skip warning annotations in Pytest < 6.0.0
@edgarrmondragon
Copy link
Collaborator Author

Rebased.

In a separate PR, I'd like to add test coverage or other means of detecting dead code after we dropped support for Pytest < 6.

@henryiii
Copy link
Contributor

That sounds good, though IMO top priority should be fixing the line calculation for pytest 7.4+. :)

@henryiii henryiii merged commit 801b644 into pytest-dev:main Oct 22, 2024
14 checks passed
@henryiii
Copy link
Contributor

Thanks!

@edgarrmondragon edgarrmondragon deleted the feat/annotate-warnings branch October 22, 2024 15:49
@edgarrmondragon
Copy link
Collaborator Author

That sounds good, though IMO top priority should be fixing the line calculation for pytest 7.4+. :)

I'll take a stab.

@henryiii
Copy link
Contributor

When I attempted it last, I had this:

diff --git a/pytest_github_actions_annotate_failures/plugin.py b/pytest_github_actions_annotate_failures/plugin.py
index 8cf1e62..c635c0c 100644
--- a/pytest_github_actions_annotate_failures/plugin.py
+++ b/pytest_github_actions_annotate_failures/plugin.py
@@ -7,7 +7,7 @@ from collections import OrderedDict
 from typing import TYPE_CHECKING

 import pytest
-from _pytest._code.code import ExceptionRepr
+from _pytest._code.code import ExceptionRepr, ExceptionChainRepr

 if TYPE_CHECKING:
     from _pytest.nodes import Item
@@ -23,6 +23,28 @@ if TYPE_CHECKING:
 # https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py


+def compute_path(filesystempath):
+    runpath = os.environ.get("PYTEST_RUN_PATH")
+    if runpath:
+        filesystempath = os.path.join(runpath, filesystempath)
+
+    # try to convert to absolute path in GitHub Actions
+    workspace = os.environ.get("GITHUB_WORKSPACE")
+    if workspace:
+        full_path = os.path.abspath(filesystempath)
+        try:
+            rel_path = os.path.relpath(full_path, workspace)
+        except ValueError:
+            # os.path.relpath() will raise ValueError on Windows
+            # when full_path and workspace have different mount points.
+            # https://github.com/utgwkk/pytest-github-actions-annotate-failures/issues/20
+            rel_path = filesystempath
+        if not rel_path.startswith(".."):
+            filesystempath = rel_path
+
+    return filesystempath
+
+
 @pytest.hookimpl(tryfirst=True, hookwrapper=True)
 def pytest_runtest_makereport(item: Item, call):  # noqa: ARG001
     # execute all other hooks to obtain the report object
@@ -38,24 +60,6 @@ def pytest_runtest_makereport(item: Item, call):  # noqa: ARG001
         # collect information to be annotated
         filesystempath, lineno, _ = report.location

-        runpath = os.environ.get("PYTEST_RUN_PATH")
-        if runpath:
-            filesystempath = os.path.join(runpath, filesystempath)
-
-        # try to convert to absolute path in GitHub Actions
-        workspace = os.environ.get("GITHUB_WORKSPACE")
-        if workspace:
-            full_path = os.path.abspath(filesystempath)
-            try:
-                rel_path = os.path.relpath(full_path, workspace)
-            except ValueError:
-                # os.path.relpath() will raise ValueError on Windows
-                # when full_path and workspace have different mount points.
-                # https://github.com/utgwkk/pytest-github-actions-annotate-failures/issues/20
-                rel_path = filesystempath
-            if not rel_path.startswith(".."):
-                filesystempath = rel_path
-
         if lineno is not None:
             # 0-index to 1-index
             lineno += 1
@@ -65,22 +69,24 @@ def pytest_runtest_makereport(item: Item, call):  # noqa: ARG001

         # get the error message and line number from the actual error
         if isinstance(report.longrepr, ExceptionRepr):
+            tb_entries = report.longrepr.reprtraceback.reprentries
             if report.longrepr.reprcrash is not None:
                 longrepr += "\n\n" + report.longrepr.reprcrash.message
-            tb_entries = report.longrepr.reprtraceback.reprentries
-            if len(tb_entries) > 1 and tb_entries[0].reprfileloc is not None:
+            if tb_entries and tb_entries[0].reprfileloc is not None:
                 # Handle third-party exceptions
                 lineno = tb_entries[0].reprfileloc.lineno
+                filesystempath = tb_entries[0].reprfileloc.path
             elif report.longrepr.reprcrash is not None:
                 lineno = report.longrepr.reprcrash.lineno
+                filesystempath = report.longrepr.reprcrash.path
         elif isinstance(report.longrepr, tuple):
-            _, lineno, message = report.longrepr
+            filesystempath, lineno, message = report.longrepr
             longrepr += "\n\n" + message
         elif isinstance(report.longrepr, str):
             longrepr += "\n\n" + report.longrepr

         print(
-            _error_workflow_command(filesystempath, lineno, longrepr), file=sys.stderr
+            _error_workflow_command(compute_path(filesystempath), lineno, longrepr), file=sys.stderr
         )

But I still had a couple of failures. Maybe that's helpful though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotate warnings as well?
2 participants