Skip to content

Commit 978a24a

Browse files
committed
test(git-abort): add its unit test
1 parent d11b4da commit 978a24a

File tree

5 files changed

+115
-29
lines changed

5 files changed

+115
-29
lines changed

.github/workflows/ci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ jobs:
3535
pip install codespell==2.2
3636
git grep --cached -l '' | grep -v -e 'History\.md' -e 'AUTHORS' -e 'man/.*\.1' -e 'man/.*\.html' | xargs codespell --ignore-words=.github/.ignore_words
3737
38+
test:
39+
runs-on: ubuntu-latest
40+
41+
strategy:
42+
matrix:
43+
python-version: ['3.10']
44+
45+
steps:
46+
- uses: actions/checkout@v3
47+
- name: Set up Python ${{ matrix.python-version }}
48+
uses: actions/setup-python@v4
49+
with:
50+
python-version: ${{ matrix.python-version }}
51+
- name: Install dependencies
52+
run: |
53+
python -m pip install --upgrade pip
54+
pip install pytest==7.4.0
55+
- name: Unit test
56+
run: |
57+
pytest
58+
3859
build:
3960
strategy:
4061
fail-fast: false

.github/workflows/pull-request.yml

-29
This file was deleted.

Readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Just getting started? Check out these screencasts:
1212

1313
See the [Installation](Installation.md) page.
1414

15+
## Test
16+
1. `pip install pytest`
17+
2. `pytest`
18+
1519
## Commands
1620

1721
Go to the [Commands](Commands.md) page for basic usage and examples.

tests/conftest.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 shutil
8+
import tempfile
9+
import pytest
10+
11+
@pytest.fixture(scope="module")
12+
def git_repo():
13+
git_extras_cwd = os.getcwd()
14+
tmp_dir = tempfile.mkdtemp()
15+
tmp_file_a = tempfile.mkstemp(dir=tmp_dir)
16+
tmp_file_b = tempfile.mkstemp(dir=tmp_dir)
17+
os.chdir(tmp_dir)
18+
result = subprocess.run(shlex.split("git init"), capture_output=True)
19+
print(result.stdout.decode())
20+
print(result.stderr.decode())
21+
result = subprocess.run(shlex.split("git add ."), capture_output=True)
22+
print(result.stdout.decode())
23+
print(result.stderr.decode())
24+
subprocess.run(shlex.split("git config --local user.name \"test\""))
25+
subprocess.run(shlex.split("git config --local user.email \"test@git-extras.com\""))
26+
result = subprocess.run(shlex.split("git commit -m 'chore: initial commit'"), capture_output=True)
27+
print(result.stdout.decode())
28+
print(result.stderr.decode())
29+
yield [git_extras_cwd, tmp_dir, tmp_file_a, tmp_file_b]
30+
shutil.rmtree(tmp_dir, ignore_errors=True)

tests/test_git-abort.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import subprocess
3+
import shlex
4+
5+
class TestGitAbort:
6+
def test_init(self, git_repo):
7+
_, tmp_dir, tmp_a, tmp_b = git_repo
8+
subprocess.run(shlex.split("git branch A"))
9+
subprocess.run(shlex.split("git branch B"))
10+
subprocess.run(shlex.split("git checkout A"))
11+
file_a = open(tmp_a[1], "w", encoding="utf-8")
12+
file_a.write('a')
13+
file_a.close()
14+
subprocess.run(shlex.split("git add ."))
15+
subprocess.run(shlex.split("git commit -m \"A\""))
16+
subprocess.run(shlex.split("git checkout B"))
17+
file_b = open(tmp_a[1], "w", encoding="utf-8")
18+
file_b.write('b')
19+
file_b.close()
20+
subprocess.run(shlex.split("git add ."))
21+
subprocess.run(shlex.split("git commit -m \"B\""))
22+
subprocess.run(shlex.split("git status"))
23+
24+
def test_cherry_pick(self, git_repo):
25+
git_extras_cwd = git_repo[0]
26+
result = subprocess.run(shlex.split("git cherry-pick A"), capture_output=True)
27+
result = subprocess.run(shlex.split("git status"), capture_output=True)
28+
assert "Unmerged path" in result.stdout.decode()
29+
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
30+
result = subprocess.run(shlex.split("git status"), capture_output=True)
31+
assert "nothing to commit, working tree clean" in result.stdout.decode()
32+
33+
def test_merge(self, git_repo):
34+
git_extras_cwd = git_repo[0]
35+
git_extras_cwd = git_repo[0]
36+
result = subprocess.run(shlex.split("git merge --allow-unrelated-histories A"), capture_output=True)
37+
result = subprocess.run(shlex.split("git status"), capture_output=True)
38+
assert "Unmerged path" in result.stdout.decode()
39+
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
40+
result = subprocess.run(shlex.split("git status"), capture_output=True)
41+
assert "nothing to commit, working tree clean" in result.stdout.decode()
42+
43+
def test_rebase(self, git_repo):
44+
git_extras_cwd = git_repo[0]
45+
git_extras_cwd = git_repo[0]
46+
result = subprocess.run(shlex.split("git rebase A"), capture_output=True)
47+
result = subprocess.run(shlex.split("git status"), capture_output=True)
48+
assert "Unmerged path" in result.stdout.decode()
49+
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
50+
result = subprocess.run(shlex.split("git status"), capture_output=True)
51+
assert "nothing to commit, working tree clean" in result.stdout.decode()
52+
53+
def test_revert(self, git_repo):
54+
git_extras_cwd = git_repo[0]
55+
result = subprocess.run(shlex.split("git revert A"), capture_output=True)
56+
result = subprocess.run(shlex.split("git status"), capture_output=True)
57+
assert "Unmerged path" in result.stdout.decode()
58+
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
59+
result = subprocess.run(shlex.split("git status"), capture_output=True)
60+
assert "nothing to commit, working tree clean" in result.stdout.decode()

0 commit comments

Comments
 (0)