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

refactor: test utils and simplify the case code #5

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 13 additions & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The test part depends on:

So the versions are higher than above is recommended.

# How to test
# How to run the tests
1. Install `poetry`
2. Install the dependencies via `poetry install --no-root`
3. Run `poetry run pytest`
Expand All @@ -26,6 +26,18 @@ It is done or go without `poetry`,

The second way maybe blocked the some missing dependencies at someday, so the first one is recommended.

# What and how to create a unit test
One command has a unit test, because one `git-*` command is just do one thing, so we can eat a piece of `git-*` command in one time.

For example,

1. The `git-alias` should has a test suite, so create `test_git_alias.py` in the directory `test`
2. Create a test class `TestGitAlias` in the `test_git_alias.py`
3. Create a test case `test_init`, and some test fixtures can be used, `temp_repo`, `named_temp_repo` etc.
* `temp_repo` is module scoped fixture which create a temporary directory and available in the test suite `test_git_alias.py`.
* `named_temp_repo` is just same as `temp_repo` except the custom directory renaming.
4. Loop the third step until the 100% coverage of the function of the `git-alias`

# References
* [poetry](https://github.com/python-poetry/poetry)
* [pytest](https://github.com/pytest-dev/pytest/)
Expand Down
28 changes: 27 additions & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os, subprocess, shutil, tempfile
from git import Repo
from git import Repo, GitCommandError
from testpath import MockCommand, modified_env

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
GIT_EXTRAS_BIN = os.path.abspath(os.path.join(CURRENT_DIR, "..", "bin"))
GIT_EXTRAS_HELPER = os.path.abspath(os.path.join(CURRENT_DIR, "..", "helper"))

GITHUB_ORIGIN = "https://github.com/tj/git-extras.git"
GITLAB_ORIGIN = "https://gitlab.com/tj/git-extras.git"
BITBUCKET_ORIGIN = "https://bitbucket.org/tj/git-extras.git"

class TempRepository:
def __init__(self, repo_work_dir = None):
self._system_tmpdir = tempfile.gettempdir()
Expand All @@ -16,6 +21,7 @@ def __init__(self, repo_work_dir = None):
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:]
self._git_repo = Repo.init(repo_work_dir, b="default")
self._files = []
self.change_origin_to_github()

def switch_cwd_under_repo(self):
os.chdir(self._cwd)
Expand All @@ -33,6 +39,10 @@ def get_repo_git(self):
def get_file(self, index):
return self._files[index]

def get_filename(self, index):
file = self._files[index]
return file[1:]

def get_files(self):
return self._files

Expand Down Expand Up @@ -98,3 +108,19 @@ def invoke_installed_extras_command(self, name, *params):
script = [temp_extras_command, *params]
print(f"Run the script \"{script}\"")
return subprocess.run(script, capture_output=True)

def change_origin(self, origin_url):
try:
self._git_repo.git.remote("add", "origin", origin_url)
except GitCommandError:
pass
self._git_repo.git.remote("set-url", "origin", origin_url)

def change_origin_to_github(self):
self.change_origin(GITHUB_ORIGIN)

def change_origin_to_gitlab(self):
self.change_origin(GITLAB_ORIGIN)

def change_origin_to_bitbucket(self):
self.change_origin(BITBUCKET_ORIGIN)
Loading
Loading