feat: RFC 135

This commit is contained in:
莘权 马 2023-11-22 17:08:00 +08:00
parent 27c731d11a
commit 2bf8ef8c6a
16 changed files with 416 additions and 82 deletions

View file

@ -0,0 +1,64 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/11/22
@Author : mashenquan
@File : test_dependency_file.py
@Desc: Unit tests for dependency_file.py
"""
from __future__ import annotations
from pathlib import Path
from typing import Optional, Set, Union
import pytest
from pydantic import BaseModel
from metagpt.utils.dependency_file import DependencyFile
@pytest.mark.asyncio
async def test_dependency_file():
class Input(BaseModel):
x: Union[Path, str]
deps: Optional[Set[Union[Path, str]]]
key: Optional[Union[Path, str]]
want: Set[str]
inputs = [
Input(x="a/b.txt", deps={"c/e.txt", Path(__file__).parent / "d.txt"}, want={"c/e.txt", "d.txt"}),
Input(
x=Path(__file__).parent / "x/b.txt",
deps={"s/e.txt", Path(__file__).parent / "d.txt"},
key="x/b.txt",
want={"s/e.txt", "d.txt"},
),
Input(x="f.txt", deps=None, want=set()),
Input(x="a/b.txt", deps=None, want=set()),
]
file = DependencyFile(workdir=Path(__file__).parent)
for i in inputs:
await file.update(filename=i.x, dependencies=i.deps)
assert await file.get(filename=i.key or i.x) == i.want
file2 = DependencyFile(workdir=Path(__file__).parent)
file2.delete_file()
assert not file.exists
await file2.update(filename="a/b.txt", dependencies={"c/e.txt", Path(__file__).parent / "d.txt"}, persist=False)
assert not file.exists
await file2.save()
assert file2.exists
file1 = DependencyFile(workdir=Path(__file__).parent)
assert file1.exists
assert await file1.get("a/b.txt") == set()
await file1.load()
assert await file1.get("a/b.txt") == {"c/e.txt", "d.txt"}
file1.delete_file()
assert not file.exists
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -34,11 +34,13 @@ async def test_file_repo():
assert file_repo.workdir.exists()
await file_repo.save("a.txt", "AAA")
await file_repo.save("b.txt", "BBB", ["a.txt"])
assert "AAA" == await file_repo.get("a.txt")
assert "BBB" == await file_repo.get("b.txt")
assert ["a.txt"] == file_repo.get_dependency("b.txt")
doc = await file_repo.get("a.txt")
assert "AAA" == doc.content
doc = await file_repo.get("b.txt")
assert "BBB" == doc.content
assert {"a.txt"} == await file_repo.get_dependency("b.txt")
assert {"a.txt": ChangeType.UNTRACTED, "b.txt": ChangeType.UNTRACTED} == file_repo.changed_files
assert ["a.txt"] == file_repo.get_changed_dependency("b.txt")
assert {"a.txt"} == await file_repo.get_changed_dependency("b.txt")
await file_repo.save("d/e.txt", "EEE")
assert ["d/e.txt"] == file_repo.get_change_dir_files("d")

View file

@ -77,5 +77,20 @@ async def test_git1():
assert not local_path.exists()
@pytest.mark.asyncio
async def test_dependency_file():
local_path = Path(__file__).parent / "git2"
repo, subdir = await mock_repo(local_path)
dependancy_file = await repo.get_dependency()
assert not dependancy_file.exists
await dependancy_file.update(filename="a/b.txt", dependencies={"c/d.txt", "e/f.txt"})
assert dependancy_file.exists
repo.delete_repository()
assert not dependancy_file.exists
if __name__ == "__main__":
pytest.main([__file__, "-s"])