Skip to content

Commit 5a12851

Browse files
authored
Make tests more robust (no pre-existing repo on staging Hub) (#1302)
* Create gated repo on the fly * create repo with awful filename on the fly * Create foreign repo on the fly to test PR creation * scan cache tests on production repo * make it work on windows * final adapt * make snapshot download tests faster * Fix test_list_liked_repos_no_auth
1 parent 6d360e3 commit 5a12851

File tree

5 files changed

+126
-64
lines changed

5 files changed

+126
-64
lines changed

tests/test_file_download.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import pytest
2222

23+
import requests
24+
from huggingface_hub import HfApi
2325
from huggingface_hub.constants import (
2426
CONFIG_NAME,
2527
PYTORCH_WEIGHTS_NAME,
@@ -45,6 +47,7 @@
4547
)
4648
from tests.testing_constants import TOKEN
4749

50+
from .testing_constants import ENDPOINT_STAGING, OTHER_TOKEN
4851
from .testing_utils import (
4952
DUMMY_MODEL_ID,
5053
DUMMY_MODEL_ID_PINNED_SHA1,
@@ -56,6 +59,7 @@
5659
SAMPLE_DATASET_IDENTIFIER,
5760
OfflineSimulationMode,
5861
offline,
62+
repo_name,
5963
with_production_testing,
6064
xfail_on_windows,
6165
)
@@ -436,15 +440,28 @@ def test_download_from_a_gated_repo_with_hf_hub_download(self):
436440
Regression test for #1121.
437441
https://github.com/huggingface/huggingface_hub/pull/1121
438442
"""
443+
# Create a gated repo on the fly. Repo is created by "other user" so that the
444+
# usual CI user don't have access to it.
445+
api = HfApi(token=OTHER_TOKEN)
446+
repo_url = api.create_repo(
447+
repo_id="gated_repo_for_huggingface_hub_ci", exist_ok=True
448+
)
449+
requests.put(
450+
f"{repo_url.endpoint}/api/models/{repo_url.repo_id}/settings",
451+
headers=api._build_hf_headers(),
452+
json={"gated": True},
453+
).raise_for_status()
454+
455+
# Cannot download file as repo is gated
439456
with SoftTemporaryDirectory() as tmpdir:
440457
with self.assertRaisesRegex(
441458
GatedRepoError,
442459
"Access to model .* is restricted and you are not in the authorized"
443460
" list",
444461
):
445462
hf_hub_download(
446-
repo_id="datasets_server_org/gated_repo_for_huggingface_hub_ci",
447-
filename="config.json",
463+
repo_id=repo_url.repo_id,
464+
filename=".gitattributes",
448465
use_auth_token=TOKEN,
449466
cache_dir=tmpdir,
450467
)
@@ -460,33 +477,48 @@ class StagingCachedDownloadOnAwfulFilenamesTest(unittest.TestCase):
460477
"""
461478

462479
cache_dir: Path
463-
repo_id = "valid_org/repo_with_awful_filename"
464480
subfolder = "subfolder/to?"
465481
filename = "awful?filename%you:should,never.give"
466-
filepath = "subfolder/to?/awful?filename%you:should,never.give"
467-
expected_url = "https://hub-ci.huggingface.co/valid_org/repo_with_awful_filename/resolve/main/subfolder/to%3F/awful%3Ffilename%25you%3Ashould%2Cnever.give"
482+
filepath = f"subfolder/to?/{filename}"
483+
484+
@classmethod
485+
def setUpClass(cls):
486+
cls.api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
487+
cls.repo_url = cls.api.create_repo(repo_id=repo_name("awful_filename"))
488+
cls.expected_resolve_url = f"{cls.repo_url}/resolve/main/subfolder/to%3F/awful%3Ffilename%25you%3Ashould%2Cnever.give"
489+
cls.api.upload_file(
490+
path_or_fileobj=b"content",
491+
path_in_repo=cls.filepath,
492+
repo_id=cls.repo_url.repo_id,
493+
)
494+
495+
@classmethod
496+
def tearDownClass(cls) -> None:
497+
cls.api.delete_repo(repo_id=cls.repo_url.repo_id)
468498

469499
def test_hf_hub_url_on_awful_filepath(self):
470-
self.assertEqual(hf_hub_url(self.repo_id, self.filepath), self.expected_url)
500+
self.assertEqual(
501+
hf_hub_url(self.repo_url.repo_id, self.filepath), self.expected_resolve_url
502+
)
471503

472504
def test_hf_hub_url_on_awful_subfolder_and_filename(self):
473505
self.assertEqual(
474-
hf_hub_url(self.repo_id, self.filename, subfolder=self.subfolder),
475-
self.expected_url,
506+
hf_hub_url(self.repo_url.repo_id, self.filename, subfolder=self.subfolder),
507+
self.expected_resolve_url,
476508
)
477509

478510
@xfail_on_windows(reason="Windows paths cannot contain a '?'.")
479511
def test_hf_hub_download_on_awful_filepath(self):
480512
local_path = hf_hub_download(
481-
self.repo_id, self.filepath, cache_dir=self.cache_dir
513+
self.repo_url.repo_id, self.filepath, cache_dir=self.cache_dir
482514
)
483515
# Local path is not url-encoded
484516
self.assertTrue(local_path.endswith(self.filepath))
485517

486518
@xfail_on_windows(reason="Windows paths cannot contain a '?'.")
487519
def test_hf_hub_download_on_awful_subfolder_and_filename(self):
488520
local_path = hf_hub_download(
489-
self.repo_id,
521+
self.repo_url.repo_id,
490522
self.filename,
491523
subfolder=self.subfolder,
492524
cache_dir=self.cache_dir,

tests/test_hf_api.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
ENDPOINT_STAGING,
8383
ENDPOINT_STAGING_BASIC_AUTH,
8484
FULL_NAME,
85+
OTHER_TOKEN,
8586
TOKEN,
8687
USER,
8788
)
@@ -833,8 +834,11 @@ def test_create_commit_create_pr_against_branch(self):
833834

834835
@retry_endpoint
835836
def test_create_commit_create_pr_on_foreign_repo(self):
836-
# Repo on which we don't have right
837+
# Create a repo with another user. The normal CI user don't have rights on it.
837838
# We must be able to create a PR on it
839+
foreign_api = HfApi(token=OTHER_TOKEN)
840+
foreign_repo_url = foreign_api.create_repo(repo_id=repo_name("repo-for-hfh-ci"))
841+
838842
self._api.create_commit(
839843
operations=[
840844
CommitOperationAdd(
@@ -845,10 +849,12 @@ def test_create_commit_create_pr_on_foreign_repo(self):
845849
),
846850
],
847851
commit_message="PR on foreign repo",
848-
repo_id="datasets_server_org/repo_for_huggingface_hub_ci_with_prs",
852+
repo_id=foreign_repo_url.repo_id,
849853
create_pr=True,
850854
)
851855

856+
foreign_api.delete_repo(repo_id=foreign_repo_url.repo_id)
857+
852858
@retry_endpoint
853859
def test_create_commit(self):
854860
for private in (False, True):
@@ -2399,12 +2405,18 @@ def test_like_twice(self) -> None:
23992405
self.api.delete_repo(repo_id, token=TOKEN)
24002406

24012407
def test_list_liked_repos_no_auth(self) -> None:
2408+
# Create a list 1 liked repo
2409+
liked_repo_name = "repo-that-is-liked-public"
2410+
repo_url = self.api.create_repo(liked_repo_name, exist_ok=True, token=TOKEN)
2411+
self.api.like(repo_url.repo_id, token=TOKEN)
2412+
2413+
# Fetch liked repos without auth
24022414
likes = self.api.list_liked_repos(USER)
24032415
self.assertEqual(likes.user, USER)
24042416
self.assertGreater(
24052417
len(likes.models) + len(likes.datasets) + len(likes.spaces), 0
24062418
)
2407-
self.assertIn(f"{USER}/repo-that-is-liked-public", likes.models)
2419+
self.assertIn(repo_url.repo_id, likes.models)
24082420

24092421
def test_list_likes_repos_auth_and_implicit_user(self) -> None:
24102422
# User is implicit

tests/test_snapshot_download.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SnapshotDownloadTests(unittest.TestCase):
2828
_api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
2929

3030
@classmethod
31+
@retry_endpoint
3132
@expect_deprecation("set_access_token")
3233
def setUpClass(cls):
3334
"""
@@ -36,22 +37,20 @@ def setUpClass(cls):
3637
cls._token = TOKEN
3738
cls._api.set_access_token(TOKEN)
3839

39-
@retry_endpoint
40-
def setUp(self) -> None:
4140
if os.path.exists(REPO_NAME):
4241
rmtree_with_retry(REPO_NAME)
4342
logger.info(f"Does {REPO_NAME} exist: {os.path.exists(REPO_NAME)}")
4443

4544
try:
46-
self._api.delete_repo(repo_id=REPO_NAME)
45+
cls._api.delete_repo(repo_id=REPO_NAME)
4746
except RepositoryNotFoundError:
4847
pass
49-
self._api.create_repo(f"{USER}/{REPO_NAME}")
48+
cls._api.create_repo(f"{USER}/{REPO_NAME}")
5049

5150
repo = Repository(
5251
REPO_NAME,
5352
clone_from=f"{USER}/{REPO_NAME}",
54-
use_auth_token=self._token,
53+
use_auth_token=cls._token,
5554
git_user="ci",
5655
git_email="[email protected]",
5756
)
@@ -60,24 +59,25 @@ def setUp(self) -> None:
6059
with open("dummy_file.txt", "w+") as f:
6160
f.write("v1")
6261

63-
self.first_commit_hash = repo.git_head_hash()
62+
cls.first_commit_hash = repo.git_head_hash()
6463

6564
with repo.commit("Add file to main branch"):
6665
with open("dummy_file.txt", "w+") as f:
6766
f.write("v2")
6867
with open("dummy_file_2.txt", "w+") as f:
6968
f.write("v3")
7069

71-
self.second_commit_hash = repo.git_head_hash()
70+
cls.second_commit_hash = repo.git_head_hash()
7271

7372
with repo.commit("Add file to other branch", branch="other"):
7473
with open("dummy_file_2.txt", "w+") as f:
7574
f.write("v4")
7675

77-
self.third_commit_hash = repo.git_head_hash()
76+
cls.third_commit_hash = repo.git_head_hash()
7877

79-
def tearDown(self) -> None:
80-
self._api.delete_repo(repo_id=REPO_NAME)
78+
@classmethod
79+
def tearDownClass(cls) -> None:
80+
cls._api.delete_repo(repo_id=REPO_NAME)
8181
rmtree_with_retry(REPO_NAME)
8282

8383
def test_download_model(self):

0 commit comments

Comments
 (0)