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

add test using git #445

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft

Conversation

mawillcockson
Copy link

This adds a few pytest fixtures that set up a git repository using git.

I used this stackoverflow answer to isolate git using these environment variable:

  • GIT_CONFIG_GLOBAL
  • GIT_CONFIG_NOSYSTEM
  • HOME
  • GIT_AUTHOR_EMAIL
  • GIT_AUTHOR_NAME
  • GIT_AUTHOR_DATE
  • GIT_COMMITTER_EMAIL
  • GIT_COMMITTER_NAME
  • GIT_COMMITTER_DATE

Instead of using GIT_DIR and GIT_WORK_TREE, I added another fixture that forms a closure around git, and calls it's -C command-line option, pointing to the temporary repository.

I'm not familiar with testing git, so I'm not sure if these measures are sufficient.

The tmp_git_repo fixture initializes the temporary repository with an empty commit. This isn't strictly necessary, it just makes some of the possible tests require less setup.

The tmp_project fixture copies the sample module from ./tests/samples/module1_toml to the repository and commits the files.

A test is added for #345 as an example of how to use the new fixtures.

I wanted to be able to run tox and pytest targeting or excluding only the tests that depend on git being installed:

tox -- -m needgit

or skipped:

tox -- -m "not needgit"

I kept getting confused myself, so both needgit and needsgit work.

Currently this is selected based on the test function name including needgit or needsgit, and I can understand if that's a bit too magical.

The tests are skipped if shutil.which("git") returns nothing, though I did not test this.


Possible changes

Would it make more sense to have tmp_project behave similarly to the existing copy_sample fixture?

It could take as an argument the sample module and return a contextmanager-wrapped iterator that copies the named sample module files into the git repository and commits the changes:

def test_example(copy_to_repo):
    with copy_to_repo("module1_toml") as repo:
        ...

git is isolated using the following environment, which are set globally
for the duration of that the new pytest fixture "tmp_git" is used:

GIT_CONFIG_GLOBAL
GIT_CONFIG_NOSYSTEM
HOME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_AUTHOR_DATE
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_COMMITTER_DATE

GIT_DIR and GIT_WORK_TREE could be set so that git can be called using
any method, but instead a git() function is added that uses git's -C
command-line option.

These were taken from one of git's test scripts:
https://github.com/git/git/blob/cefe983a320c03d7843ac78e73bd513a27806845/t/test-lib.sh#L454-L461

There are probably other ways git can be isolated.

The repository is initialized with an empty commit, but this isn't
strictly necessary, it just makes some of the possible tests require
less setup.

The new tmp_project fixture copies the sample module from
./test/samples/module1_toml to the project and commits the files.

A test is added for pypa#345 as an example of how this can be used.

A pytest marker is added so that tests with either "needgit" or
"needsgit" in the name are skipped if python can't find an executable
named "git".

The tox configuration is changed and another pytest marker is added so
that those tests can be run by themselves:

tox -- -m needgit

or skipped:

tox -- -m "not needgit"

Type hints were added to help with development, but aren't necessary to
keep.
@takluyver takluyver linked an issue Sep 29, 2021 that may be closed by this pull request
@takluyver
Copy link
Member

Thanks! This looks like a good start. I don't know exactly what's needed to isolate git either, so we may as well try it and fix things as they come up. 😄

The thing with the test names is indeed a bit more magic than I like. Let's just use an explicit @pytest.mark.needsgit marker. I'd also prefer to have just the one name - I can see the potential for mistakes, but it feels kind of messy to solve that by having both. We can use pytest's --strict-markers option to check that the markers in the code are all correct.

I think having the fixtures automatically copy one working sample is a decent starting point for now. But I might try to separate the layer for calling git (including all those environment variables to make it consistent) from the layer that prepares a specific repository. We could always refactor that later when we want more flexibility, though.

Lastly, it looks like the tests are failing on Windows (Appveyor CI). If needs be, we can skip it on Windows and only have the new tests on Linux for now, but if possible it would be good to figure it out.

tests/conftest.py Outdated Show resolved Hide resolved
@mawillcockson
Copy link
Author

mawillcockson commented Sep 30, 2021

Thanks for being welcoming to contributions, and for giving very friendly feedback.

Let's just use an explicit @pytest.mark.needsgit marker
I'd also prefer to have just the one name

✔️ 7430aee

I might try to separate the layer for calling git (including all those environment variables to make it consistent) from the layer that prepares a specific repository.

Currently, the tmp_git_repo fixture sets and removes environment variables, and makes a temporary repository. I can separate those two if you'd prefer.

The tmp_project fixture requests tmp_git_repo and copies the sample files into the repository.

Is this separation of concerns sufficient? What did you have in mind?


Lastly, it looks like the tests are failing on Windows (Appveyor CI). If needs be, we can skip it on Windows and only have the new tests on Linux for now, but if possible it would be good to figure it out.

It looks like the tests are working already! 😁

Summary of the bug: git returns file paths separated by / regardless of the platform.

In the test this pr adds (starting from this line):

I would be happy to open an issue and PR for a quick fix. ✔️ #447

My personal preference with Python is to pathlib-ify everything dealing with file paths, and I'd be happy to open another PR to do this.

mawillcockson added a commit to mawillcockson/flit that referenced this pull request Sep 30, 2021
mawillcockson added a commit to mawillcockson/flit that referenced this pull request Sep 30, 2021
this is a focused fix for the problem referenced in:

pypa#445 (comment)
mawillcockson added a commit to mawillcockson/flit that referenced this pull request Dec 4, 2021
this is a focused fix for the problem referenced in:

pypa#445 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test actually using git
2 participants