Skip to content

Commit

Permalink
test(git-abort): add its unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpipy committed Sep 8, 2023
1 parent 9fb4b5d commit 252dd06
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 29 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ jobs:
pip install codespell==2.2
git grep --cached -l '' | grep -v -e 'History\.md' -e 'AUTHORS' -e 'man/.*\.1' -e 'man/.*\.html' | xargs codespell --ignore-words=.github/.ignore_words
test:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.10']

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest==7.4.0
- name: Unit test
run: |
pytest
build:
strategy:
fail-fast: false
Expand Down
29 changes: 0 additions & 29 deletions .github/workflows/pull-request.yml

This file was deleted.

4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Just getting started? Check out these screencasts:

See [Installation](Installation.md) page.

## Test
1. `pip install pytest`
2. `pytest`

## Commands

Go to [Commands](Commands.md) page for basic usage and examples.
Expand Down
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sharing fixtures
# Ref: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session

import os
import subprocess
import shlex
import shutil
import tempfile
import pytest

@pytest.fixture(scope="module")
def git_repo():
git_extras_cwd = os.getcwd()
tmp_dir = tempfile.mkdtemp()
tmp_file_a = tempfile.mkstemp(dir=tmp_dir)
tmp_file_b = tempfile.mkstemp(dir=tmp_dir)
os.chdir(tmp_dir)
result = subprocess.run(shlex.split("git init"), capture_output=True)
print(result.stdout.decode())
print(result.stderr.decode())
result = subprocess.run(shlex.split("git add ."), capture_output=True)
print(result.stdout.decode())
print(result.stderr.decode())
subprocess.run(shlex.split("git config --local user.name \"test\""))
subprocess.run(shlex.split("git config --local user.email \"[email protected]\""))
result = subprocess.run(shlex.split("git commit -m 'chore: initial commit'"), capture_output=True)
print(result.stdout.decode())
print(result.stderr.decode())
yield [git_extras_cwd, tmp_dir, tmp_file_a, tmp_file_b]
shutil.rmtree(tmp_dir, ignore_errors=True)
60 changes: 60 additions & 0 deletions tests/test_git-abort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import subprocess
import shlex

class TestGitAbort:
def test_init(self, git_repo):
_, tmp_dir, tmp_a, tmp_b = git_repo
subprocess.run(shlex.split("git branch A"))
subprocess.run(shlex.split("git branch B"))
subprocess.run(shlex.split("git checkout A"))
file_a = open(tmp_a[1], "w", encoding="utf-8")
file_a.write('a')
file_a.close()
subprocess.run(shlex.split("git add ."))
subprocess.run(shlex.split("git commit -m \"A\""))
subprocess.run(shlex.split("git checkout B"))
file_b = open(tmp_a[1], "w", encoding="utf-8")
file_b.write('b')
file_b.close()
subprocess.run(shlex.split("git add ."))
subprocess.run(shlex.split("git commit -m \"B\""))
subprocess.run(shlex.split("git status"))

def test_cherry_pick(self, git_repo):
git_extras_cwd = git_repo[0]
result = subprocess.run(shlex.split("git cherry-pick A"), capture_output=True)
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "Unmerged path" in result.stdout.decode()
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "nothing to commit, working tree clean" in result.stdout.decode()

def test_merge(self, git_repo):
git_extras_cwd = git_repo[0]
git_extras_cwd = git_repo[0]
result = subprocess.run(shlex.split("git merge --allow-unrelated-histories A"), capture_output=True)
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "Unmerged path" in result.stdout.decode()
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "nothing to commit, working tree clean" in result.stdout.decode()

def test_rebase(self, git_repo):
git_extras_cwd = git_repo[0]
git_extras_cwd = git_repo[0]
result = subprocess.run(shlex.split("git rebase A"), capture_output=True)
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "Unmerged path" in result.stdout.decode()
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "nothing to commit, working tree clean" in result.stdout.decode()

def test_revert(self, git_repo):
git_extras_cwd = git_repo[0]
result = subprocess.run(shlex.split("git revert A"), capture_output=True)
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "Unmerged path" in result.stdout.decode()
subprocess.run(shlex.split(os.path.join(git_extras_cwd, 'bin', 'git-abort')))
result = subprocess.run(shlex.split("git status"), capture_output=True)
assert "nothing to commit, working tree clean" in result.stdout.decode()

0 comments on commit 252dd06

Please sign in to comment.