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

Make templates testable #60

Open
OverkillGuy opened this issue Jan 17, 2023 · 0 comments
Open

Make templates testable #60

OverkillGuy opened this issue Jan 17, 2023 · 0 comments
Labels
needs triage Indicates an issue or PR requires triage

Comments

@OverkillGuy
Copy link

OverkillGuy commented Jan 17, 2023

The biggest "suck" of templates as they are in cookiecutter is there's no incentive structure to ensure the templates expand to something that's in working order.
It's super easy to accidentally commit something that fails some sort of linting, making all your new code projects fail linters right away, or fail to build, etc.

I always wished I could write tests relating to my cookiecutter templates, that would say something like "if I expand the template with the following variables, then I should be able to run make and see a JAR/ZIP/DEB/WHL file in dist/".

I implemented something like that in here, see https://github.com/OverkillGuy/python-template/tree/main/tests
Specifically, it has a template() function that expands the template in a tmpdir, and tests like this that use the templated repo as fixture, and assess that if we run command X, we get output Y in file.
This is built to allow running the command in a docker container based on a Dockerfile in my template, a nice touch to allow self-contained tests (such as trying to run templates + tests for python versions I haven't got locally), but that's a bit overkill for some.

I wish cookieninja adopted some of that testability, moving some of the infrastructure I built in my tests folder into the core lib, facilitating testability of all cookiecutter templates.

Specifically, I recommend:

  • Adopt some "render the template in tempdir" fixture that's easily importable (from cookieninja.tests import template_expand_tmpdir?)
  • Adopt some datastructure like my Template to bind an tmp-expanded-template together into a path + context that spawned it, simplifying tests
  • Help (somehow? not sure how) to matrix-select template expansions (currently done by pytest.mark.parametrize, but I wish I could do specific stuff like "run this for all variants of python version, but only for run_in_dev = True cases".
  • Package the necessary dev-tools to enable such tests (I use Faker lib to generate fake project names, which is funny to me). Running test commands via docker is a big one, for instance.
  • (Stretch) Provide something quick and dirty like my make try for quickly expanding the template + run some cmd, which I find super useful when debugging or expanding the template.
  • Encourage others to maintain tests, by adding this to docs and adding tests to well known templates as example
@yuvaldolev yuvaldolev added the needs triage Indicates an issue or PR requires triage label Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Indicates an issue or PR requires triage
Projects
None yet
Development

No branches or pull requests

2 participants