Skip to content

Commit

Permalink
refactor: test utils and simplify the case code (#1136)
Browse files Browse the repository at this point in the history
* doc(tests/README.md): explain the unit test process

* test(TempRepository): support to change origin and get filename

* refactor: simplify the case code

    * test_git_browse_ci.py
    * test_git_browse.py

* fix: rename `test_authors.py` to `test_git_authors.py`

* fix: typo

* fix: log the GitCommandError
  • Loading branch information
vanpipy authored Feb 29, 2024
1 parent 52897f7 commit a17d2fb
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 299 deletions.
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 have 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 as err:
print(err)
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)
File renamed without changes.
Loading

0 comments on commit a17d2fb

Please sign in to comment.