mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
feat(auto-test): CI with pytest (#1066)
* feat(ci): add pull request action and pytest.ini * test(git-abort): add its unit test
This commit is contained in:
parent
49650eda62
commit
d2dde69d14
17
.github/workflows/ci.yml
vendored
17
.github/workflows/ci.yml
vendored
|
|
@ -35,6 +35,23 @@ 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
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up Python 3.10
|
||||
uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.10'
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install pytest==7.4.0
|
||||
pip install GitPython==3.1.36
|
||||
- name: Unit test
|
||||
run: make test
|
||||
|
||||
build:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
|
|
|
|||
4
.pytest.ini
Normal file
4
.pytest.ini
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[pytest]
|
||||
minversion = 7.4
|
||||
addopts = -ra -q
|
||||
testpaths = tests
|
||||
3
Makefile
3
Makefile
|
|
@ -138,4 +138,7 @@ docclean:
|
|||
rm -f man/*.1
|
||||
rm -f man/*.html
|
||||
|
||||
test:
|
||||
pytest
|
||||
|
||||
.PHONY: default docs clean docclean check install uninstall
|
||||
|
|
|
|||
12
Readme.md
12
Readme.md
|
|
@ -18,6 +18,18 @@ Go to the [Commands](Commands.md) page for basic usage and examples.
|
|||
|
||||
__GIT utilities__ -- repo summary, repl, changelog population, author commit percentages and more
|
||||
|
||||
## Test
|
||||
|
||||
Nowaday the continues integrations test is coming and you can access it via the `make test` easily.
|
||||
|
||||
The CI depends on
|
||||
|
||||
* `python==3.10`
|
||||
* `pytest==7.4.0`
|
||||
* `GitPython==3.1.36`
|
||||
|
||||
So the version or higher is recommended.
|
||||
|
||||
## Contributing
|
||||
|
||||
Interested in contributing? Awesome!
|
||||
|
|
|
|||
18
tests/conftest.py
Normal file
18
tests/conftest.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Sharing fixtures
|
||||
# Ref: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session
|
||||
|
||||
import pytest
|
||||
from helper import TempRepository
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def temp_repo():
|
||||
repo = TempRepository()
|
||||
git = repo.get_repo_git()
|
||||
tmp_file_a = repo.create_tmp_file()
|
||||
tmp_file_b = repo.create_tmp_file()
|
||||
repo.switch_cwd_under_repo()
|
||||
git.add(".")
|
||||
git.config("--local", "user.name", "test")
|
||||
git.config("--local", "user.email", "test@git-extras.com")
|
||||
git.commit("-m", "chore: initial commit")
|
||||
return repo
|
||||
66
tests/helper.py
Normal file
66
tests/helper.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
import tempfile
|
||||
import git
|
||||
|
||||
def invoke_git_extras_command(name):
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
git_extras_bin = os.path.join(current_dir, "..", "bin")
|
||||
return subprocess.run(os.path.join(git_extras_bin, name), capture_output=True)
|
||||
|
||||
class TempRepository:
|
||||
def __init__(self, repo_work_dir = None):
|
||||
if repo_work_dir == None:
|
||||
repo_work_dir = tempfile.mkdtemp()
|
||||
self._cwd = repo_work_dir
|
||||
self._git_repo = git.Repo.init(repo_work_dir)
|
||||
self._files = []
|
||||
|
||||
def switch_cwd_under_repo(self):
|
||||
os.chdir(self._cwd)
|
||||
print(f"The current work directory has switched to {self._cwd}")
|
||||
|
||||
def get_cwd(self):
|
||||
return self._cwd
|
||||
|
||||
def get_repo_git(self):
|
||||
return self._git_repo.git
|
||||
|
||||
def get_file(self, index):
|
||||
return self._files[index]
|
||||
|
||||
def get_files(self):
|
||||
return self._files
|
||||
|
||||
def create_tmp_dir(self):
|
||||
tmp_dir = tempfile.mkdtemp()
|
||||
return tmp_dir
|
||||
|
||||
def create_tmp_file(self, temp_dir = None):
|
||||
if temp_dir == None:
|
||||
temp_dir = self._cwd
|
||||
|
||||
tmp_file = tempfile.mkstemp(dir=temp_dir)
|
||||
self._files.append(tmp_file[1])
|
||||
return tmp_file
|
||||
|
||||
def remove_tmp_file(self, file_path):
|
||||
os.remove(file_path)
|
||||
print(f"File {file_path} has been removed")
|
||||
|
||||
def writefile(self, temp_file, data):
|
||||
if data == None:
|
||||
return
|
||||
|
||||
with open(temp_file, "w", encoding="utf-8") as f:
|
||||
f.write(data)
|
||||
|
||||
def teardown(self):
|
||||
shutil.rmtree(self._cwd, ignore_errors=True)
|
||||
print(f"The temp directory {self._cwd} has been removed")
|
||||
|
||||
def invoke_extras_command(self, name):
|
||||
command = "git-" + name
|
||||
print(f"Invoke the git-extras command - {command}")
|
||||
return invoke_git_extras_command(command)
|
||||
65
tests/test_git_abort.py
Normal file
65
tests/test_git_abort.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
from git import GitCommandError
|
||||
|
||||
class TestGitAbort:
|
||||
def test_init(self, temp_repo):
|
||||
git = temp_repo.get_repo_git()
|
||||
tmp_file = temp_repo.get_file(0)
|
||||
git.branch("A")
|
||||
git.branch("B")
|
||||
git.checkout("A")
|
||||
temp_repo.writefile(tmp_file, "a")
|
||||
git.add(".")
|
||||
git.commit("-m", "A")
|
||||
git.checkout("B")
|
||||
temp_repo.writefile(tmp_file, "b")
|
||||
git.add(".")
|
||||
git.commit("-m", "B")
|
||||
git.status()
|
||||
|
||||
def test_cherry_pick(self, temp_repo):
|
||||
git = temp_repo.get_repo_git()
|
||||
try:
|
||||
git.cherry_pick("A")
|
||||
except GitCommandError as err:
|
||||
print(err)
|
||||
result = git.status()
|
||||
assert "Unmerged path" in result
|
||||
temp_repo.invoke_extras_command("abort")
|
||||
result = git.status()
|
||||
assert "nothing to commit, working tree clean" in result
|
||||
|
||||
def test_merge(self, temp_repo):
|
||||
git = temp_repo.get_repo_git()
|
||||
try:
|
||||
git.merge("A")
|
||||
except GitCommandError as err:
|
||||
print(err)
|
||||
result = git.status()
|
||||
assert "Unmerged path" in result
|
||||
temp_repo.invoke_extras_command("abort")
|
||||
result = git.status()
|
||||
assert "nothing to commit, working tree clean" in result
|
||||
|
||||
def test_rebase(self, temp_repo):
|
||||
git = temp_repo.get_repo_git()
|
||||
try:
|
||||
git.rebase("A")
|
||||
except GitCommandError as err:
|
||||
print(err)
|
||||
result = git.status()
|
||||
assert "Unmerged path" in result
|
||||
temp_repo.invoke_extras_command("abort")
|
||||
result = git.status()
|
||||
assert "nothing to commit, working tree clean" in result
|
||||
|
||||
def test_revert(self, temp_repo):
|
||||
git = temp_repo.get_repo_git()
|
||||
try:
|
||||
git.revert("A")
|
||||
except GitCommandError as err:
|
||||
print(err)
|
||||
result = git.status()
|
||||
assert "Unmerged path" in result
|
||||
temp_repo.invoke_extras_command("abort")
|
||||
result = git.status()
|
||||
assert "nothing to commit, working tree clean" in result
|
||||
Loading…
Reference in a new issue