-
Notifications
You must be signed in to change notification settings - Fork 165
add test using git #445
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
Draft
mawillcockson
wants to merge
9
commits into
pypa:main
Choose a base branch
from
mawillcockson:test/with_git
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add test using git #445
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a1b1b4d
test: add tests using git (#347)
mawillcockson 872cf57
test(conftest): str.split() -> shlex.split()
mawillcockson b245dcf
test(conftest): tmp_git -> tmp_git_repo
mawillcockson ed360ad
test: rename git_cmd -> git and make closure
mawillcockson ea1e051
test: remove type annotations
mawillcockson 7430aee
test: explicitly use only needsgit marker
mawillcockson a47ae16
test: ensure env vars are removed
mawillcockson 8696c3a
test: make marker descriptions match
mawillcockson 4d9fe74
test: remove needgit from func name
mawillcockson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,104 @@ | ||
| import shlex | ||
| from functools import wraps | ||
| from pathlib import Path | ||
| from shutil import copy, copytree, which | ||
| from subprocess import check_output | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from shutil import copytree | ||
|
|
||
| samples_dir = Path(__file__).parent / 'samples' | ||
| samples_dir = Path(__file__).parent / "samples" | ||
|
|
||
| skip_if_no_git = pytest.mark.skipif( | ||
| (not which("git")), | ||
| reason="needs git to be installed and findable through the PATH environment variable", | ||
| ) | ||
|
|
||
|
|
||
| def pytest_collection_modifyitems(items): | ||
| for item in items: | ||
| if "needgit" in item.nodeid or "needsgit" in item.nodeid: | ||
| item.add_marker(skip_if_no_git) | ||
| item.add_marker(pytest.mark.needgit) | ||
| item.add_marker(pytest.mark.needsgit) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def copy_sample(tmp_path): | ||
| """Copy a subdirectory from the samples dir to a temp dir""" | ||
|
|
||
| def copy(dirname): | ||
| dst = tmp_path / dirname | ||
| copytree(str(samples_dir / dirname), str(dst)) | ||
| return dst | ||
|
|
||
| return copy | ||
|
|
||
|
|
||
| def git_cmd(repository_path, command): | ||
| if isinstance(command, str): | ||
| args = shlex.split(command) | ||
| else: | ||
| args = command | ||
|
|
||
| return check_output( | ||
| ["git", "-C", str(repository_path), *args], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmp_git_repo(tmp_path): | ||
| """ | ||
| Make a git repository in a temporary folder | ||
|
|
||
| The path returned is what should be passed to git's -C command, or what cwd | ||
| should be set to in subprocess calls | ||
| """ | ||
| git_global_config = tmp_path / "git_global_config" | ||
| git_global_config.touch(exist_ok=False) | ||
| repository = tmp_path / "repository" | ||
| repository.mkdir(exist_ok=False) | ||
| with patch.dict( | ||
| "os.environ", | ||
| { | ||
| # https://git-scm.com/docs/git#Documentation/git.txt-codeGITCONFIGGLOBALcode | ||
| "GIT_CONFIG_GLOBAL": str(git_global_config), | ||
| # https://git-scm.com/docs/git#Documentation/git.txt-codeGITCONFIGNOSYSTEMcode | ||
| "GIT_CONFIG_NOSYSTEM": "true", | ||
| "HOME": str(tmp_path), | ||
| # tox by default only passes the PATH environment variable, so | ||
| # XDG_CONFIG_HOME is already unset | ||
| # https://github.com/git/git/blob/cefe983a320c03d7843ac78e73bd513a27806845/t/test-lib.sh#L454-L461 | ||
| "GIT_AUTHOR_EMAIL": "author@example.com", | ||
| "GIT_AUTHOR_NAME": "A U Thor", | ||
| "GIT_AUTHOR_DATE": "1112354055 +0200", | ||
| "GIT_COMMITTER_EMAIL": "committer@example.com", | ||
| "GIT_COMMITTER_NAME": "committer", | ||
| "GIT_COMMITTER_DATE": "1112354055 +0200", | ||
| }, | ||
| ): | ||
| git_cmd(repository, "config --global init.defaultBranch main") | ||
| git_cmd(repository, ["init"]) | ||
| git_cmd(repository, "commit --allow-empty --allow-empty-message --no-edit") | ||
|
|
||
| yield repository | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def git(tmp_git_repo): | ||
| @wraps(git_cmd) | ||
| def wrapper(command): | ||
| return git_cmd(repository_path=tmp_git_repo, command=command) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmp_project(tmp_git_repo, git): | ||
| "return a path to the root of a git repository containing a sample package" | ||
| for file in (samples_dir / "module1_toml").glob("*"): | ||
| copy(str(file), str(tmp_git_repo / file.name)) | ||
| git("add -A :/") | ||
| git("commit --allow-empty --allow-empty-message --no-edit") | ||
|
|
||
| yield tmp_git_repo | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.