Skip to content

Commit d81a3f6

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

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

tests/conftest.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 os
5+
import subprocess
6+
import shlex
7+
import tempfile
8+
import pytest
9+
import git
10+
11+
git_extras_cwd = os.getcwd()
12+
13+
@pytest.fixture(scope="module")
14+
def git_repo():
15+
tmp_dir = tempfile.mkdtemp()
16+
tmp_file_a = tempfile.mkstemp(dir=tmp_dir)
17+
tmp_file_b = tempfile.mkstemp(dir=tmp_dir)
18+
repo = git.Repo.init(tmp_dir)
19+
os.chdir(tmp_dir)
20+
repo.git.add(".")
21+
repo.git.config("--local", "user.name", "test")
22+
repo.git.config("--local", "user.email", "test@git-extras.com")
23+
repo.git.commit("-m", "chore: initial commit")
24+
return [repo, tmp_dir, tmp_file_a, tmp_file_b]

tests/helper.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
import subprocess
3+
4+
def invoke_git_extras_command(name):
5+
current_dir = os.path.dirname(os.path.abspath(__file__))
6+
git_extras_bin = os.path.join(current_dir, "..", "bin")
7+
return subprocess.run(os.path.join(git_extras_bin, name), capture_output=True)

tests/test_git_abort.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
import subprocess
3+
import shlex
4+
import git
5+
from helper import invoke_git_extras_command
6+
7+
class TestGitAbort:
8+
def test_init(self, git_repo):
9+
repo, tmp_dir, tmp_a, tmp_b = git_repo
10+
repo.git.branch("A")
11+
repo.git.branch("B")
12+
repo.git.checkout("A")
13+
with open(tmp_a[1], "w", encoding="utf-8") as file_a_on_A:
14+
file_a_on_A.write("a")
15+
file_a_on_A.close()
16+
repo.git.add(".")
17+
repo.git.commit("-m", "A")
18+
repo.git.checkout("B")
19+
with open(tmp_a[1], "w", encoding="utf-8") as file_a_on_B:
20+
file_a_on_B.write("b")
21+
file_a_on_B.close()
22+
repo.git.add(".")
23+
repo.git.commit("-m", "B")
24+
repo.git.status()
25+
26+
def test_cherry_pick(self, git_repo):
27+
repo = git_repo[0]
28+
try:
29+
repo.git.cherry_pick("A")
30+
except git.GitCommandError:
31+
pass
32+
result = repo.git.status()
33+
assert "Unmerged path" in result
34+
invoke_git_extras_command("git-abort")
35+
result = repo.git.status()
36+
assert "nothing to commit, working tree clean" in result
37+
38+
def test_merge(self, git_repo):
39+
repo = git_repo[0]
40+
try:
41+
repo.git.merge("A")
42+
except git.GitCommandError:
43+
pass
44+
result = repo.git.status()
45+
assert "Unmerged path" in result
46+
invoke_git_extras_command("git-abort")
47+
result = repo.git.status()
48+
assert "nothing to commit, working tree clean" in result
49+
50+
def test_rebase(self, git_repo):
51+
repo = git_repo[0]
52+
try:
53+
repo.git.rebase("A")
54+
except git.GitCommandError:
55+
pass
56+
result = repo.git.status()
57+
assert "Unmerged path" in result
58+
invoke_git_extras_command("git-abort")
59+
result = repo.git.status()
60+
assert "nothing to commit, working tree clean" in result
61+
62+
def test_revert(self, git_repo):
63+
repo = git_repo[0]
64+
try:
65+
repo.git.revert("A")
66+
except git.GitCommandError:
67+
pass
68+
result = repo.git.status()
69+
assert "Unmerged path" in result
70+
invoke_git_extras_command("git-abort")
71+
result = repo.git.status()
72+
assert "nothing to commit, working tree clean" in result

0 commit comments

Comments
 (0)