Skip to content

Commit

Permalink
馃И more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Dec 18, 2023
1 parent 2c22a8b commit 0f2bdac
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 158 deletions.
2 changes: 1 addition & 1 deletion docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Environment Setup

> TIP: **pipx**
>
>
> This documentaion uses [pipx] to
> install and manage non-project command line tools like `hatch` and
> `pre-commit`. If you don't already have `pipx` installed, make sure to
Expand Down
7 changes: 6 additions & 1 deletion hatch_pip_compile/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
Hatch Plugin Registration
"""

from typing import Type

from hatchling.plugin import hookimpl

from hatch_pip_compile.plugin import PipCompileEnvironment


@hookimpl
def hatch_register_environment():
def hatch_register_environment() -> Type[PipCompileEnvironment]:
"""
Register the PipCompileEnvironment plugin with Hatch
"""
return PipCompileEnvironment
3 changes: 2 additions & 1 deletion hatch_pip_compile/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_option_types() -> Dict[str, Any]:
"""
Get option types
"""
return {
return { # pragma: no cover
"lock-filename": str,
"pip-compile-hashes": bool,
"pip-compile-args": List[str],
Expand Down Expand Up @@ -122,6 +122,7 @@ def pip_compile_cli(self) -> None:
"""
if not self.dependencies:
self.piptools_lock_file.unlink(missing_ok=True)
self.lockfile_up_to_date = True
return
no_compile = bool(os.getenv("PIP_COMPILE_DISABLE"))
if no_compile:
Expand Down
10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ show_missing = true
[tool.coverage.run]
branch = true
omit = [
"hatch_pip_compile/__about__.py"
"hatch_pip_compile/__about__.py",
"hatch_pip_compile/hooks.py"
]
parallel = true
source_pkgs = ["hatch_pip_compile", "tests"]
Expand Down Expand Up @@ -120,7 +121,8 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
[tool.hatch.envs.test]
dependencies = [
"pytest",
"pytest-cov"
"pytest-cov",
"tomlkit"
]

[tool.hatch.envs.test.scripts]
Expand Down Expand Up @@ -196,8 +198,8 @@ ban-relative-imports = "all"
known-first-party = ["hatch_pip_compile"]

[tool.ruff.per-file-ignores]
# Tests can use magic values, assertions, and relative imports
"tests/**/*" = ["PLR2004", "S101", "TID252"]
# Tests can use magic values, assertions, relative imports, and unused arguments
"tests/**/*" = ["PLR2004", "S101", "TID252", "ARG001"]

[tool.ruff.pydocstyle]
convention = "numpy"
2 changes: 2 additions & 0 deletions requirements/requirements-matrix.py3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand Down Expand Up @@ -165,6 +166,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.matrix.py3.10
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-matrix.py3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand Down Expand Up @@ -156,6 +157,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.matrix.py3.11
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-matrix.py3.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand Down Expand Up @@ -152,6 +153,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.matrix.py3.12
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-matrix.py3.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand Down Expand Up @@ -167,6 +168,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.matrix.py3.8
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
6 changes: 5 additions & 1 deletion requirements/requirements-matrix.py3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand All @@ -23,7 +24,9 @@ click==8.1.7
# hatch
# userpath
coverage==7.3.2
# via pytest-cov
# via
# coverage
# pytest-cov
distlib==0.3.7
# via
# -c requirements.txt
Expand Down Expand Up @@ -163,6 +166,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.matrix.py3.9
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
2 changes: 2 additions & 0 deletions requirements/requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#
# - pytest
# - pytest-cov
# - tomlkit
# - hatch
#

Expand Down Expand Up @@ -177,6 +178,7 @@ tomli-w==1.0.0
tomlkit==0.12.3
# via
# -c requirements.txt
# hatch.envs.test
# hatch
trove-classifiers==2023.11.29
# via
Expand Down
164 changes: 96 additions & 68 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@
import os
import pathlib
import shutil
from typing import Generator
from dataclasses import dataclass, field
from subprocess import CompletedProcess
from typing import Dict, Generator, Type
from unittest.mock import patch

import pytest
import tomlkit
from hatch.cli.application import Application
from hatch.config.constants import AppEnvVars, ConfigEnvVars, PublishEnvVars
from hatch.project.core import Project
from hatch.utils.fs import Path, temp_directory
from hatch.utils.platform import Platform
from platformdirs import user_cache_dir, user_data_dir

from hatch_pip_compile.installer import PipInstaller, PipSyncInstaller, PluginInstaller
from hatch_pip_compile.plugin import PipCompileEnvironment


@pytest.fixture(scope="session")
@pytest.fixture
def mock_check_command() -> Generator[patch, None, None]:
"""
Disable the `plugin_check_command` for testing
"""
with patch("hatch_pip_compile.plugin.PipCompileEnvironment.plugin_check_command") as mock:
mock.return_value = CompletedProcess(args=[], returncode=0, stdout=b"", stderr=b"")
yield mock


@pytest.fixture
def platform() -> Platform:
"""
Platform information.
Platform
"""
return Platform()


@pytest.fixture()
@pytest.fixture
def isolation(platform: Platform) -> Generator[Path, None, None]:
"""
Isolated hatch environment for testing.
Expand All @@ -49,7 +64,7 @@ def isolation(platform: Platform) -> Generator[Path, None, None]:
"COLUMNS": "80",
"LINES": "24",
}
if platform.windows:
if platform.windows: # pragma: no cover
default_env_vars["COMSPEC"] = "cmd.exe"
else:
default_env_vars["SHELL"] = "sh"
Expand All @@ -60,75 +75,88 @@ def isolation(platform: Platform) -> Generator[Path, None, None]:
yield temp_dir


@pytest.fixture
def isolated_data_dir() -> Path:
"""
Isolated data directory for testing.
"""
return Path(os.environ[ConfigEnvVars.DATA])


@pytest.fixture
def default_data_dir() -> Path:
"""
Path to Data Directory
"""
return Path(user_data_dir("hatch", appauthor=False))
@dataclass
class PipCompileFixture:
"""
Testing Fixture Data Container
"""

__test__ = False

isolation: pathlib.Path
toml_doc: tomlkit.TOMLDocument
pyproject: pathlib.Path
project: Project
platform: Platform
isolated_data_dir: pathlib.Path

application: Application = field(init=False)
default_environment: PipCompileEnvironment = field(init=False)
test_environment: PipCompileEnvironment = field(init=False)

def __post_init__(self) -> None:
"""
Post Init
"""
self.application = Application(
exit_func=lambda x: None, # noqa: ARG005
verbosity=0,
interactive=False,
enable_color=False,
)
self.default_environment = self.reload_environment("default")
self.test_environment = self.reload_environment("test")

def reload_environment(self, environment: str) -> PipCompileEnvironment:
"""
Reload a new environment given the current state of the isolated project
"""
new_project = Project(self.isolation)
return PipCompileEnvironment(
root=self.isolation,
metadata=new_project.metadata,
name=environment,
config=new_project.config.envs[environment],
matrix_variables={},
data_directory=self.isolated_data_dir,
isolated_data_directory=self.isolated_data_dir,
platform=self.platform,
verbosity=0,
)

def update_pyproject(self) -> None:
"""
Update pyproject.toml
"""
tomlkit.dump(self.toml_doc, self.pyproject.open("w"))


@pytest.fixture
def default_cache_dir() -> Path:
"""
Path to Cache Directory
"""
return Path(user_cache_dir("hatch", appauthor=False))


@pytest.fixture
def project(isolation: Path) -> Project:
"""
Standard project configuration
"""
return Project(isolation)


@pytest.fixture
def default_environment(
project: Project, isolated_data_dir: Path, platform: Platform, isolation: Path
) -> PipCompileEnvironment:
"""
Isolated PipCompileEnvironment - `default`
"""
environment = PipCompileEnvironment(
root=isolation,
metadata=project.metadata,
name="default",
config=project.config.envs["default"],
matrix_variables={},
data_directory=isolated_data_dir,
isolated_data_directory=isolated_data_dir,
def pip_compile(
isolation: Path,
platform: Platform,
) -> PipCompileFixture:
"""
PipCompile testing fixture
"""
pyproject = isolation / "pyproject.toml"
isolated_data_dir = Path(os.environ[ConfigEnvVars.DATA])
return PipCompileFixture(
isolation=isolation,
toml_doc=tomlkit.parse(string=pyproject.read_text()),
pyproject=pyproject,
project=Project(path=isolation),
platform=platform,
verbosity=0,
isolated_data_dir=isolated_data_dir,
)
return environment


@pytest.fixture
def test_environment(
project: Project, isolated_data_dir: Path, platform: Platform, isolation: Path
) -> PipCompileEnvironment:
def installer_dict() -> Dict[str, Type[PluginInstaller]]:
"""
Isolated PipCompileEnvironment - `test`
Installer dictionary for parametrized tests
"""
environment = PipCompileEnvironment(
root=isolation,
metadata=project.metadata,
name="test",
config=project.config.envs["test"],
matrix_variables={},
data_directory=isolated_data_dir,
isolated_data_directory=isolated_data_dir,
platform=platform,
verbosity=0,
)
return environment
return {
"pip": PipInstaller,
"pip-sync": PipSyncInstaller,
}
Empty file.
Loading

0 comments on commit 0f2bdac

Please sign in to comment.