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

Streamline the manual creation of Distribution objects with new fixtures #63

Merged
merged 3 commits into from
Aug 21, 2023

Conversation

diazona
Copy link
Owner

@diazona diazona commented Aug 21, 2023

This PR adds a new fixture, make_write_pyproject, which takes keyword arguments and passes them to the Distribution constructor, using that Distribution to create a WritePyproject instance. The fixture also ensures that the tests run in an empty directory, to prevent the setup.cfg and setup.py files in the project root directory from interfering with the test.

This streamlines a common workflow in our tests and makes it easier to write new tests that don't need a setup.cfg or setup.py file.

When testing WritePyproject, we need to give it a Distribution object,
which can be created either by calling setuptools.setup() or by manually
calling the constructor and passing in a dict of attributes. The latter
way is faster to run because it doesn't have to create and read a file,
but it's a little unwieldy to write that code over and over again. Plus,
we've had problems where the Distribution object will read
setuptools configuration files in whatever is the current working
directory when the test runs, and will set some of its attributes based
on that configuration. (Specifically, it seems to use setuptools_scm to
set the version number, but maybe this would apply to other dynamic
attributes, I'm not sure.)

This commit adds a fixture, make_write_pyproject, that will address
the problem in two ways:

- It ensures that any test requesting the fixture will run in an empty
  working directory
- It also provides a convenience function that creates a Distribution
  from the given keyword arguments and then creates a WritePyproject
  from that Distribution.

We can now use this fixture to avoid having to run the full setuptools
configuration procedure if we don't have to.
In this commit, I'm changing the make_write_pyproject fixture to set
a default name and version if those are not passed as keyword arguments.
Those are the only two attributes required by the Distribution
constructor, but we often don't care what their values are. If a test
does care, the values are accessible as class attributes DEFAULT_NAME
and DEFAULT_VERSION.

As a side effect of making the fixture return a class, I had to drop
typing information, because names in conftest.py aren't made available
to test code except through fixtures. It should be possible to bring
back the typing information by creating an extra package in the main
project for test helper code, or by using something like
pytest-helpers-namespace, but I don't think that's strictly necessary
to get this code merged.
@diazona diazona added this to the Initial release milestone Aug 21, 2023
@diazona diazona self-assigned this Aug 21, 2023
This commit changes all the tests that used to manually create
Distribution objects to use the new make_write_pyproject fixture.
@diazona diazona force-pushed the test-isolation/1/dev branch from 39aea22 to dab6605 Compare August 21, 2023 03:39
@diazona diazona requested a review from sjlongland August 21, 2023 03:41
@diazona diazona assigned sjlongland and unassigned diazona Aug 21, 2023
@diazona diazona enabled auto-merge August 21, 2023 03:42
@diazona diazona merged commit 85cb37a into main Aug 21, 2023
@diazona diazona deleted the test-isolation/1/dev branch August 21, 2023 03:59
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.

2 participants