Reject negative retention before modifying stored versions
This commit is contained in:
parent
421960726c
commit
15e4ed81a2
2 changed files with 15 additions and 0 deletions
|
|
@ -65,6 +65,8 @@ class LargeFileBase(ABC):
|
||||||
keep_last_n: Optional[int] = None,
|
keep_last_n: Optional[int] = None,
|
||||||
cache_only_mode: bool = False,
|
cache_only_mode: bool = False,
|
||||||
):
|
):
|
||||||
|
if keep_last_n is not None and keep_last_n < 0:
|
||||||
|
raise ValueError("keep_last_n must be non-negative")
|
||||||
clean_name = re.sub(r"[^!a-zA-Z0-9._-]+", "", name)
|
clean_name = re.sub(r"[^!a-zA-Z0-9._-]+", "", name)
|
||||||
if clean_name != name:
|
if clean_name != name:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,19 @@ def test_retention_counts_the_new_version() -> None:
|
||||||
assert reader.get().read_text() == "2"
|
assert reader.get().read_text() == "2"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"implementation", [LargeFileLocal, LargeFileS3, LargeFileMongo]
|
||||||
|
)
|
||||||
|
def test_negative_retention_is_rejected_before_storage_access(
|
||||||
|
implementation: Any,
|
||||||
|
) -> None:
|
||||||
|
with LargeFileLocal("preserved", "w") as file:
|
||||||
|
file.write("weights")
|
||||||
|
with pytest.raises(ValueError, match="keep_last_n must be non-negative"):
|
||||||
|
implementation("preserved", "w", keep_last_n=-1)
|
||||||
|
assert LargeFileLocal("preserved").get().read_text() == "weights"
|
||||||
|
|
||||||
|
|
||||||
def write_model(root: str, value: str, barrier: Any) -> None:
|
def write_model(root: str, value: str, barrier: Any) -> None:
|
||||||
LargeFileBase.cache_path = LargeFileLocal.cache_path = Path(root)
|
LargeFileBase.cache_path = LargeFileLocal.cache_path = Path(root)
|
||||||
LargeFileBase.max_cache_size = LargeFileLocal.max_cache_size = None
|
LargeFileBase.max_cache_size = LargeFileLocal.max_cache_size = None
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue