diff --git a/src/cosmofy/bundler.py b/src/cosmofy/bundler.py index 75b2acd..78382f1 100644 --- a/src/cosmofy/bundler.py +++ b/src/cosmofy/bundler.py @@ -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) @@ -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: diff --git a/test/test_bundler.py b/test/test_bundler.py index 502241c..23fc99e 100644 --- a/test/test_bundler.py +++ b/test/test_bundler.py @@ -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: @@ -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, "*")) == []