From 36ea76707778743b69e6cbebd068112a8b947c76 Mon Sep 17 00:00:00 2001 From: vanpipy Date: Wed, 23 Aug 2023 21:16:51 +0800 Subject: [PATCH 1/2] feat(ci): add pull request action and pytest.ini --- .github/workflows/ci.yml | 17 +++++++++++++++++ .pytest.ini | 4 ++++ Makefile | 3 +++ Readme.md | 12 ++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 .pytest.ini diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7e84d3ec..d65381b40 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.pytest.ini b/.pytest.ini new file mode 100644 index 000000000..bc650af37 --- /dev/null +++ b/.pytest.ini @@ -0,0 +1,4 @@ +[pytest] +minversion = 7.4 +addopts = -ra -q +testpaths = tests diff --git a/Makefile b/Makefile index 80a851c25..811f400ee 100644 --- a/Makefile +++ b/Makefile @@ -138,4 +138,7 @@ docclean: rm -f man/*.1 rm -f man/*.html +test: + pytest + .PHONY: default docs clean docclean check install uninstall diff --git a/Readme.md b/Readme.md index 885499a5c..21d864411 100644 --- a/Readme.md +++ b/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! From e11ba174d7fb26e6db046800fda9410fc52e05c5 Mon Sep 17 00:00:00 2001 From: vanpipy Date: Wed, 23 Aug 2023 21:17:16 +0800 Subject: [PATCH 2/2] test(git-abort): add its unit test --- tests/conftest.py | 18 +++++++++++ tests/helper.py | 66 +++++++++++++++++++++++++++++++++++++++++ tests/test_git_abort.py | 65 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/helper.py create mode 100644 tests/test_git_abort.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..40033e8e4 --- /dev/null +++ b/tests/conftest.py @@ -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 diff --git a/tests/helper.py b/tests/helper.py new file mode 100644 index 000000000..3c2698bb7 --- /dev/null +++ b/tests/helper.py @@ -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) diff --git a/tests/test_git_abort.py b/tests/test_git_abort.py new file mode 100644 index 000000000..38e52c574 --- /dev/null +++ b/tests/test_git_abort.py @@ -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