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

Update path argument #89

Merged
merged 11 commits into from Dec 21, 2021
36 changes: 29 additions & 7 deletions pytest_mypy_plugins/collect.py
@@ -1,9 +1,20 @@
import os
import pathlib
import platform
import sys
import tempfile
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional, Set

from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterator,
List,
Mapping,
Optional,
Set,
)

import pkg_resources
import pytest
import yaml
from _pytest.config.argparsing import Parser
Expand Down Expand Up @@ -76,7 +87,9 @@ class YamlTestFile(pytest.File):
def collect(self) -> Iterator["YamlTestItem"]:
from pytest_mypy_plugins.item import YamlTestItem

parsed_file = yaml.load(stream=self.fspath.read_text("utf8"), Loader=SafeLineLoader)
# To support both Pytest 6.x and 7.x
path = getattr(self, "path", None) or getattr(self, "fspath")
parsed_file = yaml.load(stream=path.read_text("utf8"), Loader=SafeLineLoader)
if parsed_file is None:
return

Expand Down Expand Up @@ -138,10 +151,19 @@ def _eval_skip(self, skip_if: str) -> bool:
return eval(skip_if, {"sys": sys, "os": os, "pytest": pytest, "platform": platform})


def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]:
if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")):
return YamlTestFile.from_parent(parent, fspath=path)
return None
if pkg_resources.parse_version(pytest.__version__) < pkg_resources.parse_version("7.0.0rc1"):

def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]:
if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")):
return YamlTestFile.from_parent(parent, fspath=path)
return None

else:

def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> Optional[YamlTestFile]:
if file_path.suffix in {".yaml", ".yml"} and file_path.name.startswith(("test-", "test_")):
return YamlTestFile.from_parent(parent, path=file_path)
return None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If only this part is implemented, pluggy fails with:

pluggy._manager.PluginValidationError: Plugin 'pytest-mypy-plugins' for hook 'pytest_collect_file'
hookimpl definition: pytest_collect_file(file_path: pathlib.Path, parent: _pytest.nodes.Node) -> Optional[pytest_mypy_plugins.collect.YamlTestFile]
Argument(s) {'file_path'} are declared in the hookimpl but can not be found in the hookspec



def pytest_addoption(parser: Parser) -> None:
Expand Down