TreeWalker.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
from datetime import UTC, datetime
|
||||
from io import BytesIO
|
||||
from os import stat_result
|
||||
from pathlib import Path
|
||||
from random import randbytes
|
||||
from typing import Iterator
|
||||
@@ -25,6 +25,7 @@ from tempfile import TemporaryDirectory
|
||||
|
||||
from bsv.repository import Repository, Snapshot, Tree, TreeItem, create_repository, timestamp_from_time
|
||||
from bsv.simple_cas.cas import Digest
|
||||
from bsv.tree_walker import Action, IgnoreCause, TreeWalker
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -61,18 +62,20 @@ def test_read_write_tree(repo: Repository):
|
||||
repo,
|
||||
[
|
||||
TreeItem(
|
||||
"xyz",
|
||||
Digest(bytes([42]) * repo._cas._digest_size),
|
||||
0o744,
|
||||
creation_timestamp = timestamp_from_time(now),
|
||||
digest = Digest(bytes([42]) * repo._cas._digest_size),
|
||||
object_type = b"blob",
|
||||
size = 123,
|
||||
permissions = 0o744,
|
||||
modification_timestamp = timestamp_from_time(now),
|
||||
name = "xyz",
|
||||
),
|
||||
TreeItem(
|
||||
"foobar",
|
||||
Digest(bytes([123]) * repo._cas._digest_size),
|
||||
0o777,
|
||||
creation_timestamp = timestamp_from_time(now),
|
||||
digest = Digest(bytes([123]) * repo._cas._digest_size),
|
||||
object_type = b"slnk",
|
||||
size = 42,
|
||||
permissions = 0o777,
|
||||
modification_timestamp = timestamp_from_time(now),
|
||||
name = "foobar",
|
||||
),
|
||||
]
|
||||
)
|
||||
@@ -97,6 +100,75 @@ def test_read_write_snapshot(repo: Repository):
|
||||
assert repo.get_snapshot(digest) == snapshot
|
||||
|
||||
|
||||
class TestTreeWalker(TreeWalker):
|
||||
reports: list
|
||||
|
||||
def __init__(self, repo: Repository, dry_run: bool=False):
|
||||
super().__init__(repo, dry_run)
|
||||
self.reports = []
|
||||
|
||||
def report(
|
||||
self,
|
||||
action: Action,
|
||||
path: Path,
|
||||
pstat: stat_result | None,
|
||||
info: IgnoreCause | Exception | None = None
|
||||
):
|
||||
super().report(action, path, pstat, info)
|
||||
self.reports.append((action, path, pstat, info))
|
||||
|
||||
|
||||
def test_add_tree(tmp_dir: Path, repo: Repository):
|
||||
dir = tmp_dir / "test"
|
||||
structure = {
|
||||
"folder": {
|
||||
"sub_folder": {
|
||||
"empty_folder": {},
|
||||
"foo.txt": b"Hello World!\n",
|
||||
},
|
||||
"test.py": b"print(\"Hello World!\")\n",
|
||||
"bar.dat": bytes(range(256)),
|
||||
},
|
||||
"Another test with long name and spaces and a bang !": b"Should works.\n",
|
||||
"bsv_repo": {
|
||||
"bsv_config.toml": b"[bsv]\n",
|
||||
},
|
||||
}
|
||||
|
||||
create_file_structure(dir, structure)
|
||||
|
||||
walker = TestTreeWalker(repo)
|
||||
dir_digest = walker.add_tree(dir)
|
||||
|
||||
def check(digest: Digest, value: dict | bytes):
|
||||
if isinstance(value, dict):
|
||||
tree = repo.get_tree(digest)
|
||||
assert tree
|
||||
assert list(map(lambda i: i.name, tree.items)) == sorted(value.keys())
|
||||
for item in tree.items:
|
||||
check(item.digest, value[item.name])
|
||||
elif isinstance(value, bytes):
|
||||
blob = repo.get_blob(digest)
|
||||
data = blob.reader().read()
|
||||
assert data == value
|
||||
|
||||
expected = dict(structure)
|
||||
del expected["bsv_repo"]
|
||||
check(dir_digest, expected)
|
||||
|
||||
|
||||
def create_file_structure(dst: Path, value: dict | bytes):
|
||||
assert not dst.exists()
|
||||
if isinstance(value, dict):
|
||||
dst.mkdir()
|
||||
for name, item in value.items():
|
||||
create_file_structure(dst / name, item)
|
||||
elif isinstance(value, bytes):
|
||||
dst.write_bytes(value)
|
||||
else:
|
||||
raise TypeError(f"invalid type {type(value).__name__} for parameter value")
|
||||
|
||||
|
||||
def make_random_file(path: Path, size: int):
|
||||
with path.open("wb") as stream:
|
||||
for chunk_size in iter_chunks(size):
|
||||
|
||||
Reference in New Issue
Block a user