Skip to content

Commit

Permalink
add: glob tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
metaist committed Sep 13, 2024
1 parent e518654 commit 4ab176d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/cosmofy/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ def expand_globs(start: Path, *patterns: str) -> Iterator[Tuple[Path, Set[str]]]
"""Yield paths of all glob patterns."""
seen: Set[Path] = set()
for pattern in patterns:
for path in sorted(start.glob(pattern)):
if pattern == ".":
paths = [start]
elif pattern == "..":
paths = [start.parent]
else:
paths = sorted(start.glob(pattern))

for path in paths:
if not path.is_dir():
if path not in seen:
seen.add(path)
Expand Down Expand Up @@ -211,6 +218,8 @@ def add_files(
data = path.read_bytes()
file_name = path.name
parent_module = modules.get(path.parent, tuple())
if not parent_module and file_name in PACKAGE_FILES:
parent_module = (path.parent.name,)
modules[path] = parent_module + (path.stem,)

if not main_module and file_name in MAIN_FILES:
Expand Down
54 changes: 49 additions & 5 deletions test/test_bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
# std
from pathlib import Path
import io
import os
import tempfile

# pkg
from cosmofy.bundler import RE_MAIN
from cosmofy import bundler
from cosmofy.bundler import _archive
from cosmofy.bundler import _pack_uint32
from cosmofy.bundler import compile_python
from cosmofy.zipfile2 import ZipFile2

EXAMPLES = Path(__file__).parent.parent / "examples"


def test_main_detector() -> None:
"""Detect __main__ blocks."""
code = b"""if __name__ == "__main__":\n\t..."""
assert RE_MAIN.search(code)
assert bundler.RE_MAIN.search(code)

code = b"""\nif __name__ == '__main__':\n\t..."""
assert RE_MAIN.search(code)
assert bundler.RE_MAIN.search(code)

code = b"""\nif '__main__' == __name__ :\n\t..."""
assert RE_MAIN.search(code)
assert bundler.RE_MAIN.search(code)

code = b"""\n# if __name__ == "__main__":\n\t..."""
assert not RE_MAIN.search(code)
assert not bundler.RE_MAIN.search(code)


def test_utilities() -> None:
Expand All @@ -35,3 +39,43 @@ def test_utilities() -> None:
src = Path(__file__).parent.parent / "src" / "cosmofy" / "__init__.py"
assert isinstance(compile_python(src), bytearray)
assert isinstance(compile_python(src, src.read_bytes()), bytearray)


def test_move() -> None:
"""Move and make executable."""
content = b"test content"
with tempfile.NamedTemporaryFile() as f:
f.write(content)
f.flush()

with tempfile.NamedTemporaryFile() as g:
src = Path(f.name)
dest = Path(g.name)
bundler.move_set_executable(src, dest)
assert dest.read_bytes() == content
assert os.access(dest, os.X_OK)


def test_globs() -> None:
"""Glob patterns."""

src = EXAMPLES / "pkg-nested"
assert list(bundler.expand_globs(src)) == [] # no patterns

items = bundler.expand_globs(src, ".")
assert next(items) == (src, {"__init__.py"})

items = bundler.expand_globs(src, "..")
assert next(items) == (src.parent, set()) # examples only has sub-folders

items = list(bundler.expand_globs(src, "*"))
assert items[0] == (src / "__init__.py", set())
assert len(items) > 1

# see same item multiple times
items = list(bundler.expand_globs(src, "*", "*"))
assert items

src = EXAMPLES / "empty"
src.mkdir(parents=True, exist_ok=True)
assert list(bundler.expand_globs(src, "*")) == []

0 comments on commit 4ab176d

Please sign in to comment.