Skip to content

Commit 6c85f97

Browse files
committed
test(git-archive-file): add unit test
1 parent 5fe4226 commit 6c85f97

File tree

3 files changed

+132
-15
lines changed

3 files changed

+132
-15
lines changed

tests/conftest.py

+18-4
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", "test@git-extras.com")
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

+43-11
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

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

0 commit comments

Comments
 (0)