Skip to content

Commit

Permalink
test: config_factory fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Apr 9, 2024
1 parent 50abc8c commit e3ae21a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from hyprshade.shader import hyprctl
from hyprshade.shader.core import Shader
from tests.types import HyprshadeDirectoryName, ShaderPathFactory
from tests.types import ConfigFactory, HyprshadeDirectoryName, ShaderPathFactory


@pytest.fixture(scope="session", autouse=True)
Expand Down Expand Up @@ -142,6 +142,11 @@ def _shader_path(
return _shader_path


@pytest.fixture()
def config_factory(isolation: Isolation) -> ConfigFactory:
return ConfigFactory(isolation)


def pytest_runtest_setup(item: pytest.Item) -> None:
for marker in item.iter_markers():
if marker.name == "requires_hyprland" and not has_hyprland():
Expand Down
48 changes: 46 additions & 2 deletions tests/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from pathlib import Path
from typing import Literal
from __future__ import annotations

from typing import TYPE_CHECKING, Literal

from typing_extensions import Protocol

if TYPE_CHECKING:
from pathlib import Path

from hyprshade.config.core import Config
from tests.conftest import Isolation


HyprshadeDirectoryName = Literal["env", "user_hypr", "user_hyprshade", "system"]
ConfigPathName = Literal["user_hypr", "user_hyprshade"]


class ShaderPathFactory(Protocol):
Expand All @@ -14,3 +23,38 @@ def __call__(
*,
extension: str = "glsl",
) -> Path: ...


class ConfigFactory:
_isolation: Isolation
_path: Path

def __init__(self, isolation: Isolation):
self._isolation = isolation
self.set_path("user_hypr")

@property
def path(self) -> Path:
return self._path

def set_path(self, config_path_name: ConfigPathName) -> None:
match config_path_name:
case "user_hypr":
self._path = self._isolation.hyprshade_user_hypr_dir / "hyprshade.toml"
case "user_hyrpshade":
self._path = (
self._isolation.hyprshade_user_hyprshade_dir / "config.toml"
)
case _:
raise ValueError(f"Unknown config path name: {config_path_name}")

def write(self, data: dict) -> None:
import tomlkit

with open(self._path, "w") as f:
tomlkit.dump(data, f)

def get_config(self) -> Config:
from hyprshade.config.core import Config

return Config(str(self._path))

0 comments on commit e3ae21a

Please sign in to comment.