Skip to content

Commit e11ba17

Browse files
committed
test(git-abort): add its unit test
1 parent 36ea767 commit e11ba17

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

tests/conftest.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Sharing fixtures
2+
# Ref: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session
3+
4+
import pytest
5+
from helper import TempRepository
6+
7+
@pytest.fixture(scope="module")
8+
def temp_repo():
9+
repo = TempRepository()
10+
git = repo.get_repo_git()
11+
tmp_file_a = repo.create_tmp_file()
12+
tmp_file_b = repo.create_tmp_file()
13+
repo.switch_cwd_under_repo()
14+
git.add(".")
15+
git.config("--local", "user.name", "test")
16+
git.config("--local", "user.email", "test@git-extras.com")
17+
git.commit("-m", "chore: initial commit")
18+
return repo

tests/helper.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
import subprocess
3+
import shutil
4+
import tempfile
5+
import git
6+
7+
def invoke_git_extras_command(name):
8+
current_dir = os.path.dirname(os.path.abspath(__file__))
9+
git_extras_bin = os.path.join(current_dir, "..", "bin")
10+
return subprocess.run(os.path.join(git_extras_bin, name), capture_output=True)
11+
12+
class TempRepository:
13+
def __init__(self, repo_work_dir = None):
14+
if repo_work_dir == None:
15+
repo_work_dir = tempfile.mkdtemp()
16+
self._cwd = repo_work_dir
17+
self._git_repo = git.Repo.init(repo_work_dir)
18+
self._files = []
19+
20+
def switch_cwd_under_repo(self):
21+
os.chdir(self._cwd)
22+
print(f"The current work directory has switched to {self._cwd}")
23+
24+
def get_cwd(self):
25+
return self._cwd
26+
27+
def get_repo_git(self):
28+
return self._git_repo.git
29+
30+
def get_file(self, index):
31+
return self._files[index]
32+
33+
def get_files(self):
34+
return self._files
35+
36+
def create_tmp_dir(self):
37+
tmp_dir = tempfile.mkdtemp()
38+
return tmp_dir
39+
40+
def create_tmp_file(self, temp_dir = None):
41+
if temp_dir == None:
42+
temp_dir = self._cwd
43+
44+
tmp_file = tempfile.mkstemp(dir=temp_dir)
45+
self._files.append(tmp_file[1])
46+
return tmp_file
47+
48+
def remove_tmp_file(self, file_path):
49+
os.remove(file_path)
50+
print(f"File {file_path} has been removed")
51+
52+
def writefile(self, temp_file, data):
53+
if data == None:
54+
return
55+
56+
with open(temp_file, "w", encoding="utf-8") as f:
57+
f.write(data)
58+
59+
def teardown(self):
60+
shutil.rmtree(self._cwd, ignore_errors=True)
61+
print(f"The temp directory {self._cwd} has been removed")
62+
63+
def invoke_extras_command(self, name):
64+
command = "git-" + name
65+
print(f"Invoke the git-extras command - {command}")
66+
return invoke_git_extras_command(command)

tests/test_git_abort.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from git import GitCommandError
2+
3+
class TestGitAbort:
4+
def test_init(self, temp_repo):
5+
git = temp_repo.get_repo_git()
6+
tmp_file = temp_repo.get_file(0)
7+
git.branch("A")
8+
git.branch("B")
9+
git.checkout("A")
10+
temp_repo.writefile(tmp_file, "a")
11+
git.add(".")
12+
git.commit("-m", "A")
13+
git.checkout("B")
14+
temp_repo.writefile(tmp_file, "b")
15+
git.add(".")
16+
git.commit("-m", "B")
17+
git.status()
18+
19+
def test_cherry_pick(self, temp_repo):
20+
git = temp_repo.get_repo_git()
21+
try:
22+
git.cherry_pick("A")
23+
except GitCommandError as err:
24+
print(err)
25+
result = git.status()
26+
assert "Unmerged path" in result
27+
temp_repo.invoke_extras_command("abort")
28+
result = git.status()
29+
assert "nothing to commit, working tree clean" in result
30+
31+
def test_merge(self, temp_repo):
32+
git = temp_repo.get_repo_git()
33+
try:
34+
git.merge("A")
35+
except GitCommandError as err:
36+
print(err)
37+
result = git.status()
38+
assert "Unmerged path" in result
39+
temp_repo.invoke_extras_command("abort")
40+
result = git.status()
41+
assert "nothing to commit, working tree clean" in result
42+
43+
def test_rebase(self, temp_repo):
44+
git = temp_repo.get_repo_git()
45+
try:
46+
git.rebase("A")
47+
except GitCommandError as err:
48+
print(err)
49+
result = git.status()
50+
assert "Unmerged path" in result
51+
temp_repo.invoke_extras_command("abort")
52+
result = git.status()
53+
assert "nothing to commit, working tree clean" in result
54+
55+
def test_revert(self, temp_repo):
56+
git = temp_repo.get_repo_git()
57+
try:
58+
git.revert("A")
59+
except GitCommandError as err:
60+
print(err)
61+
result = git.status()
62+
assert "Unmerged path" in result
63+
temp_repo.invoke_extras_command("abort")
64+
result = git.status()
65+
assert "nothing to commit, working tree clean" in result

0 commit comments

Comments
 (0)