1
1
import shutil
2
2
import subprocess
3
+ import sys
3
4
import tempfile
4
5
import uuid
5
6
from pathlib import Path
9
10
from git .repo .fun import is_git_dir
10
11
from git .util import IterableList
11
12
12
- from .common import BaseTestCase
13
+ from .common import BaseTestCase , CommandResult , run_executable
13
14
14
15
15
16
class Repository :
@@ -30,7 +31,10 @@ def __init__(self, path: str | Path, *args, **kwargs):
30
31
path = Path (path )
31
32
32
33
if "" .join (path .suffixes ) == ".tar.gz" :
33
- self .temp_dir = tempfile .TemporaryDirectory (delete = False )
34
+ if sys .version_info < (3 , 12 , 0 ):
35
+ self .temp_dir = tempfile .TemporaryDirectory ()
36
+ else :
37
+ self .temp_dir = tempfile .TemporaryDirectory (delete = False )
34
38
temp_dir_path = Path (self .temp_dir .name )
35
39
shutil .copy (path , temp_dir_path / path .name )
36
40
subprocess .run (
@@ -40,6 +44,15 @@ def __init__(self, path: str | Path, *args, **kwargs):
40
44
# We are not trying to find out what the root folder in the archive file is.
41
45
# We do not create an archive file that does not have the root folder because
42
46
# whens student extract the archive file, files will be everywhere.
47
+ if not (temp_dir_path / "repo" ).exists ():
48
+ raise FileNotFoundError (
49
+ f"Expect the archive to have folder 'repo', but this 'repo' folder cannot be found after extracting '{ path .name } '"
50
+ )
51
+ if not (temp_dir_path / "repo" / ".git" ).exists ():
52
+ raise FileNotFoundError (
53
+ f"Expect the 'repo' to be a Git repository (it must have .git folder), but '.git' is missing from the 'repo' extracted from '{ path .name } '"
54
+ )
55
+
43
56
self .repo = Repo (temp_dir_path / "repo" , * args , ** kwargs )
44
57
else :
45
58
if is_git_dir (path ):
@@ -87,6 +100,12 @@ def to_gzip_archive(self, path: Path) -> None:
87
100
]
88
101
)
89
102
103
+ def run_executable (self , args : list [str ], timeout : float = 15.0 ) -> CommandResult :
104
+ """
105
+ Run a command using repostiory's working directory as cwd.
106
+ """
107
+ return run_executable (args , cwd = self .repo .working_tree_dir , timeout = timeout )
108
+
90
109
def create_and_add_random_file (
91
110
self , name : str | None = None , content : str | None = None
92
111
) -> str :
@@ -139,6 +158,13 @@ def get_tag_refs_at(self, commit_hash: str) -> list[Tag]:
139
158
matched_tag_refs .append (tag_ref )
140
159
return matched_tag_refs
141
160
161
+ def visualize (self ) -> str :
162
+ """
163
+ Run 'git log --oneline --all --graph --decorate' and returns the output.
164
+ """
165
+ res = self .repo .git .log ("--graph" , "--all" , "--decorate" , "--oneline" )
166
+ return res
167
+
142
168
143
169
class RepositoryBaseTestCase (BaseTestCase ):
144
170
def assertHasTagWithNameAt (self , repo : Repository , name : str , commit_hash : str ):
@@ -148,8 +174,9 @@ def assertHasTagWithNameAt(self, repo: Repository, name: str, commit_hash: str):
148
174
if tag_ref .path == tag_path :
149
175
return
150
176
177
+ tags_text = "\n " .join (tag_ref .path for tag_ref in tag_refs )
151
178
raise self .failureException (
152
- f"Expect to see a tag '{ name } ' at commit '{ commit_hash } ', but found none."
179
+ f"Expect to see a tag '{ name } ' at commit '{ commit_hash } ', but found none. Tags at commit { commit_hash } : \n { tags_text } "
153
180
)
154
181
155
182
def assertHasTagWithNameAndMessageAt (
@@ -166,6 +193,14 @@ def assertHasTagWithNameAndMessageAt(
166
193
):
167
194
return
168
195
196
+ tags_texts = []
197
+ for tag_ref in tag_refs :
198
+ if tag_ref .tag is None :
199
+ text = f"{ tag_ref .path } "
200
+ else :
201
+ text = f"{ tag_ref .path } : { tag_ref .tag .message } "
202
+ tags_texts .append (text )
203
+ tags_text = "\n " .join (tags_texts )
169
204
raise self .failureException (
170
- f"Expect to see a tag '{ name } ' with message '{ message } ' at commit '{ commit_hash } ', but found none."
205
+ f"Expect to see a tag '{ name } ' with message '{ message } ' at commit '{ commit_hash } ', but found none. Tags at commit { commit_hash } : \n { tags_text } "
171
206
)
0 commit comments