Skip to content

Add Jinja2 templating - #351

Merged
colinleach merged 8 commits into
exercism:mainfrom
colinleach:add-templating
Oct 6, 2026
Merged

colinleach merged 8 commits into
exercism:mainfrom
colinleach:add-templating

Conversation

@colinleach

Copy link
Copy Markdown
Contributor

I think this is basically working. I've updated a couple of exercises with templates to test it.

No doubt there will be further refinements to the Python scripts as we get more experience.

Collatz Conjecture is cheating a bit, for backwards compatibility. The old version had no tests for invalid input (<1), so I set include = false for the two tests expecting errors. We can update it later, but be sure to avoid retesting: old solutions don't just fail benignly, they get in an infinite loop and time out.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

This PR touches files which potentially affect the outcome of the tests of an exercise. This will cause all students' solutions to affected exercises to be re-tested.

If this PR does not affect the result of the test (or, for example, adds an edge case that is not worth rerunning all tests for), please add the following to the merge-commit message which will stops student's tests from re-running. Please copy-paste to avoid typos.

[no important files changed]

For more information, refer to the documentation. If you are unsure whether to add the message or not, please ping @exercism/maintainers-admin in a comment. Thank you!

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Hello 👋 Thanks for your PR.

This repo does not currently have dedicated maintainers. Our cross-track maintainers team will attempt to review and merge your PR, but it will likely take longer for your PR to be reviewed.

If you enjoy contributing to Exercism and have a track-record of doing so successfully, you might like to become an Exercism maintainer for this track.

Please feel free to ask any questions, or chat to us about anything to do with this PR or the reviewing process on the Exercism forum.

(cc @exercism/cross-track-maintainers)

@colinleach

colinleach commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Use update-new-exercise-files.py to add a template.j2 stub.

Once the template is filled in, run generate_tests.py to convert the template to a tests.scm.

I wondered about adding a linting/formatting step, but didn't like the options. guile-schemefmt is a pain to install (git clone, then build manually from source), and the results of running it are remarkably ugly. Maybe it just has good Emacs integration.

Comment thread bin/generate_tests.py
@BNAndras

BNAndras commented Oct 4, 2026

Copy link
Copy Markdown
Member

https://github.com/raviqqe/schemat looks a bit promising for a formatter, but I haven't tested it yet. It seems to be opinionated though since I don't see configuration info.

@colinleach

Copy link
Copy Markdown
Contributor Author

I noticed schemat, but haven't tried it yet. When we're already juggling Scheme and Python (plus Makefiles and shell scripts), adding Rust to the mix is not very appealing.

I'm pretty relaxed about leaving this out. Scheme isn't fussy about formatting, I don't think there is anything equivalent to PEP 8, and a direct translation of the j2 template gives a decent, human-readable test file.

@colinleach

Copy link
Copy Markdown
Contributor Author

Is anything blocking approval of this, other than @exercism/cross-track-maintainers being very busy people? It's messing up some other stuff I want to PR.

@glennj

glennj commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Use update-new-exercise-files.py to add a template.j2 stub.

Once the template is filled in, run generate_tests.py to convert the template to a tests.scm.

This is the kind of knowledge that belongs in a CONTRIBUTING.md doc. Hint hint.

Comment thread exercises/practice/collatz-conjecture/.meta/tests.toml
Co-authored-by: Glenn Jackman <glenn.jackman@gmail.com>

Prerequisites
- Python 3.x (it may work with Python 2.x, but that is untested)
- Up-to-date local version of the https://github.com/exercism/problem-specifications.git repo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note that configlet caches the repo in $XDG_CACHE_DIR/exercism/configlet/proglem-specifications

XDG_CACHE_DIR is by default $HOME/.cache

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In my (partial) defence, @IsaacG wrote that bit. Actually, he wrote most of the Python...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't recall writing the update-new-exercise-files.py file. Just the generate_tests.py. That file has a problem_spec_dir() which does mimic the configlet behavior IIUC. It may be possible to reuse some logic across the Python files.

@glennj glennj left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't really review the python scripts, not my area of expertise.

Comment on lines +92 to +94
def template_stub() -> str:
return textwrap.dedent("""\
{{ header }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In some of my repos, like exercism/moonscript, I store the generator template in /exercises/shared/templates -- that shared directory is underused IMO

Comment thread exercises/practice/collatz-conjecture/.meta/tests.toml Outdated
Co-authored-by: Glenn Jackman <glenn.jackman@gmail.com>
@colinleach

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I'll merge for now, and add a CONTRIBUTING.md later today.

I'll probably move the template-stub code to a separate file, both here and on the (closely similar) Fortran track.

@colinleach
colinleach merged commit dd2f6da into exercism:main Oct 6, 2026
7 checks passed
@IsaacG

IsaacG commented Oct 6, 2026

Copy link
Copy Markdown
Member

Is anything blocking approval of this, other than @exercism/cross-track-maintainers being very busy people? It's messing up some other stuff I want to PR.

Apologies. I missed that this was blocked on cross-track-maintainers and assumed there were other Scheme maintainers.

@IsaacG

IsaacG commented Oct 6, 2026

Copy link
Copy Markdown
Member

I didn't really review the python scripts, not my area of expertise.

bin/generate_tests.py is a near-copy/paste from other tracks.

@colinleach

Copy link
Copy Markdown
Contributor Author

I missed that this was blocked on cross-track-maintainers and assumed there were other Scheme maintainers

The track was unmaintained for a long time. I became sole maintainer about a week ago - hesitantly, but I'd rather not see it die through neglect.

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.

4 participants