-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_repository.py
895 lines (672 loc) · 32.4 KB
/
test_repository.py
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# Copyright 2020 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import time
import unittest
from pathlib import Path
import pytest
import requests
from huggingface_hub import RepoUrl
from huggingface_hub.hf_api import HfApi
from huggingface_hub.repository import (
Repository,
is_tracked_upstream,
is_tracked_with_lfs,
)
from huggingface_hub.utils import SoftTemporaryDirectory, logging, run_subprocess
from .testing_constants import ENDPOINT_STAGING, TOKEN
from .testing_utils import (
expect_deprecation,
repo_name,
use_tmp_repo,
with_production_testing,
)
logger = logging.get_logger(__name__)
@pytest.mark.usefixtures("fx_cache_dir")
class RepositoryTestAbstract(unittest.TestCase):
cache_dir: Path
repo_path: Path
# This content is 5MB (under 10MB)
small_content = json.dumps([100] * int(1e6))
# This content is 20MB (over 10MB)
large_content = json.dumps([100] * int(4e6))
# This content is binary (contains the null character)
binary_content = "\x00\x00\x00\x00"
_api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)
@classmethod
def setUp(self) -> None:
self.repo_path = self.cache_dir / "working_dir"
self.repo_path.mkdir()
def _create_dummy_files(self):
# Create dummy files
# one is lfs-tracked, the other is not.
small_file = self.repo_path / "dummy.txt"
small_file.write_text(self.small_content)
binary_file = self.repo_path / "model.bin"
binary_file.write_text(self.binary_content)
class TestRepositoryShared(RepositoryTestAbstract):
"""Tests in this class shares a single repo on the Hub (common to all tests).
These tests must not push data to it.
"""
@classmethod
def setUpClass(cls):
"""
Share this valid token in all tests below.
"""
super().setUpClass()
cls.repo_url = cls._api.create_repo(repo_id=repo_name())
cls.repo_id = cls.repo_url.repo_id
cls._api.upload_file(
path_or_fileobj=cls.binary_content.encode(),
path_in_repo="random_file.txt",
repo_id=cls.repo_id,
)
@classmethod
def tearDownClass(cls):
cls._api.delete_repo(repo_id=cls.repo_id)
@expect_deprecation("Repository")
def test_clone_from_repo_url(self):
Repository(self.repo_path, clone_from=self.repo_url)
@expect_deprecation("Repository")
def test_clone_from_repo_id(self):
Repository(self.repo_path, clone_from=self.repo_id)
@expect_deprecation("Repository")
def test_clone_from_repo_name_no_namespace_fails(self):
with self.assertRaises(EnvironmentError):
Repository(self.repo_path, clone_from=self.repo_id.split("/")[1], token=TOKEN)
@expect_deprecation("Repository")
def test_clone_from_not_hf_url(self):
# Should not error out
Repository(self.repo_path, clone_from="https://hf.co/hf-internal-testing/huggingface-hub-dummy-repository")
@expect_deprecation("Repository")
def test_clone_from_missing_repo(self):
"""If the repo does not exist an EnvironmentError is raised."""
with self.assertRaises(EnvironmentError):
Repository(self.repo_path, clone_from="missing_repo")
@expect_deprecation("Repository")
@with_production_testing
def test_clone_from_prod_canonical_repo_id(self):
Repository(self.repo_path, clone_from="bert-base-cased", skip_lfs_files=True)
@expect_deprecation("Repository")
@with_production_testing
def test_clone_from_prod_canonical_repo_url(self):
Repository(self.repo_path, clone_from="https://huggingface.co/bert-base-cased", skip_lfs_files=True)
@expect_deprecation("Repository")
def test_init_from_existing_local_clone(self):
run_subprocess(["git", "clone", self.repo_url, str(self.repo_path)])
repo = Repository(self.repo_path)
repo.lfs_track(["*.pdf"])
repo.lfs_enable_largefiles()
repo.git_pull()
@expect_deprecation("Repository")
def test_init_failure(self):
with self.assertRaises(ValueError):
Repository(self.repo_path)
@expect_deprecation("Repository")
def test_init_clone_in_empty_folder(self):
repo = Repository(self.repo_path, clone_from=self.repo_url)
repo.lfs_track(["*.pdf"])
repo.lfs_enable_largefiles()
repo.git_pull()
self.assertIn("random_file.txt", os.listdir(self.repo_path))
@expect_deprecation("Repository")
def test_git_lfs_filename(self):
run_subprocess("git init", folder=self.repo_path)
repo = Repository(self.repo_path)
large_file = self.repo_path / "large_file[].txt"
large_file.write_text(self.large_content)
repo.git_add()
repo.lfs_track([large_file.name])
self.assertFalse(is_tracked_with_lfs(large_file))
repo.lfs_track([large_file.name], filename=True)
self.assertTrue(is_tracked_with_lfs(large_file))
@expect_deprecation("Repository")
def test_init_clone_in_nonempty_folder(self):
self._create_dummy_files()
with self.assertRaises(EnvironmentError):
Repository(self.repo_path, clone_from=self.repo_url)
@expect_deprecation("Repository")
def test_init_clone_in_nonempty_linked_git_repo_with_token(self):
Repository(self.repo_path, clone_from=self.repo_url, token=TOKEN)
Repository(self.repo_path, clone_from=self.repo_url, token=TOKEN)
@expect_deprecation("Repository")
def test_is_tracked_upstream(self):
Repository(self.repo_path, clone_from=self.repo_id)
self.assertTrue(is_tracked_upstream(self.repo_path))
@expect_deprecation("Repository")
def test_push_errors_on_wrong_checkout(self):
repo = Repository(self.repo_path, clone_from=self.repo_id)
head_commit_ref = run_subprocess("git show --oneline -s", folder=self.repo_path).stdout.split()[0]
repo.git_checkout(head_commit_ref)
with self.assertRaises(OSError):
with repo.commit("New commit"):
with open("new_file", "w+") as f:
f.write("Ok")
class TestRepositoryUniqueRepos(RepositoryTestAbstract):
"""Tests in this class use separated repos on the Hub (i.e. 1 test = 1 repo).
These tests can push data to it.
"""
def setUp(self):
super().setUp()
self.repo_url = self._api.create_repo(repo_id=repo_name())
self.repo_id = self.repo_url.repo_id
self._api.upload_file(
path_or_fileobj=self.binary_content.encode(), path_in_repo="random_file.txt", repo_id=self.repo_id
)
def tearDown(self):
self._api.delete_repo(repo_id=self.repo_id)
@expect_deprecation("Repository")
def clone_repo(self, **kwargs) -> Repository:
if "local_dir" not in kwargs:
kwargs["local_dir"] = self.repo_path
if "clone_from" not in kwargs:
kwargs["clone_from"] = self.repo_url
if "token" not in kwargs:
kwargs["token"] = TOKEN
if "git_user" not in kwargs:
kwargs["git_user"] = "ci"
if "git_email" not in kwargs:
kwargs["git_email"] = "[email protected]"
return Repository(**kwargs)
@use_tmp_repo()
@expect_deprecation("Repository")
def test_init_clone_in_nonempty_non_linked_git_repo(self, repo_url: RepoUrl):
self.clone_repo()
# Try and clone another repository within the same directory.
# Should error out due to mismatched remotes.
with self.assertRaises(EnvironmentError):
Repository(self.repo_path, clone_from=repo_url)
def test_init_clone_in_nonempty_linked_git_repo(self):
# Clone the repository to disk
self.clone_repo()
# Add to the remote repository without doing anything to the local repository.
self._api.upload_file(
path_or_fileobj=self.binary_content.encode(), path_in_repo="random_file_3.txt", repo_id=self.repo_id
)
# Cloning the repository in the same directory should not result in a git pull.
self.clone_repo(clone_from=self.repo_url)
self.assertNotIn("random_file_3.txt", os.listdir(self.repo_path))
def test_init_clone_in_nonempty_linked_git_repo_unrelated_histories(self):
# Clone the repository to disk
repo = self.clone_repo()
# Create and commit file locally
(self.repo_path / "random_file_3.txt").write_text("hello world")
repo.git_add()
repo.git_commit("Unrelated commit")
# Add to the remote repository without doing anything to the local repository.
self._api.upload_file(
path_or_fileobj=self.binary_content.encode(),
path_in_repo="random_file_3.txt",
repo_id=self.repo_url.repo_id,
)
# The repo should initialize correctly as the remote is the same, even with unrelated historied
self.clone_repo()
def test_add_commit_push(self):
repo = self.clone_repo()
self._create_dummy_files()
repo.git_add()
repo.git_commit()
url = repo.git_push()
# Check that the returned commit url
# actually exists.
r = requests.head(url)
r.raise_for_status()
def test_add_commit_push_non_blocking(self):
repo = self.clone_repo()
self._create_dummy_files()
repo.git_add()
repo.git_commit()
url, result = repo.git_push(blocking=False)
# Check background process
if result._process.poll() is None:
self.assertEqual(result.status, -1)
while not result.is_done:
time.sleep(0.5)
self.assertTrue(result.is_done)
self.assertEqual(result.status, 0)
# Check that the returned commit url
# actually exists.
r = requests.head(url)
r.raise_for_status()
def test_context_manager_non_blocking(self):
repo = self.clone_repo()
with repo.commit("New commit", blocking=False):
(self.repo_path / "dummy.txt").write_text("hello world")
while repo.commands_in_progress:
time.sleep(1)
self.assertEqual(len(repo.commands_in_progress), 0)
self.assertEqual(len(repo.command_queue), 1)
self.assertEqual(repo.command_queue[-1].status, 0)
self.assertEqual(repo.command_queue[-1].is_done, True)
self.assertEqual(repo.command_queue[-1].title, "push")
@unittest.skip("This is a flaky and legacy test")
def test_add_commit_push_non_blocking_process_killed(self):
repo = self.clone_repo()
# Far too big file: will take forever
(self.repo_path / "dummy.txt").write_text(str([[[1] * 10000] * 1000] * 10))
repo.git_add(auto_lfs_track=True)
repo.git_commit()
_, result = repo.git_push(blocking=False)
result._process.kill()
while result._process.poll() is None:
time.sleep(0.5)
self.assertTrue(result.is_done)
self.assertEqual(result.status, -9)
def test_commit_context_manager(self):
# Clone and commit from a first folder
folder_1 = self.repo_path / "folder_1"
clone = self.clone_repo(local_dir=folder_1)
with clone.commit("Commit"):
with open("dummy.txt", "w") as f:
f.write("hello")
with open("model.bin", "w") as f:
f.write("hello")
# Clone in second folder. Check existence of committed files
folder_2 = self.repo_path / "folder_2"
self.clone_repo(local_dir=folder_2)
files = os.listdir(folder_2)
self.assertTrue("dummy.txt" in files)
self.assertTrue("model.bin" in files)
def test_clone_skip_lfs_files(self):
# Upload LFS file
self._api.upload_file(path_or_fileobj=b"Bin file", path_in_repo="file.bin", repo_id=self.repo_id)
repo = self.clone_repo(skip_lfs_files=True)
file_bin = self.repo_path / "file.bin"
self.assertTrue(file_bin.read_text().startswith("version"))
repo.git_pull(lfs=True)
self.assertEqual(file_bin.read_text(), "Bin file")
def test_commits_on_correct_branch(self):
repo = self.clone_repo()
branch = repo.current_branch
repo.git_checkout("new-branch", create_branch_ok=True)
repo.git_checkout(branch)
with repo.commit("New commit"):
with open("file.txt", "w+") as f:
f.write("Ok")
repo.git_checkout("new-branch")
with repo.commit("New commit"):
with open("new_file.txt", "w+") as f:
f.write("Ok")
with SoftTemporaryDirectory() as tmp:
clone = self.clone_repo(local_dir=tmp)
files = os.listdir(clone.local_dir)
self.assertTrue("file.txt" in files)
self.assertFalse("new_file.txt" in files)
clone.git_checkout("new-branch")
files = os.listdir(clone.local_dir)
self.assertFalse("file.txt" in files)
self.assertTrue("new_file.txt" in files)
def test_repo_checkout_push(self):
repo = self.clone_repo()
repo.git_checkout("new-branch", create_branch_ok=True)
repo.git_checkout("main")
(self.repo_path / "file.txt").write_text("OK")
repo.push_to_hub("Commit #1")
repo.git_checkout("new-branch", create_branch_ok=True)
(self.repo_path / "new_file.txt").write_text("OK")
repo.push_to_hub("Commit #2")
with SoftTemporaryDirectory() as tmp:
clone = self.clone_repo(local_dir=tmp)
files = os.listdir(clone.local_dir)
self.assertTrue("file.txt" in files)
self.assertFalse("new_file.txt" in files)
clone.git_checkout("new-branch")
files = os.listdir(clone.local_dir)
self.assertFalse("file.txt" in files)
self.assertTrue("new_file.txt" in files)
def test_repo_checkout_commit_context_manager(self):
repo = self.clone_repo()
with repo.commit("Commit #1", branch="new-branch"):
with open(os.path.join(repo.local_dir, "file.txt"), "w+") as f:
f.write("Ok")
with repo.commit("Commit #2", branch="main"):
with open(os.path.join(repo.local_dir, "new_file.txt"), "w+") as f:
f.write("Ok")
# Maintains lastly used branch
with repo.commit("Commit #3"):
with open(os.path.join(repo.local_dir, "new_file-2.txt"), "w+") as f:
f.write("Ok")
with SoftTemporaryDirectory() as tmp:
clone = self.clone_repo(local_dir=tmp)
files = os.listdir(clone.local_dir)
self.assertFalse("file.txt" in files)
self.assertTrue("new_file-2.txt" in files)
self.assertTrue("new_file.txt" in files)
clone.git_checkout("new-branch")
files = os.listdir(clone.local_dir)
self.assertTrue("file.txt" in files)
self.assertFalse("new_file.txt" in files)
self.assertFalse("new_file-2.txt" in files)
def test_add_tag(self):
repo = self.clone_repo()
repo.add_tag("v4.6.0", remote="origin")
self.assertTrue(repo.tag_exists("v4.6.0", remote="origin"))
def test_add_annotated_tag(self):
repo = self.clone_repo()
repo.add_tag("v4.5.0", message="This is an annotated tag", remote="origin")
# Unfortunately git offers no built-in way to check the annotated
# message of a remote tag.
# In order to check that the remote tag was correctly annotated,
# we delete the local tag before pulling the remote tag (which
# should be the same). We then check that this tag is correctly
# annotated.
repo.delete_tag("v4.5.0")
self.assertTrue(repo.tag_exists("v4.5.0", remote="origin"))
self.assertFalse(repo.tag_exists("v4.5.0"))
# Tag still exists on remote
run_subprocess("git pull --tags", folder=self.repo_path)
self.assertTrue(repo.tag_exists("v4.5.0"))
# Tag is annotated
result = run_subprocess("git tag -n9", folder=self.repo_path).stdout.strip()
self.assertIn("This is an annotated tag", result)
def test_delete_tag(self):
repo = self.clone_repo()
repo.add_tag("v4.6.0", message="This is an annotated tag", remote="origin")
self.assertTrue(repo.tag_exists("v4.6.0", remote="origin"))
repo.delete_tag("v4.6.0")
self.assertFalse(repo.tag_exists("v4.6.0"))
self.assertTrue(repo.tag_exists("v4.6.0", remote="origin"))
repo.delete_tag("v4.6.0", remote="origin")
self.assertFalse(repo.tag_exists("v4.6.0", remote="origin"))
def test_lfs_prune(self):
repo = self.clone_repo()
with repo.commit("Committing LFS file"):
with open("file.bin", "w+") as f:
f.write("Random string 1")
with repo.commit("Committing LFS file"):
with open("file.bin", "w+") as f:
f.write("Random string 2")
root_directory = self.repo_path / ".git" / "lfs"
git_lfs_files_size = sum(f.stat().st_size for f in root_directory.glob("**/*") if f.is_file())
repo.lfs_prune()
post_prune_git_lfs_files_size = sum(f.stat().st_size for f in root_directory.glob("**/*") if f.is_file())
# Size of the directory holding LFS files was reduced
self.assertLess(post_prune_git_lfs_files_size, git_lfs_files_size)
def test_lfs_prune_git_push(self):
repo = self.clone_repo()
with repo.commit("Committing LFS file"):
with open("file.bin", "w+") as f:
f.write("Random string 1")
root_directory = self.repo_path / ".git" / "lfs"
git_lfs_files_size = sum(f.stat().st_size for f in root_directory.glob("**/*") if f.is_file())
with open(os.path.join(repo.local_dir, "file.bin"), "w+") as f:
f.write("Random string 2")
repo.git_add()
repo.git_commit("New commit")
repo.git_push(auto_lfs_prune=True)
post_prune_git_lfs_files_size = sum(f.stat().st_size for f in root_directory.glob("**/*") if f.is_file())
# Size of the directory holding LFS files is the exact same
self.assertEqual(post_prune_git_lfs_files_size, git_lfs_files_size)
class TestRepositoryOffline(RepositoryTestAbstract):
"""Class to test `Repository` object on local folders only (no cloning from Hub)."""
repo: Repository
@classmethod
@expect_deprecation("Repository")
def setUp(self) -> None:
super().setUp()
run_subprocess("git init", folder=self.repo_path)
self.repo = Repository(self.repo_path, git_user="ci", git_email="[email protected]")
git_attributes_path = self.repo_path / ".gitattributes"
git_attributes_path.write_text("*.pt filter=lfs diff=lfs merge=lfs -text")
self.repo.git_add(".gitattributes")
self.repo.git_commit("Add .gitattributes")
def test_is_tracked_with_lfs(self):
txt_1 = self.repo_path / "small_file_1.txt"
txt_2 = self.repo_path / "small_file_2.txt"
pt_1 = self.repo_path / "model.pt"
txt_1.write_text(self.small_content)
txt_2.write_text(self.small_content)
pt_1.write_text(self.small_content)
self.repo.lfs_track("small_file_1.txt")
self.assertTrue(is_tracked_with_lfs(txt_1))
self.assertFalse(is_tracked_with_lfs(txt_2))
self.assertTrue(pt_1)
def test_is_tracked_with_lfs_with_pattern(self):
txt_small_file = self.repo_path / "small_file.txt"
txt_small_file.write_text(self.small_content)
txt_large_file = self.repo_path / "large_file.txt"
txt_large_file.write_text(self.large_content)
(self.repo_path / "dir").mkdir()
txt_small_file_in_dir = self.repo_path / "dir" / "small_file.txt"
txt_small_file_in_dir.write_text(self.small_content)
txt_large_file_in_dir = self.repo_path / "dir" / "large_file.txt"
txt_large_file_in_dir.write_text(self.large_content)
self.repo.auto_track_large_files("dir")
self.assertFalse(is_tracked_with_lfs(txt_large_file))
self.assertFalse(is_tracked_with_lfs(txt_small_file))
self.assertTrue(is_tracked_with_lfs(txt_large_file_in_dir))
self.assertFalse(is_tracked_with_lfs(txt_small_file_in_dir))
def test_auto_track_large_files(self):
txt_small_file = self.repo_path / "small_file.txt"
txt_small_file.write_text(self.small_content)
txt_large_file = self.repo_path / "large_file.txt"
txt_large_file.write_text(self.large_content)
self.repo.auto_track_large_files()
self.assertTrue(is_tracked_with_lfs(txt_large_file))
self.assertFalse(is_tracked_with_lfs(txt_small_file))
def test_auto_track_binary_files(self):
non_binary_file = self.repo_path / "non_binary_file.txt"
non_binary_file.write_text(self.small_content)
binary_file = self.repo_path / "binary_file.txt"
binary_file.write_text(self.binary_content)
self.repo.auto_track_binary_files()
self.assertFalse(is_tracked_with_lfs(non_binary_file))
self.assertTrue(is_tracked_with_lfs(binary_file))
def test_auto_track_large_files_ignored_with_gitignore(self):
(self.repo_path / "dir").mkdir()
# Test nested gitignores
gitignore_file = self.repo_path / ".gitignore"
gitignore_file.write_text("large_file.txt")
gitignore_file_in_dir = self.repo_path / "dir" / ".gitignore"
gitignore_file_in_dir.write_text("large_file_3.txt")
large_file = self.repo_path / "large_file.txt"
large_file.write_text(self.large_content)
large_file_2 = self.repo_path / "large_file_2.txt"
large_file_2.write_text(self.large_content)
large_file_3 = self.repo_path / "dir" / "large_file_3.txt"
large_file_3.write_text(self.large_content)
large_file_4 = self.repo_path / "dir" / "large_file_4.txt"
large_file_4.write_text(self.large_content)
self.repo.auto_track_large_files()
# Large files
self.assertFalse(is_tracked_with_lfs(large_file))
self.assertTrue(is_tracked_with_lfs(large_file_2))
self.assertFalse(is_tracked_with_lfs(large_file_3))
self.assertTrue(is_tracked_with_lfs(large_file_4))
def test_auto_track_binary_files_ignored_with_gitignore(self):
(self.repo_path / "dir").mkdir()
# Test nested gitignores
gitignore_file = self.repo_path / ".gitignore"
gitignore_file.write_text("binary_file.txt")
gitignore_file_in_dir = self.repo_path / "dir" / ".gitignore"
gitignore_file_in_dir.write_text("binary_file_3.txt")
binary_file = self.repo_path / "binary_file.txt"
binary_file.write_text(self.binary_content)
binary_file_2 = self.repo_path / "binary_file_2.txt"
binary_file_2.write_text(self.binary_content)
binary_file_3 = self.repo_path / "dir" / "binary_file_3.txt"
binary_file_3.write_text(self.binary_content)
binary_file_4 = self.repo_path / "dir" / "binary_file_4.txt"
binary_file_4.write_text(self.binary_content)
self.repo.auto_track_binary_files()
# Binary files
self.assertFalse(is_tracked_with_lfs(binary_file))
self.assertTrue(is_tracked_with_lfs(binary_file_2))
self.assertFalse(is_tracked_with_lfs(binary_file_3))
self.assertTrue(is_tracked_with_lfs(binary_file_4))
def test_auto_track_large_files_through_git_add(self):
txt_small_file = self.repo_path / "small_file.txt"
txt_small_file.write_text(self.small_content)
txt_large_file = self.repo_path / "large_file.txt"
txt_large_file.write_text(self.large_content)
self.repo.git_add(auto_lfs_track=True)
self.assertTrue(is_tracked_with_lfs(txt_large_file))
self.assertFalse(is_tracked_with_lfs(txt_small_file))
def test_auto_track_binary_files_through_git_add(self):
non_binary_file = self.repo_path / "small_file.txt"
non_binary_file.write_text(self.small_content)
binary_file = self.repo_path / "binary.txt"
binary_file.write_text(self.binary_content)
self.repo.git_add(auto_lfs_track=True)
self.assertTrue(is_tracked_with_lfs(binary_file))
self.assertFalse(is_tracked_with_lfs(non_binary_file))
def test_auto_no_track_large_files_through_git_add(self):
txt_small_file = self.repo_path / "small_file.txt"
txt_small_file.write_text(self.small_content)
txt_large_file = self.repo_path / "large_file.txt"
txt_large_file.write_text(self.large_content)
self.repo.git_add(auto_lfs_track=False)
self.assertFalse(is_tracked_with_lfs(txt_large_file))
self.assertFalse(is_tracked_with_lfs(txt_small_file))
def test_auto_no_track_binary_files_through_git_add(self):
non_binary_file = self.repo_path / "small_file.txt"
non_binary_file.write_text(self.small_content)
binary_file = self.repo_path / "binary.txt"
binary_file.write_text(self.binary_content)
self.repo.git_add(auto_lfs_track=False)
self.assertFalse(is_tracked_with_lfs(binary_file))
self.assertFalse(is_tracked_with_lfs(non_binary_file))
def test_auto_track_updates_removed_gitattributes(self):
txt_small_file = self.repo_path / "small_file.txt"
txt_small_file.write_text(self.small_content)
txt_large_file = self.repo_path / "large_file.txt"
txt_large_file.write_text(self.large_content)
self.repo.git_add(auto_lfs_track=True)
self.assertTrue(is_tracked_with_lfs(txt_large_file))
self.assertFalse(is_tracked_with_lfs(txt_small_file))
# Remove large file
txt_large_file.unlink()
# Auto track should remove the entry from .gitattributes
self.repo.auto_track_large_files()
# Recreate the large file with smaller contents
txt_large_file.write_text(self.small_content)
# Ensure the file is not LFS tracked anymore
self.repo.auto_track_large_files()
self.assertFalse(is_tracked_with_lfs(txt_large_file))
def test_checkout_non_existing_branch(self):
self.assertRaises(EnvironmentError, self.repo.git_checkout, "brand-new-branch")
def test_checkout_new_branch(self):
self.repo.git_checkout("new-branch", create_branch_ok=True)
self.assertEqual(self.repo.current_branch, "new-branch")
def test_is_not_tracked_upstream(self):
self.repo.git_checkout("new-branch", create_branch_ok=True)
self.assertFalse(is_tracked_upstream(self.repo.local_dir))
def test_no_branch_checked_out_raises(self):
head_commit_ref = run_subprocess("git show --oneline -s", folder=self.repo_path).stdout.split()[0]
self.repo.git_checkout(head_commit_ref)
self.assertRaises(OSError, is_tracked_upstream, self.repo.local_dir)
@expect_deprecation("Repository")
def test_repo_init_checkout_default_revision(self):
# Instantiate repository on a given revision
repo = Repository(self.repo_path, revision="new-branch")
self.assertEqual(repo.current_branch, "new-branch")
# The revision should be kept when re-initializing the repo
repo_2 = Repository(self.repo_path)
self.assertEqual(repo_2.current_branch, "new-branch")
@expect_deprecation("Repository")
def test_repo_init_checkout_revision(self):
current_head_hash = self.repo.git_head_hash()
(self.repo_path / "file.txt").write_text("hello world")
self.repo.git_add()
self.repo.git_commit("Add file.txt")
new_head_hash = self.repo.git_head_hash()
self.assertNotEqual(current_head_hash, new_head_hash)
previous_head_repo = Repository(self.repo_path, revision=current_head_hash)
files = os.listdir(previous_head_repo.local_dir)
self.assertNotIn("file.txt", files)
current_head_repo = Repository(self.repo_path, revision=new_head_hash)
files = os.listdir(current_head_repo.local_dir)
self.assertIn("file.txt", files)
@expect_deprecation("Repository")
def test_repo_user(self):
_ = Repository(self.repo_path, token=TOKEN)
username = run_subprocess("git config user.name", folder=self.repo_path).stdout
email = run_subprocess("git config user.email", folder=self.repo_path).stdout
# hardcode values to avoid another api call to whoami
self.assertEqual(username.strip(), "Dummy User")
self.assertEqual(email.strip(), "[email protected]")
@expect_deprecation("Repository")
def test_repo_passed_user(self):
_ = Repository(self.repo_path, token=TOKEN, git_user="RANDOM_USER", git_email="[email protected]")
username = run_subprocess("git config user.name", folder=self.repo_path).stdout
email = run_subprocess("git config user.email", folder=self.repo_path).stdout
self.assertEqual(username.strip(), "RANDOM_USER")
self.assertEqual(email.strip(), "[email protected]")
def test_add_tag(self):
self.repo.add_tag("v4.6.0")
self.assertTrue(self.repo.tag_exists("v4.6.0"))
def test_add_annotated_tag(self):
self.repo.add_tag("v4.6.0", message="This is an annotated tag")
self.assertTrue(self.repo.tag_exists("v4.6.0"))
result = run_subprocess("git tag -n9", folder=self.repo_path).stdout.strip()
self.assertIn("This is an annotated tag", result)
def test_delete_tag(self):
self.repo.add_tag("v4.6.0", message="This is an annotated tag")
self.assertTrue(self.repo.tag_exists("v4.6.0"))
self.repo.delete_tag("v4.6.0")
self.assertFalse(self.repo.tag_exists("v4.6.0"))
def test_repo_clean(self):
self.assertTrue(self.repo.is_repo_clean())
(self.repo_path / "file.txt").write_text("hello world")
self.assertFalse(self.repo.is_repo_clean())
class TestRepositoryDataset(RepositoryTestAbstract):
"""Class to test that cloning from a different repo_type works fine."""
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.repo_url = cls._api.create_repo(repo_id=repo_name(), repo_type="dataset")
cls.repo_id = cls.repo_url.repo_id
cls._api.upload_file(
path_or_fileobj=cls.binary_content.encode(),
path_in_repo="file.txt",
repo_id=cls.repo_id,
repo_type="dataset",
)
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls._api.delete_repo(repo_id=cls.repo_id, repo_type="dataset")
@expect_deprecation("Repository")
def test_clone_dataset_with_endpoint_explicit_repo_type(self):
Repository(
self.repo_path, clone_from=self.repo_url, repo_type="dataset", git_user="ci", git_email="[email protected]"
)
self.assertTrue((self.repo_path / "file.txt").exists())
@expect_deprecation("Repository")
def test_clone_dataset_with_endpoint_implicit_repo_type(self):
self.assertIn("dataset", self.repo_url) # Implicit
Repository(self.repo_path, clone_from=self.repo_url, git_user="ci", git_email="[email protected]")
self.assertTrue((self.repo_path / "file.txt").exists())
@expect_deprecation("Repository")
def test_clone_dataset_with_repo_id_and_repo_type(self):
Repository(
self.repo_path, clone_from=self.repo_id, repo_type="dataset", git_user="ci", git_email="[email protected]"
)
self.assertTrue((self.repo_path / "file.txt").exists())
@expect_deprecation("Repository")
def test_clone_dataset_no_ci_user_and_email(self):
Repository(self.repo_path, clone_from=self.repo_id, repo_type="dataset")
self.assertTrue((self.repo_path / "file.txt").exists())
@expect_deprecation("Repository")
def test_clone_dataset_with_repo_name_and_repo_type_fails(self):
with self.assertRaises(EnvironmentError):
Repository(
self.repo_path,
clone_from=self.repo_id.split("/")[1],
repo_type="dataset",
token=TOKEN,
git_user="ci",
git_email="[email protected]",
)