1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
import os
import time
import unittest
from pathlib import Path
from unittest.mock import Mock
import pytest
from huggingface_hub._snapshot_download import snapshot_download
from huggingface_hub.utils import DeleteCacheStrategy, HFCacheInfo, _format_size, scan_cache_dir
from huggingface_hub.utils._cache_manager import CacheNotFound, _try_delete_path
from .testing_utils import rmtree_with_retry, skip_on_windows, with_production_testing
# On production server to avoid recreating them all the time
MODEL_ID = "hf-internal-testing/hfh_ci_scan_repo_a"
MODEL_PATH = "models--hf-internal-testing--hfh_ci_scan_repo_a"
DATASET_ID = "hf-internal-testing/hfh_ci_scan_dataset_b"
DATASET_PATH = "datasets--hf-internal-testing--hfh_ci_scan_dataset_b"
REPO_A_MAIN_HASH = "c0d57e03d9f128062eadb6665618982db612b2e3"
REPO_A_PR_1_HASH = "1a665a9d28a66b1d0f8edd9359fc824aacc63234"
REPO_A_OTHER_HASH = "f95875cd910793299a545417cc4b3c9055202883"
REPO_A_MAIN_README_BLOB_HASH = "fffc22b462ba2368b09b4d38527760051c9090a9"
REPO_B_MAIN_HASH = "f1cdcd4641b3ea2dfa8d4333dba1ea3b532735e1"
REF_1_NAME = "refs/pr/1"
@pytest.mark.usefixtures("fx_cache_dir")
class TestMissingCacheUtils(unittest.TestCase):
cache_dir: Path
def test_cache_dir_is_missing(self) -> None:
"""Directory to scan does not exist raises CacheNotFound."""
self.assertRaises(CacheNotFound, scan_cache_dir, self.cache_dir / "does_not_exist")
def test_cache_dir_is_a_file(self) -> None:
"""Directory to scan is a file raises ValueError."""
file_path = self.cache_dir / "file.txt"
file_path.touch()
self.assertRaises(ValueError, scan_cache_dir, file_path)
@pytest.mark.usefixtures("fx_cache_dir")
class TestValidCacheUtils(unittest.TestCase):
cache_dir: Path
@with_production_testing
def setUp(self) -> None:
"""Set up a clean cache for tests that will remain valid in all tests."""
# Download latest main
snapshot_download(repo_id=MODEL_ID, repo_type="model", cache_dir=self.cache_dir)
# Download latest commit which is same as `main`
snapshot_download(repo_id=MODEL_ID, revision=REPO_A_MAIN_HASH, repo_type="model", cache_dir=self.cache_dir)
# Download the first commit
snapshot_download(repo_id=MODEL_ID, revision=REPO_A_OTHER_HASH, repo_type="model", cache_dir=self.cache_dir)
# Download from a PR
snapshot_download(repo_id=MODEL_ID, revision="refs/pr/1", repo_type="model", cache_dir=self.cache_dir)
# Download a Dataset repo from "main"
snapshot_download(repo_id=DATASET_ID, revision="main", repo_type="dataset", cache_dir=self.cache_dir)
@unittest.skipIf(os.name == "nt", "Windows cache is tested separately")
def test_scan_cache_on_valid_cache_unix(self) -> None:
"""Scan the cache dir without warnings (on unix-based platform).
This test is duplicated and adapted for Windows in `test_scan_cache_on_valid_cache_windows`.
Note: Please make sure to update both if any change is made.
"""
report = scan_cache_dir(self.cache_dir)
# Check general information about downloaded snapshots
self.assertEqual(report.size_on_disk, 3766)
self.assertEqual(len(report.repos), 2) # Model and dataset
self.assertEqual(len(report.warnings), 0) # Repos are valid
repo_a = [repo for repo in report.repos if repo.repo_id == MODEL_ID][0]
# Check repo A general information
repo_a_path = self.cache_dir / MODEL_PATH
self.assertEqual(repo_a.repo_id, MODEL_ID)
self.assertEqual(repo_a.repo_type, "model")
self.assertEqual(repo_a.repo_path, repo_a_path)
# 4 downloads but 3 revisions because "main" and REPO_A_MAIN_HASH are the same
self.assertEqual(len(repo_a.revisions), 3)
self.assertEqual(
{rev.commit_hash for rev in repo_a.revisions},
{REPO_A_MAIN_HASH, REPO_A_PR_1_HASH, REPO_A_OTHER_HASH},
)
# Repo size on disk is less than sum of revisions !
self.assertEqual(repo_a.size_on_disk, 1501)
self.assertEqual(sum(rev.size_on_disk for rev in repo_a.revisions), 4463)
# Repo nb files is less than sum of revisions !
self.assertEqual(repo_a.nb_files, 3)
self.assertEqual(sum(rev.nb_files for rev in repo_a.revisions), 6)
# 2 REFS in the repo: "main" and "refs/pr/1"
# We could have added a tag as well
self.assertEqual(set(repo_a.refs.keys()), {"main", REF_1_NAME})
self.assertEqual(repo_a.refs["main"].commit_hash, REPO_A_MAIN_HASH)
self.assertEqual(repo_a.refs[REF_1_NAME].commit_hash, REPO_A_PR_1_HASH)
# Check "main" revision information
main_revision = repo_a.refs["main"]
main_revision_path = repo_a_path / "snapshots" / REPO_A_MAIN_HASH
self.assertEqual(main_revision.commit_hash, REPO_A_MAIN_HASH)
self.assertEqual(main_revision.snapshot_path, main_revision_path)
self.assertEqual(main_revision.refs, {"main"})
# Same nb of files and size on disk that the sum
self.assertEqual(main_revision.nb_files, len(main_revision.files))
self.assertEqual(
main_revision.size_on_disk,
sum(file.size_on_disk for file in main_revision.files),
)
# Check readme file from "main" revision
main_readme_file = [file for file in main_revision.files if file.file_name == "README.md"][0]
main_readme_file_path = main_revision_path / "README.md"
main_readme_blob_path = repo_a_path / "blobs" / REPO_A_MAIN_README_BLOB_HASH
self.assertEqual(main_readme_file.file_name, "README.md")
self.assertEqual(main_readme_file.file_path, main_readme_file_path)
self.assertEqual(main_readme_file.blob_path, main_readme_blob_path)
# Check readme file from "refs/pr/1" revision
pr_1_revision = repo_a.refs[REF_1_NAME]
pr_1_revision_path = repo_a_path / "snapshots" / REPO_A_PR_1_HASH
pr_1_readme_file = [file for file in pr_1_revision.files if file.file_name == "README.md"][0]
pr_1_readme_file_path = pr_1_revision_path / "README.md"
# file_path in "refs/pr/1" revision is different from "main" but same blob path
self.assertEqual(pr_1_readme_file.file_path, pr_1_readme_file_path) # different
self.assertEqual(pr_1_readme_file.blob_path, main_readme_blob_path) # same
@unittest.skipIf(os.name != "nt", "Windows cache is tested separately")
def test_scan_cache_on_valid_cache_windows(self) -> None:
"""Scan the cache dir without warnings (on Windows).
Windows tests do not use symlinks which leads to duplication in the cache.
This test is duplicated from `test_scan_cache_on_valid_cache_unix` with a few
tweaks specific to windows.
Note: Please make sure to update both if any change is made.
"""
report = scan_cache_dir(self.cache_dir)
# Check general information about downloaded snapshots
self.assertEqual(report.size_on_disk, 6728)
self.assertEqual(len(report.repos), 2) # Model and dataset
self.assertEqual(len(report.warnings), 0) # Repos are valid
repo_a = [repo for repo in report.repos if repo.repo_id == MODEL_ID][0]
# Check repo A general information
repo_a_path = self.cache_dir / MODEL_PATH
self.assertEqual(repo_a.repo_id, MODEL_ID)
self.assertEqual(repo_a.repo_type, "model")
self.assertEqual(repo_a.repo_path, repo_a_path)
# 4 downloads but 3 revisions because "main" and REPO_A_MAIN_HASH are the same
self.assertEqual(len(repo_a.revisions), 3)
self.assertEqual(
{rev.commit_hash for rev in repo_a.revisions},
{REPO_A_MAIN_HASH, REPO_A_PR_1_HASH, REPO_A_OTHER_HASH},
)
# Repo size on disk is equal to the sum of revisions (no symlinks)
self.assertEqual(repo_a.size_on_disk, 4463) # Windows-specific
self.assertEqual(sum(rev.size_on_disk for rev in repo_a.revisions), 4463)
# Repo nb files is equal to the sum of revisions !
self.assertEqual(repo_a.nb_files, 6) # Windows-specific
self.assertEqual(sum(rev.nb_files for rev in repo_a.revisions), 6)
# 2 REFS in the repo: "main" and "refs/pr/1"
# We could have added a tag as well
REF_1_NAME = "refs\\pr\\1" # Windows-specific
self.assertEqual(set(repo_a.refs.keys()), {"main", REF_1_NAME})
self.assertEqual(repo_a.refs["main"].commit_hash, REPO_A_MAIN_HASH)
self.assertEqual(repo_a.refs[REF_1_NAME].commit_hash, REPO_A_PR_1_HASH)
# Check "main" revision information
main_revision = repo_a.refs["main"]
main_revision_path = repo_a_path / "snapshots" / REPO_A_MAIN_HASH
self.assertEqual(main_revision.commit_hash, REPO_A_MAIN_HASH)
self.assertEqual(main_revision.snapshot_path, main_revision_path)
self.assertEqual(main_revision.refs, {"main"})
# Same nb of files and size on disk that the sum
self.assertEqual(main_revision.nb_files, len(main_revision.files))
self.assertEqual(
main_revision.size_on_disk,
sum(file.size_on_disk for file in main_revision.files),
)
# Check readme file from "main" revision
main_readme_file = [file for file in main_revision.files if file.file_name == "README.md"][0]
main_readme_file_path = main_revision_path / "README.md"
main_readme_blob_path = repo_a_path / "blobs" / REPO_A_MAIN_README_BLOB_HASH
self.assertEqual(main_readme_file.file_name, "README.md")
self.assertEqual(main_readme_file.file_path, main_readme_file_path)
self.assertEqual(main_readme_file.blob_path, main_readme_file_path) # Windows-specific: no blob file
self.assertFalse(main_readme_blob_path.exists()) # Windows-specific
# Check readme file from "refs/pr/1" revision
pr_1_revision = repo_a.refs[REF_1_NAME]
pr_1_revision_path = repo_a_path / "snapshots" / REPO_A_PR_1_HASH
pr_1_readme_file = [file for file in pr_1_revision.files if file.file_name == "README.md"][0]
pr_1_readme_file_path = pr_1_revision_path / "README.md"
# file_path in "refs/pr/1" revision is different from "main"
# Windows-specific: even blob path is different
self.assertEqual(pr_1_readme_file.file_path, pr_1_readme_file_path)
self.assertNotEqual( # Windows-specific: different as well
pr_1_readme_file.blob_path, main_readme_file.blob_path
)
@pytest.mark.usefixtures("fx_cache_dir")
class TestCorruptedCacheUtils(unittest.TestCase):
cache_dir: Path
repo_path: Path
refs_path: Path
snapshots_path: Path
@with_production_testing
def setUp(self) -> None:
"""Set up a clean cache for tests that will get corrupted/modified in tests."""
# Download latest main
snapshot_download(repo_id=MODEL_ID, repo_type="model", cache_dir=self.cache_dir)
self.repo_path = self.cache_dir / MODEL_PATH
self.refs_path = self.repo_path / "refs"
self.snapshots_path = self.repo_path / "snapshots"
def test_repo_path_not_valid_dir(self) -> None:
"""Test if found a not valid path in cache dir."""
# Case 1: a file
repo_path = self.cache_dir / "a_file_that_should_not_be_there.txt"
repo_path.touch()
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 1) # Scan still worked !
self.assertEqual(len(report.warnings), 1)
self.assertEqual(str(report.warnings[0]), f"Repo path is not a directory: {repo_path}")
# Case 2: a folder with wrong naming
os.remove(repo_path)
repo_path = self.cache_dir / "a_folder_that_should_not_be_there"
repo_path.mkdir()
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 1) # Scan still worked !
self.assertEqual(len(report.warnings), 1)
self.assertEqual(
str(report.warnings[0]),
f"Repo path is not a valid HuggingFace cache directory: {repo_path}",
)
# Case 3: good naming but not a dataset/model/space
rmtree_with_retry(repo_path)
repo_path = self.cache_dir / "not-models--t5-small"
repo_path.mkdir()
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 1) # Scan still worked !
self.assertEqual(len(report.warnings), 1)
self.assertEqual(
str(report.warnings[0]),
f"Repo type must be `dataset`, `model` or `space`, found `not-model` ({repo_path}).",
)
def test_snapshots_path_not_found(self) -> None:
"""Test if snapshots directory is missing in cached repo."""
rmtree_with_retry(self.snapshots_path)
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 0) # Failed
self.assertEqual(len(report.warnings), 1)
self.assertEqual(
str(report.warnings[0]),
f"Snapshots dir doesn't exist in cached repo: {self.snapshots_path}",
)
def test_file_in_snapshots_dir(self) -> None:
"""Test if snapshots directory contains a file."""
wrong_file_path = self.snapshots_path / "should_not_be_there"
wrong_file_path.touch()
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 0) # Failed
self.assertEqual(len(report.warnings), 1)
self.assertEqual(
str(report.warnings[0]),
f"Snapshots folder corrupted. Found a file: {wrong_file_path}",
)
def test_snapshot_with_no_blob_files(self) -> None:
"""Test if a snapshot directory (e.g. a cached revision) is empty."""
for revision_path in self.snapshots_path.glob("*"):
# Delete content of the revision
rmtree_with_retry(revision_path)
revision_path.mkdir()
# Scan
report = scan_cache_dir(self.cache_dir)
# Get single repo
self.assertEqual(len(report.warnings), 0) # Did not fail
self.assertEqual(len(report.repos), 1)
repo_report = list(report.repos)[0]
# Repo report is empty
self.assertEqual(repo_report.size_on_disk, 0)
self.assertEqual(len(repo_report.revisions), 1)
revision_report = list(repo_report.revisions)[0]
# No files in revision so last_modified is the one from the revision folder
self.assertEqual(revision_report.nb_files, 0)
self.assertEqual(revision_report.last_modified, revision_path.stat().st_mtime)
def test_repo_with_no_snapshots(self) -> None:
"""Test if the snapshot directory exists but is empty."""
rmtree_with_retry(self.refs_path)
rmtree_with_retry(self.snapshots_path)
self.snapshots_path.mkdir()
# Scan
report = scan_cache_dir(self.cache_dir)
# Get single repo
self.assertEqual(len(report.warnings), 0) # Did not fail
self.assertEqual(len(report.repos), 1)
repo_report = list(report.repos)[0]
# No revisions in repos so last_modified is the one from the repo folder
self.assertEqual(repo_report.size_on_disk, 0)
self.assertEqual(len(repo_report.revisions), 0)
self.assertEqual(repo_report.last_modified, self.repo_path.stat().st_mtime)
self.assertEqual(repo_report.last_accessed, self.repo_path.stat().st_atime)
def test_ref_to_missing_revision(self) -> None:
"""Test if a `refs` points to a missing revision."""
new_ref = self.repo_path / "refs" / "not_main"
with new_ref.open("w") as f:
f.write("revision_hash_that_does_not_exist")
report = scan_cache_dir(self.cache_dir)
self.assertEqual(len(report.repos), 0) # Failed
self.assertEqual(len(report.warnings), 1)
self.assertEqual(
str(report.warnings[0]),
"Reference(s) refer to missing commit hashes: {'revision_hash_that_does_not_exist': {'not_main'}} "
+ f"({self.repo_path}).",
)
@skip_on_windows("Last modified/last accessed work a bit differently on Windows.")
def test_scan_cache_last_modified_and_last_accessed(self) -> None:
"""Scan the last_modified and last_accessed properties when scanning."""
TIME_GAP = 0.1
# Make a first scan
report_1 = scan_cache_dir(self.cache_dir)
# Values from first report
repo_1 = list(report_1.repos)[0]
revision_1 = list(repo_1.revisions)[0]
readme_file_1 = [file for file in revision_1.files if file.file_name == "README.md"][0]
another_file_1 = [file for file in revision_1.files if file.file_name == ".gitattributes"][0]
# Comparison of last_accessed/last_modified between file and repo
self.assertLessEqual(readme_file_1.blob_last_accessed, repo_1.last_accessed)
self.assertLessEqual(readme_file_1.blob_last_modified, repo_1.last_modified)
self.assertEqual(revision_1.last_modified, repo_1.last_modified)
# Sleep and write new readme
time.sleep(TIME_GAP)
readme_file_1.file_path.write_text("modified readme")
# Sleep and read content from readme
time.sleep(TIME_GAP)
with readme_file_1.file_path.open("r") as f:
_ = f.read()
# Sleep and re-scan
time.sleep(TIME_GAP)
report_2 = scan_cache_dir(self.cache_dir)
# Values from second report
repo_2 = list(report_2.repos)[0]
revision_2 = list(repo_2.revisions)[0]
readme_file_2 = [file for file in revision_2.files if file.file_name == "README.md"][0]
another_file_2 = [file for file in revision_1.files if file.file_name == ".gitattributes"][0]
# Report 1 is not updated when cache changes
self.assertLess(repo_1.last_accessed, repo_2.last_accessed)
self.assertLess(repo_1.last_modified, repo_2.last_modified)
# "Another_file.md" did not change
self.assertEqual(another_file_1, another_file_2)
# Readme.md has been modified and then accessed more recently
self.assertGreaterEqual(
readme_file_2.blob_last_modified - readme_file_1.blob_last_modified,
TIME_GAP * 0.9, # 0.9 factor because not exactly precise
)
self.assertGreaterEqual(
readme_file_2.blob_last_accessed - readme_file_1.blob_last_accessed,
2 * TIME_GAP * 0.9, # 0.9 factor because not exactly precise
)
self.assertGreaterEqual(
readme_file_2.blob_last_accessed - readme_file_2.blob_last_modified,
TIME_GAP * 0.9, # 0.9 factor because not exactly precise
)
# Comparison of last_accessed/last_modified between file and repo
self.assertEqual(readme_file_2.blob_last_accessed, repo_2.last_accessed)
self.assertEqual(readme_file_2.blob_last_modified, repo_2.last_modified)
self.assertEqual(revision_2.last_modified, repo_2.last_modified)
class TestDeleteRevisionsDryRun(unittest.TestCase):
cache_info: Mock # Mocked HFCacheInfo
def setUp(self) -> None:
"""Set up fake cache scan report."""
repo_A_path = Path("repo_A")
blobs_path = repo_A_path / "blobs"
snapshots_path = repo_A_path / "snapshots_path"
# Define blob files
main_only_file = Mock()
main_only_file.blob_path = blobs_path / "main_only_hash"
main_only_file.size_on_disk = 1
detached_only_file = Mock()
detached_only_file.blob_path = blobs_path / "detached_only_hash"
detached_only_file.size_on_disk = 10
pr_1_only_file = Mock()
pr_1_only_file.blob_path = blobs_path / "pr_1_only_hash"
pr_1_only_file.size_on_disk = 100
detached_and_pr_1_only_file = Mock()
detached_and_pr_1_only_file.blob_path = blobs_path / "detached_and_pr_1_only_hash"
detached_and_pr_1_only_file.size_on_disk = 1000
shared_file = Mock()
shared_file.blob_path = blobs_path / "shared_file_hash"
shared_file.size_on_disk = 10000
# Define revisions
repo_A_rev_main = Mock()
repo_A_rev_main.commit_hash = "repo_A_rev_main"
repo_A_rev_main.snapshot_path = snapshots_path / "repo_A_rev_main"
repo_A_rev_main.files = {main_only_file, shared_file}
repo_A_rev_main.refs = {"main"}
repo_A_rev_detached = Mock()
repo_A_rev_detached.commit_hash = "repo_A_rev_detached"
repo_A_rev_detached.snapshot_path = snapshots_path / "repo_A_rev_detached"
repo_A_rev_detached.files = {
detached_only_file,
detached_and_pr_1_only_file,
shared_file,
}
repo_A_rev_detached.refs = {}
repo_A_rev_pr_1 = Mock()
repo_A_rev_pr_1.commit_hash = "repo_A_rev_pr_1"
repo_A_rev_pr_1.snapshot_path = snapshots_path / "repo_A_rev_pr_1"
repo_A_rev_pr_1.files = {
pr_1_only_file,
detached_and_pr_1_only_file,
shared_file,
}
repo_A_rev_pr_1.refs = {"refs/pr/1"}
# Define repo
repo_A = Mock()
repo_A.repo_path = Path("repo_A")
repo_A.size_on_disk = 4444
repo_A.revisions = {repo_A_rev_main, repo_A_rev_detached, repo_A_rev_pr_1}
# Define cache
cache_info = Mock()
cache_info.repos = [repo_A]
self.cache_info = cache_info
def test_delete_detached_revision(self) -> None:
strategy = HFCacheInfo.delete_revisions(self.cache_info, "repo_A_rev_detached")
expected = DeleteCacheStrategy(
expected_freed_size=10,
blobs={
# "shared_file_hash" and "detached_and_pr_1_only_hash" are not deleted
Path("repo_A/blobs/detached_only_hash"),
},
refs=set(), # No ref deleted since detached
repos=set(), # No repo deleted as other revisions exist
snapshots={Path("repo_A/snapshots_path/repo_A_rev_detached")},
)
self.assertEqual(strategy, expected)
def test_delete_pr_1_revision(self) -> None:
strategy = HFCacheInfo.delete_revisions(self.cache_info, "repo_A_rev_pr_1")
expected = DeleteCacheStrategy(
expected_freed_size=100,
blobs={
# "shared_file_hash" and "detached_and_pr_1_only_hash" are not deleted
Path("repo_A/blobs/pr_1_only_hash")
},
refs={Path("repo_A/refs/refs/pr/1")}, # Ref is deleted !
repos=set(), # No repo deleted as other revisions exist
snapshots={Path("repo_A/snapshots_path/repo_A_rev_pr_1")},
)
self.assertEqual(strategy, expected)
def test_delete_pr_1_and_detached(self) -> None:
strategy = HFCacheInfo.delete_revisions(self.cache_info, "repo_A_rev_detached", "repo_A_rev_pr_1")
expected = DeleteCacheStrategy(
expected_freed_size=1110,
blobs={
Path("repo_A/blobs/detached_only_hash"),
Path("repo_A/blobs/pr_1_only_hash"),
# blob shared in both revisions and only those two
Path("repo_A/blobs/detached_and_pr_1_only_hash"),
},
refs={Path("repo_A/refs/refs/pr/1")},
repos=set(),
snapshots={
Path("repo_A/snapshots_path/repo_A_rev_detached"),
Path("repo_A/snapshots_path/repo_A_rev_pr_1"),
},
)
self.assertEqual(strategy, expected)
def test_delete_all_revisions(self) -> None:
strategy = HFCacheInfo.delete_revisions(
self.cache_info, "repo_A_rev_detached", "repo_A_rev_pr_1", "repo_A_rev_main"
)
expected = DeleteCacheStrategy(
expected_freed_size=4444,
blobs=set(),
refs=set(),
repos={Path("repo_A")}, # No remaining revisions: full repo is deleted
snapshots=set(),
)
self.assertEqual(strategy, expected)
def test_delete_unknown_revision(self) -> None:
with self.assertLogs() as captured:
strategy = HFCacheInfo.delete_revisions(self.cache_info, "repo_A_rev_detached", "abcdef123456789")
# Expected is same strategy as without "abcdef123456789"
expected = HFCacheInfo.delete_revisions(self.cache_info, "repo_A_rev_detached")
self.assertEqual(strategy, expected)
# Expect a warning message
self.assertEqual(len(captured.records), 1)
self.assertEqual(captured.records[0].levelname, "WARNING")
self.assertEqual(
captured.records[0].message,
"Revision(s) not found - cannot delete them: abcdef123456789",
)
@pytest.mark.usefixtures("fx_cache_dir")
class TestDeleteStrategyExecute(unittest.TestCase):
cache_dir: Path
def test_execute(self) -> None:
# Repo folders
repo_A_path = self.cache_dir / "repo_A"
repo_A_path.mkdir()
repo_B_path = self.cache_dir / "repo_B"
repo_B_path.mkdir()
# Refs files in repo_B
refs_main_path = repo_B_path / "refs" / "main"
refs_main_path.parent.mkdir(parents=True)
refs_main_path.touch()
refs_pr_1_path = repo_B_path / "refs" / "refs" / "pr" / "1"
refs_pr_1_path.parent.mkdir(parents=True)
refs_pr_1_path.touch()
# Blobs files in repo_B
(repo_B_path / "blobs").mkdir()
blob_1 = repo_B_path / "blobs" / "blob_1"
blob_2 = repo_B_path / "blobs" / "blob_2"
blob_3 = repo_B_path / "blobs" / "blob_3"
blob_1.touch()
blob_2.touch()
blob_3.touch()
# Snapshot folders in repo_B
snapshot_1 = repo_B_path / "snapshots" / "snapshot_1"
snapshot_2 = repo_B_path / "snapshots" / "snapshot_2"
snapshot_1.mkdir(parents=True)
snapshot_2.mkdir()
# Execute deletion
# Delete repo_A + keep only blob_1, main ref and snapshot_1 in repo_B.
DeleteCacheStrategy(
expected_freed_size=123456,
blobs={blob_2, blob_3},
refs={refs_pr_1_path},
repos={repo_A_path},
snapshots={snapshot_2},
).execute()
# Repo A deleted
self.assertFalse(repo_A_path.exists())
self.assertTrue(repo_B_path.exists())
# Only `blob` 1 remains
self.assertTrue(blob_1.exists())
self.assertFalse(blob_2.exists())
self.assertFalse(blob_3.exists())
# Only ref `main` remains
self.assertTrue(refs_main_path.exists())
self.assertFalse(refs_pr_1_path.exists())
# Only `snapshot_1` remains
self.assertTrue(snapshot_1.exists())
self.assertFalse(snapshot_2.exists())
@pytest.mark.usefixtures("fx_cache_dir")
class TestTryDeletePath(unittest.TestCase):
cache_dir: Path
def test_delete_path_on_file_success(self) -> None:
"""Successfully delete a local file."""
file_path = self.cache_dir / "file.txt"
file_path.touch()
_try_delete_path(file_path, path_type="TYPE")
self.assertFalse(file_path.exists())
def test_delete_path_on_folder_success(self) -> None:
"""Successfully delete a local folder."""
dir_path = self.cache_dir / "something"
subdir_path = dir_path / "bar"
subdir_path.mkdir(parents=True) # subfolder
file_path_1 = dir_path / "file.txt" # file at root
file_path_1.touch()
file_path_2 = subdir_path / "config.json" # file in subfolder
file_path_2.touch()
_try_delete_path(dir_path, path_type="TYPE")
self.assertFalse(dir_path.exists())
self.assertFalse(subdir_path.exists())
self.assertFalse(file_path_1.exists())
self.assertFalse(file_path_2.exists())
def test_delete_path_on_missing_file(self) -> None:
"""Try to delete a missing file."""
file_path = self.cache_dir / "file.txt"
with self.assertLogs() as captured:
_try_delete_path(file_path, path_type="TYPE")
# Assert warning message with traceback for debug purposes
assert len(captured.output) > 0
assert any(f"Couldn't delete TYPE: file not found ({file_path})" in log for log in captured.output)
def test_delete_path_on_missing_folder(self) -> None:
"""Try to delete a missing folder."""
dir_path = self.cache_dir / "folder"
with self.assertLogs() as captured:
_try_delete_path(dir_path, path_type="TYPE")
# Assert warning message with traceback for debug purposes
assert len(captured.output) > 0
assert any(f"Couldn't delete TYPE: file not found ({dir_path})" in log for log in captured.output)
@skip_on_windows(reason="Permissions are handled differently on Windows.")
def test_delete_path_on_local_folder_with_wrong_permission(self) -> None:
"""Try to delete a local folder that is protected."""
dir_path = self.cache_dir / "something"
dir_path.mkdir()
file_path_1 = dir_path / "file.txt" # file at root
file_path_1.touch()
dir_path.chmod(444) # Read-only folder
with self.assertLogs() as captured:
_try_delete_path(dir_path, path_type="TYPE")
# Folder still exists (couldn't be deleted)
self.assertTrue(dir_path.is_dir())
# Assert warning message with traceback for debug purposes
self.assertEqual(len(captured.output), 1)
self.assertTrue(
captured.output[0].startswith(
"WARNING:huggingface_hub.utils._cache_manager:Couldn't delete TYPE:"
f" permission denied ({dir_path})\nTraceback (most recent call last):"
)
)
# For proper cleanup
dir_path.chmod(509)
class TestStringFormatters(unittest.TestCase):
SIZES = {
16.0: "16.0",
1000.0: "1.0K",
1024 * 1024 * 1024: "1.1G", # not 1.0GiB
}
SINCE = {
1: "a few seconds ago",
15: "a few seconds ago",
25: "25 seconds ago",
80: "1 minute ago",
1000: "17 minutes ago",
4000: "1 hour ago",
8000: "2 hours ago",
3600 * 24 * 13: "2 weeks ago",
3600 * 24 * 30 * 8.2: "8 months ago",
3600 * 24 * 365: "1 year ago",
3600 * 24 * 365 * 9.6: "10 years ago",
}
def test_format_size(self) -> None:
"""Test `_format_size` formatter."""
for size, expected in self.SIZES.items():
self.assertEqual(
_format_size(size),
expected,
msg=f"Wrong formatting for {size} == '{expected}'",
)
|