Skip to content

Commit 04bf403

Browse files
authored
test(git-archive-file): add unit test (tj#1084)
1 parent 5fe4226 commit 04bf403

File tree

3 files changed

+131
-15
lines changed

3 files changed

+131
-15
lines changed

tests/conftest.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,29 @@
44
import pytest
55
from helper import TempRepository
66

7-
@pytest.fixture(scope="module")
8-
def temp_repo():
9-
repo = TempRepository()
10-
git = repo.get_repo_git()
7+
def create_repo(dirname = None):
8+
repo = TempRepository(dirname)
119
tmp_file_a = repo.create_tmp_file()
1210
tmp_file_b = repo.create_tmp_file()
1311
repo.switch_cwd_under_repo()
12+
return repo
13+
14+
def init_repo_git_status(repo):
15+
git = repo.get_repo_git()
1416
git.add(".")
1517
git.config("--local", "user.name", "test")
1618
git.config("--local", "user.email", "[email protected]")
1719
git.commit("-m", "chore: initial commit")
20+
21+
@pytest.fixture(scope="module")
22+
def temp_repo():
23+
repo = create_repo()
24+
init_repo_git_status(repo)
25+
return repo
26+
27+
@pytest.fixture(scope="module")
28+
def named_temp_repo(request):
29+
dirname = request.param
30+
repo = create_repo(dirname)
31+
init_repo_git_status(repo)
1832
return repo

tests/helper.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import os
2-
import subprocess
3-
import shutil
4-
import tempfile
5-
import git
1+
import os, subprocess, stat, shutil, tempfile, git
2+
3+
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
4+
GIT_EXTRAS_BIN = os.path.join(CURRENT_DIR, "..", "bin")
5+
GIT_EXTRAS_HELPER = os.path.join(CURRENT_DIR, "..", "helper")
66

77
def invoke_git_extras_command(name, *params):
8-
current_dir = os.path.dirname(os.path.abspath(__file__))
9-
git_extras_bin = os.path.join(current_dir, "..", "bin")
10-
script = [os.path.join(git_extras_bin, name), *params]
8+
script = [os.path.join(GIT_EXTRAS_BIN, name), *params]
119
print(f"Run the script \"{script}\"")
1210
return subprocess.run(script, capture_output=True)
1311

1412
class TempRepository:
1513
def __init__(self, repo_work_dir = None):
14+
self._system_tmpdir = tempfile.gettempdir()
1615
if repo_work_dir == None:
1716
repo_work_dir = tempfile.mkdtemp()
17+
else:
18+
repo_work_dir = os.path.join(self._system_tmpdir, repo_work_dir)
1819
self._cwd = repo_work_dir
20+
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:]
1921
self._git_repo = git.Repo.init(repo_work_dir)
2022
self._files = []
2123

@@ -26,6 +28,9 @@ def switch_cwd_under_repo(self):
2628
def get_cwd(self):
2729
return self._cwd
2830

31+
def get_repo_dirname(self):
32+
return self._tempdirname
33+
2934
def get_repo_git(self):
3035
return self._git_repo.git
3136

@@ -63,6 +68,33 @@ def teardown(self):
6368
print(f"The temp directory {self._cwd} has been removed")
6469

6570
def invoke_extras_command(self, name, *params):
66-
command = "git-" + name
67-
print(f"Invoke the git-extras command - {command}")
68-
return invoke_git_extras_command(command, *params)
71+
command_name = "git-" + name
72+
print(f"Invoke the git-extras command - {command_name}")
73+
return invoke_git_extras_command(command_name, *params)
74+
75+
def invoke_installed_extras_command(self, name, *params):
76+
command_name = "git-" + name
77+
print(f"Invoke the git-extras command - {command_name}")
78+
origin_extras_command = os.path.join(GIT_EXTRAS_BIN, command_name)
79+
temp_extras_command = os.path.join(self._cwd, command_name)
80+
helpers = [
81+
os.path.join(GIT_EXTRAS_HELPER, "git-extra-utility"),
82+
os.path.join(GIT_EXTRAS_HELPER, "is-git-repo")]
83+
84+
if not os.path.exists(temp_extras_command):
85+
whole = []
86+
with open(temp_extras_command, "w") as t:
87+
for helper in helpers:
88+
with open(helper) as h:
89+
content = h.read()
90+
whole.extend(content.splitlines())
91+
with open(origin_extras_command) as o:
92+
content = o.read()
93+
first, *rest = content.splitlines()
94+
whole.extend(rest)
95+
whole.insert(0, first)
96+
t.write("\n".join(whole))
97+
print("Update file {temp_extras_command}:\n{t.read()}")
98+
os.chmod(temp_extras_command, 0o775)
99+
100+
return subprocess.run([temp_extras_command, *params], capture_output=True)

tests/test_archive_file.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os, pytest
2+
3+
class TestGitArchiveFile:
4+
def test_init(self, temp_repo):
5+
git = temp_repo.get_repo_git()
6+
tmp_file = temp_repo.get_file(0)
7+
temp_repo.writefile(tmp_file, "data")
8+
git.add(".")
9+
git.commit("-m", "test: add data")
10+
git.tag("0.1.0", "-m", "bump: 0.1.0")
11+
12+
def test_archive_file_on_tags_branch(self, temp_repo):
13+
git = temp_repo.get_repo_git()
14+
git.checkout("-b", "tags0.1.0")
15+
temp_repo.invoke_installed_extras_command("archive-file")
16+
filename = "{0}.{1}.zip".format(temp_repo.get_repo_dirname(), git.describe())
17+
assert filename in os.listdir()
18+
19+
def test_archive_file_on_any_not_tags_branch_without_default_branch(self, temp_repo):
20+
git = temp_repo.get_repo_git()
21+
git.checkout("-b", "not-tags-branch")
22+
temp_repo.invoke_installed_extras_command("archive-file")
23+
filename = "{0}.{1}.{2}.zip".format(
24+
temp_repo.get_repo_dirname(),
25+
git.describe("--always", "--long"),
26+
"not-tags-branch")
27+
assert filename in os.listdir()
28+
29+
def test_archive_file_on_any_not_tags_branch_with_default_branch(self, temp_repo):
30+
git = temp_repo.get_repo_git()
31+
git.checkout("master")
32+
git.config("git-extras.default-branch", "master")
33+
temp_repo.invoke_installed_extras_command("archive-file")
34+
filename = "{0}.{1}.zip".format(
35+
temp_repo.get_repo_dirname(),
36+
git.describe("--always", "--long"))
37+
assert filename in os.listdir()
38+
39+
def test_archive_file_on_branch_name_has_slash(self, temp_repo):
40+
git = temp_repo.get_repo_git()
41+
git.checkout("-b", "feature/slash")
42+
temp_repo.invoke_installed_extras_command("archive-file")
43+
filename = "{0}.{1}.{2}.zip".format(
44+
temp_repo.get_repo_dirname(),
45+
git.describe("--always", "--long"),
46+
"feature-slash")
47+
assert filename in os.listdir()
48+
49+
@pytest.mark.parametrize("named_temp_repo", ["backslash\\dir"], indirect=True)
50+
def test_archive_file_on_dirname_has_backslash(self, named_temp_repo):
51+
named_temp_repo.invoke_installed_extras_command("archive-file")
52+
git = named_temp_repo.get_repo_git()
53+
filename = "{0}.{1}.{2}.zip".format(
54+
"backslash-dir",
55+
git.describe("--always", "--long"),
56+
"master")
57+
assert filename in os.listdir()
58+
59+
def test_archive_file_on_tag_name_has_slash(self, temp_repo):
60+
temp_repo.switch_cwd_under_repo()
61+
git = temp_repo.get_repo_git()
62+
git.checkout("master")
63+
git.tag("--delete", "0.1.0")
64+
git.tag("0.1.0/slash", "-m", "bump: 0.1.0")
65+
temp_repo.invoke_installed_extras_command("archive-file")
66+
description_include_version = git.describe("--always", "--long")
67+
filename = "{0}.{1}.zip".format(
68+
temp_repo.get_repo_dirname(),
69+
description_include_version.replace("/", "-"))
70+
assert filename in os.listdir()

0 commit comments

Comments
 (0)