-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #1136
Changes from 3 commits
fe36835
4c5a8b5
8a3b8bd
b6af257
f084329
6b8517b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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` | ||||||
|
@@ -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` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Could we have a way to measure it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the coverage of the unit test is a good way to measure it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pytest coverage will be update in another PR. |
||||||
|
||||||
# References | ||||||
* [poetry](https://github.com/python-poetry/poetry) | ||||||
* [pytest](https://github.com/pytest-dev/pytest/) | ||||||
|
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() | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should log the error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The log is better but the throw is not expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update |
||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rename https://github.com/tj/git-extras/blob/main/tests/test_authors.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be, update later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update