copier-org / copier

Library and command-line utility for rendering projects templates.
https://readthedocs.org/projects/copier/
MIT License
2.06k stars 182 forks source link

fix: re-render answers file path when producing render context #1840

Closed sisp closed 3 weeks ago

sisp commented 3 weeks ago

I've fixed a bug related to rendering a template answers file name/path in combination with _message_before_copy.

The reason for this bug is as follows: When _message_before_copy is set, Copier renders this message before starting the questionnaire, and thus at this point the render context does not include any answers of questions, so rendering the templated _answers_file value leads to an incorrect result – but there's likely no practical reason to render the answers file in the pre-copy message. But rendering the pre-copy message requires the render context which contains the rendered answers file and prior to this PR the rendered answers file value was cached, hence the incorrect value was cached when the first render context was produced for rendering the pre-copy message.

This PR solves the problem by simply re-rendering the answers file path every time a render context is produced.

By the way, this bug would have been caught with less confusion if Copier had supported configuring Jinja with StrictUndefined. :nerd_face:

Fixes #1825.

codecov[bot] commented 3 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 97.72%. Comparing base (05aa175) to head (6cb92f9). Report is 1 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1840 +/- ## ======================================= Coverage 97.72% 97.72% ======================================= Files 49 49 Lines 5229 5238 +9 ======================================= + Hits 5110 5119 +9 Misses 119 119 ``` | [Flag](https://app.codecov.io/gh/copier-org/copier/pull/1840/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=copier-org) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/copier-org/copier/pull/1840/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=copier-org) | `97.72% <100.00%> (+<0.01%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=copier-org#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

pawamoy commented 3 weeks ago

Caching! Always tempting us, and stabbing us in the back once we succumbed.