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

Update tests to use copier instead of cookiecutter #13

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions copier.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# Essential questions

directory_name:
type: str
default: my-python-project
help: Enter the name of the directory where the project will be created.
validator: >-
{% if not (directory_name | regex_search('^[a-z0-9\-]+$')) %}
directory_name must be lowercase, and can only contain letters, digits, and hyphens.
{% endif %}
package_name:
type: str
default: my_python_package
Expand Down Expand Up @@ -62,7 +54,7 @@ copyright_holder:
default: Netherlands eScience Center
code_of_conduct_email:
type: str
default: {{ email }}
default: "{{ email }}"

_subdirectory: template

Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ include_package_data = True
python_requires = >=3.8
packages =
install_requires =
cookiecutter==1.7.2
copier==9.2.0

[options.data_files]
# This section requires setuptools>=40.6.0
Expand All @@ -45,7 +45,8 @@ install_requires =
dev =
coverage [toml]
pytest
pytest-cookies
pytest-copie
pyprojroot
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with pyprojroot. I was originally thinking of replacing 'pytest-cookies' with 'pytest-copie'. See https://github.com/12rambau/pytest-copie

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyprojroot is a simple but very helpful package to easily refer to paths relative to the project root. For the tests specifically, I used it to guarantee that the source refers to the root of the project, regardless of where the tests are called from (which can sometimes cause issues with these things.)

Using pytest-copie would be nice, but it doesn't a session-scoped fixture such as the one that pytest-cookies does. That means we end up with a whole bunch of ScopeMismatch errors if we do a 'naive' replacement:

ERROR tests/test_project.py::test_pytest - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_coverage - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_tox - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_subpackage - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_generate_api_docs - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_coverage_api_docs - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_doctest_api_docs - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_ruff_check - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:
ERROR tests/test_project.py::test_bumpversion - Failed: ScopeMismatch: You tried to access the function scoped fixture copie with a session scoped request object. Requesting fixture stack:

Looking at the pytest-cookies code, it seems like it should be an easy contribution to add a similar session scoped version. We should look into that!



[tool:pytest]
Expand Down
24 changes: 13 additions & 11 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
from sys import platform
from typing import Sequence

from pyprojroot.here import here
from copier import run_copy
import pytest

IS_WINDOWS = platform.startswith('win')


def test_project_folder(cookies):
project = cookies.bake()
def test_project_folder(copie):
project = copie.copy()

assert project.exit_code == 0
assert project.exception is None
assert project.project_path.name == 'my-python-project'
assert project.project_path.is_dir()
assert project.project_dir.is_dir()


def run(args: Sequence[str], dirpath: os.PathLike) -> subprocess.CompletedProcess:
Expand All @@ -41,15 +42,16 @@ def project_env_bin_dir(tmp_path_factory):


@pytest.fixture(scope='session')
def baked_with_development_dependencies(cookies_session, project_env_bin_dir):
result = cookies_session.bake()
assert result.exit_code == 0
def baked_with_development_dependencies(tmp_path_factory, project_env_bin_dir):
project = run_copy(src_path=str(here()), dst_path=str(tmp_path_factory.mktemp('projects')), defaults=True)
project_dir = project.dst_path

bin_dir = project_env_bin_dir
latest_pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--upgrade', 'pip', 'setuptools'], result.project_path)
latest_pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--upgrade', 'pip', 'setuptools'], project_dir)
assert latest_pip_output.returncode == 0
pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--editable', '.[dev]'], result.project_path)
pip_output = run([f'{bin_dir}python', '-m', 'pip', 'install', '--editable', '.[dev]'], project_dir)
assert pip_output.returncode == 0
return result.project_path
return project_dir


def test_pytest(baked_with_development_dependencies, project_env_bin_dir):
Expand Down Expand Up @@ -167,7 +169,7 @@ def test_ruff_check(baked_with_development_dependencies, project_env_bin_dir):
project_dir = baked_with_development_dependencies
bin_dir = project_env_bin_dir

result = run([f'{bin_dir}ruff', '.'], project_dir)
result = run([f'{bin_dir}ruff', 'check', '.'], project_dir)
assert result.returncode == 0
assert '' in result.stdout

Expand Down
16 changes: 8 additions & 8 deletions tests/test_values.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
def test_double_quotes_in_name_and_description(cookies):
def test_double_quotes_in_name_and_description(copie):
ctx = {
"project_short_description": '"double quotes"',
"full_name": '"name"name'
}
project = cookies.bake(extra_context=ctx)
project = copie.copy(extra_answers=ctx)

assert project.exit_code == 0


def test_single_quotes_in_name_and_description(cookies):
def test_single_quotes_in_name_and_description(copie):
ctx = {
"project_short_description": "'single quotes'",
"full_name": "Mr. O'Keefe"
}
project = cookies.bake(extra_context=ctx)
project = copie.copy(extra_answers=ctx)

assert project.exit_code == 0


def test_dash_in_directory_name(cookies):
def test_dash_in_directory_name(copie):
ctx = {
"directory_name": "my-python-project"
}
project = cookies.bake(extra_context=ctx)
project = copie.copy(extra_answers=ctx)

assert project.exit_code == 0


def test_space_in_directory_name(cookies):
def test_space_in_directory_name(copie):
ctx = {
"directory_name": "my python project"
}
project = cookies.bake(extra_context=ctx)
project = copie.copy(extra_answers=ctx)

assert project.exit_code == 0
Loading