Skip to content

Commit a17d2fb

Browse files
authored
refactor: test utils and simplify the case code (#1136)
* 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
1 parent 52897f7 commit a17d2fb

File tree

5 files changed

+256
-299
lines changed

5 files changed

+256
-299
lines changed

tests/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The test part depends on:
1212

1313
So the versions are higher than above is recommended.
1414

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

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

29+
# What and how to create a unit test
30+
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.
31+
32+
For example,
33+
34+
1. The `git-alias` should have a test suite, so create `test_git_alias.py` in the directory `test`
35+
2. Create a test class `TestGitAlias` in the `test_git_alias.py`
36+
3. Create a test case `test_init`, and some test fixtures can be used, `temp_repo`, `named_temp_repo` etc.
37+
* `temp_repo` is module scoped fixture which create a temporary directory and available in the test suite `test_git_alias.py`.
38+
* `named_temp_repo` is just same as `temp_repo` except the custom directory renaming.
39+
4. Loop the third step until the 100% coverage of the function of the `git-alias`
40+
2941
# References
3042
* [poetry](https://github.com/python-poetry/poetry)
3143
* [pytest](https://github.com/pytest-dev/pytest/)

tests/helper.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import os, subprocess, shutil, tempfile
2-
from git import Repo
2+
from git import Repo, GitCommandError
3+
from testpath import MockCommand, modified_env
34

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

9+
GITHUB_ORIGIN = "https://github.com/tj/git-extras.git"
10+
GITLAB_ORIGIN = "https://gitlab.com/tj/git-extras.git"
11+
BITBUCKET_ORIGIN = "https://bitbucket.org/tj/git-extras.git"
12+
813
class TempRepository:
914
def __init__(self, repo_work_dir = None):
1015
self._system_tmpdir = tempfile.gettempdir()
@@ -16,6 +21,7 @@ def __init__(self, repo_work_dir = None):
1621
self._tempdirname = self._cwd[len(self._system_tmpdir) + 1:]
1722
self._git_repo = Repo.init(repo_work_dir, b="default")
1823
self._files = []
24+
self.change_origin_to_github()
1925

2026
def switch_cwd_under_repo(self):
2127
os.chdir(self._cwd)
@@ -33,6 +39,10 @@ def get_repo_git(self):
3339
def get_file(self, index):
3440
return self._files[index]
3541

42+
def get_filename(self, index):
43+
file = self._files[index]
44+
return file[1:]
45+
3646
def get_files(self):
3747
return self._files
3848

@@ -98,3 +108,19 @@ def invoke_installed_extras_command(self, name, *params):
98108
script = [temp_extras_command, *params]
99109
print(f"Run the script \"{script}\"")
100110
return subprocess.run(script, capture_output=True)
111+
112+
def change_origin(self, origin_url):
113+
try:
114+
self._git_repo.git.remote("add", "origin", origin_url)
115+
except GitCommandError as err:
116+
print(err)
117+
self._git_repo.git.remote("set-url", "origin", origin_url)
118+
119+
def change_origin_to_github(self):
120+
self.change_origin(GITHUB_ORIGIN)
121+
122+
def change_origin_to_gitlab(self):
123+
self.change_origin(GITLAB_ORIGIN)
124+
125+
def change_origin_to_bitbucket(self):
126+
self.change_origin(BITBUCKET_ORIGIN)
File renamed without changes.

0 commit comments

Comments
 (0)