cookieninja-generator / cookieninja

A cross-platform command-line utility that creates projects from cookiecutters (project templates), e.g. Python package projects, C projects - a cookiecutter fork
BSD 3-Clause "New" or "Revised" License
70 stars 7 forks source link

Make templates testable #60

Open OverkillGuy opened 1 year ago

OverkillGuy commented 1 year ago

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: