-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
feat: Add _copier_conf.operation
variable
#1733
base: master
Are you sure you want to change the base?
Changes from all commits
250820f
f010d84
e0cac30
80e9227
9512a46
cb5ed75
cf4934a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import pytest | ||
from plumbum import local | ||
|
||
import copier | ||
|
||
from .helpers import build_file_tree, git_save | ||
|
||
|
||
@pytest.mark.parametrize("operation", ("recopy", "update")) | ||
def test_operation_in_context_matches( | ||
operation: str, tmp_path_factory: pytest.TempPathFactory | ||
) -> None: | ||
""" | ||
Ensure that the _copier_conf.operation context variable is set | ||
as expected during template rendering. | ||
""" | ||
src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) | ||
with local.cwd(src): | ||
build_file_tree( | ||
{ | ||
# Ensure the file is regenerated on update. | ||
"copier.yml": "_skip_if_exists: [foo]", | ||
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}", | ||
"foo.jinja": "{{ _copier_conf.operation }}", | ||
} | ||
) | ||
git_save(tag="1.0.0") | ||
|
||
copier.run_copy(str(src), dst, defaults=True, overwrite=True) | ||
ctx_file = dst / "foo" | ||
assert ctx_file.read_text() == "copy" | ||
# Ensure the file is regenerated on update. | ||
# If we left it, an update would detect custom changes | ||
# that would be reapplied, i.e. an update would leave us with `copy`. | ||
ctx_file.unlink() | ||
with local.cwd(dst): | ||
git_save() | ||
getattr(copier, f"run_{operation}")(str(dst), overwrite=True) | ||
expected = "copy" if operation == "recopy" else operation | ||
assert ctx_file.read_text() == expected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: So, let's say that we copy one template and render the value. The file will contain Now we run it again. The file contains However, what would happen if we run it yet again? The replay will render Triple-thinking this... are we introducing here a way to make replays non-reproducible and thus maybe making the update algorithm unreliable? Because there's no way Copier could know if last play was a copy or an update. CC @copier-org/maintainers. Should we just reject this feature to avoid shooting our own feet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disregarding that
What would happen without it being deleted is the following:
Why (3):
Edit: I emphasized that |
||
|
||
|
||
def test_exclude_templating_with_operation(tmp_path_factory: pytest.TempPathFactory) -> None: | ||
""" | ||
Ensure it's possible to create one-off boilerplate files | ||
that are not managed during updates. | ||
""" | ||
src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) | ||
exclude = r"{%- if _copier_conf.operation == 'update' %}dumb_boilerplate{%- endif %}" | ||
with local.cwd(src): | ||
build_file_tree( | ||
{ | ||
"copier.yml": f"_exclude:\n - \"{exclude}\"", | ||
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}", | ||
"dumb_boilerplate": "foo", | ||
"other_file": "foo", | ||
} | ||
) | ||
git_save(tag="1.0.0") | ||
build_file_tree( | ||
{ | ||
"dumb_boilerplate": "bar", | ||
"other_file": "bar", | ||
} | ||
) | ||
git_save(tag="2.0.0") | ||
copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0") | ||
boilerplate = dst / "dumb_boilerplate" | ||
other_file = dst / "other_file" | ||
for file in (boilerplate, other_file): | ||
assert file.exists() | ||
assert file.read_text() == "foo" | ||
with local.cwd(dst): | ||
git_save() | ||
copier.run_update(str(dst), overwrite=True) | ||
assert boilerplate.read_text() == "foo" | ||
assert other_file.read_text() == "bar" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass ˋmodeˋ if it is in ˋself.operationˋ, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I did it like that, but noticed that this would cause a behavior change (at least in theory):
During
_apply_update()
,self.operation
isupdate
, but it calls onrun_copy()
several times, which would passcopy
to_check_unsafe()
before this patch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. However, that is correct. You will notice that there are calls to
replace
. In those calls, you can replace some configuration for the sub-worker that is created. Could you please try doing it that way?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure I'm following. First, let me sum up:
_copier_conf.operation
means we have an attribute on the worker representing the current high-level (user-requested) operation._check_unsafe
instead of the parameter._check_unsafe
behaves during the individual copy operations that run during an update, where the high-level operation isupdate
, but the low-level one iscopy
, advocating for keeping the parameter.I'm already using
replace
for overriding the operation duringupdate
. Are you saying the high-level operation during the individual copy operations should becopy
? Because that would mean_copier_conf.operation
is alwayscopy
during template rendering, i.e. defeat the purpose of this feature.