forked from tj/git-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auto-test): CI with pytest (tj#1066)
* feat(ci): add pull request action and pytest.ini * test(git-abort): add its unit test
- Loading branch information
Showing
7 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[pytest] | ||
minversion = 7.4 | ||
addopts = -ra -q | ||
testpaths = tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |