Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auto-test): CI with pytest #1066

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
minversion = 7.4
addopts = -ra -q
testpaths = tests
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@ docclean:
rm -f man/*.1
rm -f man/*.html

test:
pytest

.PHONY: default docs clean docclean check install uninstall
12 changes: 12 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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", "[email protected]")
git.commit("-m", "chore: initial commit")
return repo
66 changes: 66 additions & 0 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -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 changes: 65 additions & 0 deletions tests/test_git_abort.py
Original file line number Diff line number Diff line change
@@ -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