Files
wehub-resource-sync bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:44 +08:00

210 lines
6.4 KiB
Python

import pytest
from mirage.commands.builtin.generic.tree import tree
from mirage.types import FileStat, FileType, PathSpec
def _spec(path: str) -> PathSpec:
return PathSpec(virtual=path,
directory=path,
resource_path=path.strip("/"))
def _file(name: str, size: int = 0) -> FileStat:
return FileStat(name=name, size=size, type=FileType.TEXT)
def _dir(name: str) -> FileStat:
return FileStat(name=name, size=None, type=FileType.DIRECTORY)
def _make_backend(tree_map: dict[str, FileStat]):
async def stat(p: PathSpec, index=None) -> FileStat:
if p.virtual not in tree_map:
raise FileNotFoundError(p.virtual)
return tree_map[p.virtual]
async def readdir(p: PathSpec, _index=None) -> list[str]:
if p.virtual not in tree_map:
raise FileNotFoundError(p.virtual)
if tree_map[p.virtual].type != FileType.DIRECTORY:
raise ValueError(f"not a directory: {p.virtual}")
prefix = p.virtual.rstrip("/") + "/"
children: list[str] = []
for key in tree_map:
if key == p.virtual:
continue
if key.startswith(prefix):
remainder = key[len(prefix):]
if "/" not in remainder:
children.append(key)
return sorted(children)
return readdir, stat
@pytest.mark.asyncio
async def test_tree_flat_dir():
"""Two siblings: the last gets `└──`, the others get `├──`."""
tree_map = {
"/r": _dir("r"),
"/r/a.txt": _file("a.txt"),
"/r/b.txt": _file("b.txt"),
}
readdir, stat = _make_backend(tree_map)
output, io = await tree(_spec("/r"), readdir=readdir, stat=stat)
lines = output.decode().splitlines()
assert lines == ["├── a.txt", "└── b.txt"]
assert io.exit_code == 0
@pytest.mark.asyncio
async def test_tree_nested_dir_uses_vertical_continuation():
"""A non-last directory should continue its children with `│ `."""
tree_map = {
"/r": _dir("r"),
"/r/d1": _dir("d1"),
"/r/d1/x.txt": _file("x.txt"),
"/r/z.txt": _file("z.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"), readdir=readdir, stat=stat)
lines = output.decode().splitlines()
assert lines == ["├── d1", "│ └── x.txt", "└── z.txt"]
@pytest.mark.asyncio
async def test_tree_last_dir_uses_indent_continuation():
"""A last directory should continue with plain spaces, no vertical bar."""
tree_map = {
"/r": _dir("r"),
"/r/d1": _dir("d1"),
"/r/d1/x.txt": _file("x.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"), readdir=readdir, stat=stat)
lines = output.decode().splitlines()
assert lines == ["└── d1", " └── x.txt"]
@pytest.mark.asyncio
async def test_tree_max_depth_limits_recursion():
tree_map = {
"/r": _dir("r"),
"/r/d1": _dir("d1"),
"/r/d1/d2": _dir("d2"),
"/r/d1/d2/deep.txt": _file("deep.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"),
readdir=readdir,
stat=stat,
max_depth=1)
decoded = output.decode()
assert "d1" in decoded
assert "d2" in decoded
assert "deep.txt" not in decoded
@pytest.mark.asyncio
async def test_tree_hides_dotfiles_by_default():
tree_map = {
"/r": _dir("r"),
"/r/.hidden": _file(".hidden"),
"/r/visible.txt": _file("visible.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"), readdir=readdir, stat=stat)
decoded = output.decode()
assert ".hidden" not in decoded
assert "visible.txt" in decoded
@pytest.mark.asyncio
async def test_tree_show_hidden_includes_dotfiles():
tree_map = {
"/r": _dir("r"),
"/r/.hidden": _file(".hidden"),
"/r/visible.txt": _file("visible.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"),
readdir=readdir,
stat=stat,
show_hidden=True)
assert ".hidden" in output.decode()
@pytest.mark.asyncio
async def test_tree_ignore_pattern_drops_matches():
tree_map = {
"/r": _dir("r"),
"/r/a.pyc": _file("a.pyc"),
"/r/b.py": _file("b.py"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"),
readdir=readdir,
stat=stat,
ignore_pattern="*.pyc")
decoded = output.decode()
assert "a.pyc" not in decoded
assert "b.py" in decoded
@pytest.mark.asyncio
async def test_tree_dirs_only_drops_files():
tree_map = {
"/r": _dir("r"),
"/r/d1": _dir("d1"),
"/r/a.txt": _file("a.txt"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"),
readdir=readdir,
stat=stat,
dirs_only=True)
decoded = output.decode()
assert "d1" in decoded
assert "a.txt" not in decoded
@pytest.mark.asyncio
async def test_tree_match_pattern_only_applies_to_files():
"""`-P` filters file names but never excludes directories."""
tree_map = {
"/r": _dir("r"),
"/r/d1": _dir("d1"),
"/r/d1/match.py": _file("match.py"),
"/r/d1/skip.txt": _file("skip.txt"),
"/r/top.py": _file("top.py"),
}
readdir, stat = _make_backend(tree_map)
output, _ = await tree(_spec("/r"),
readdir=readdir,
stat=stat,
match_pattern="*.py")
decoded = output.decode()
assert "d1" in decoded
assert "match.py" in decoded
assert "skip.txt" not in decoded
assert "top.py" in decoded
@pytest.mark.asyncio
async def test_tree_missing_path_emits_warning_not_crash():
readdir, stat = _make_backend({})
output, io = await tree(_spec("/nowhere"), readdir=readdir, stat=stat)
assert output == b""
assert b"nowhere" in (io.stderr or b"")
@pytest.mark.asyncio
async def test_tree_empty_dir_emits_nothing():
tree_map = {"/r": _dir("r")}
readdir, stat = _make_backend(tree_map)
output, io = await tree(_spec("/r"), readdir=readdir, stat=stat)
assert output == b""
assert io.exit_code == 0