Skip to content

Commit

Permalink
fix: Extract 'matches if specified' logic
Browse files Browse the repository at this point in the history
  • Loading branch information
koterpillar committed Aug 12, 2024
1 parent 61e213b commit d83b8ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
11 changes: 4 additions & 7 deletions mybox/package/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..parallel import gather_
from ..state import DB
from ..tracker import Tracker
from ..utils import allow_singular, async_cached
from ..utils import allow_singular, async_cached, matches_if_specified


class Package(BaseModel, ABC):
Expand Down Expand Up @@ -63,12 +63,9 @@ async def install(self, *, tracker: Tracker) -> None:
pass

async def applicable(self) -> bool:
def check_os(name: str) -> bool:
return self.os is None or name in self.os

os = await self.driver.os()
return os.switch_(
linux=lambda linux: check_os("linux")
and (self.distribution is None or linux.distribution in self.distribution),
macos=lambda: check_os("darwin"),
linux=lambda linux: matches_if_specified(self.os, "linux")
and matches_if_specified(self.distribution, linux.distribution),
macos=lambda: matches_if_specified(self.os, "darwin"),
)
4 changes: 4 additions & 0 deletions mybox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def allow_singular_none(field: str) -> Any:
return field_validator(field, mode="before")(lambda cls, x: unsome(x))


def matches_if_specified(possibilities: Optional[list[T]], value: T) -> bool:
return possibilities is None or value in possibilities


def intercalate(delimiter: T, items: Iterable[Iterable[T]]) -> list[T]:
it = iter(items)
try:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

from mybox.utils import flatten, intercalate, run_ok, with_extensions
from mybox.utils import (
flatten,
intercalate,
matches_if_specified,
run_ok,
with_extensions,
)


class TestRunOK:
Expand Down Expand Up @@ -39,3 +45,10 @@ def test_with_extensions():
extensions = ["com", "exe"]
assert with_extensions("foo", extensions) == ["foo.com", "foo.exe"]
assert with_extensions("foo.com", extensions) == ["foo.com"]


def test_matches_if_specified():
assert matches_if_specified(None, "anything")
assert not matches_if_specified([], "anything")
assert matches_if_specified(["alpha", "beta"], "alpha")
assert not matches_if_specified(["alpha", "beta"], "gamma")

0 comments on commit d83b8ad

Please sign in to comment.