Skip to content

Commit dc5ccac

Browse files
committed
test: more tests
1 parent c2dace9 commit dc5ccac

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pytailwindcss_extra/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
def main() -> int:
2626
bin_dir_path = Path(user_data_dir("pytailwindcss-extra")) / "bin"
2727

28-
version = get_version(environ.get("PYTAILWINDCSS_EXTRA_VERSION", "major"))
28+
cache_file_path = Path(gettempdir()) / "tailwindcss-extra-cache.json"
29+
version = get_version(environ.get("PYTAILWINDCSS_EXTRA_VERSION", "major"), cache_file_path)
2930

3031
bin_path = bin_dir_path / f"tailwindcss-extra-{version.replace('.', '-')}"
3132
if not bin_path.exists():
@@ -38,11 +39,10 @@ def main() -> int:
3839
return result.returncode
3940

4041

41-
def get_version(specifier: str) -> str:
42+
def get_version(specifier: str, cache_file_path: Path) -> str:
4243
if specifier not in ["latest", "major"]:
4344
return specifier
4445

45-
cache_file_path = Path(gettempdir()) / "tailwindcss-extra-cache.json"
4646
cached_version = get_cached_version(specifier, cache_file_path)
4747
if cached_version:
4848
return cached_version
@@ -66,6 +66,8 @@ def get_cached_version(key: str, cache_file_path: Path) -> str | None:
6666
cache = loads(file.read())
6767

6868
cache_for_key = cache.get(key)
69+
if not cache_for_key:
70+
return None
6971

7072
if datetime.fromtimestamp(int(cache_for_key["time"])) + timedelta(hours=CACHE_EXPIRATION_HOURS) < datetime.now():
7173
return None

tests/test_main.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
from pytailwindcss_extra.main import get_latest_major_version_tag
1+
from pathlib import Path
2+
from typing import Generator, Callable
3+
from os import remove
4+
5+
import pytest
6+
7+
from pytailwindcss_extra.main import get_latest_major_version_tag, get_download_url, get_version
28

39

410
def test_get_latest_major_version_tag() -> None:
511
assert get_latest_major_version_tag(1) == "v1.7.27"
12+
13+
14+
def test_get_download_url() -> None:
15+
assert get_download_url("v1.7.27")
16+
17+
18+
@pytest.fixture(scope="module")
19+
def cleanup_cache() -> Generator:
20+
yield
21+
remove(Path("cache.json").absolute())
22+
23+
24+
@pytest.mark.parametrize("specifier", ["latest", "major", "v1.7.27"])
25+
def test_get_version(cleanup_cache: Callable, specifier: str) -> None:
26+
cache_file_path = Path("cache.json").absolute()
27+
28+
assert get_version(specifier, cache_file_path).startswith("v")

0 commit comments

Comments
 (0)