-
-
Notifications
You must be signed in to change notification settings - Fork 182
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 _operation
variable
#1733
Open
lkubb
wants to merge
1
commit into
copier-org:master
Choose a base branch
from
lkubb:feat/operation-var
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import pytest | ||
from plumbum import local | ||
|
||
import copier | ||
|
||
from .helpers import build_file_tree, git_save | ||
|
||
|
||
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 via `_exclude` using the `_operation` context variable. | ||
""" | ||
src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) | ||
|
||
template = "{% if _operation == 'update' %}copy-only{% endif %}" | ||
with local.cwd(src): | ||
build_file_tree( | ||
{ | ||
"copier.yml": f'_exclude:\n - "{template}"', | ||
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}", | ||
"copy-only": "foo", | ||
"copy-and-update": "foo", | ||
} | ||
) | ||
git_save(tag="1.0.0") | ||
build_file_tree( | ||
{ | ||
"copy-only": "bar", | ||
"copy-and-update": "bar", | ||
} | ||
) | ||
git_save(tag="2.0.0") | ||
copy_only = dst / "copy-only" | ||
copy_and_update = dst / "copy-and-update" | ||
|
||
copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0") | ||
for file in (copy_only, copy_and_update): | ||
assert file.exists() | ||
assert file.read_text() == "foo" | ||
|
||
with local.cwd(dst): | ||
git_save() | ||
|
||
copier.run_update(str(dst), overwrite=True) | ||
assert copy_only.read_text() == "foo" | ||
assert copy_and_update.read_text() == "bar" | ||
|
||
|
||
def test_task_templating_with_operation( | ||
tmp_path_factory: pytest.TempPathFactory, tmp_path: Path | ||
) -> None: | ||
""" | ||
Ensure that it is possible to define tasks that are only executed when copying. | ||
""" | ||
src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) | ||
# Use a file outside the Copier working directories to ensure accurate tracking | ||
task_counter = tmp_path / "task_calls.txt" | ||
with local.cwd(src): | ||
build_file_tree( | ||
{ | ||
"copier.yml": ( | ||
f"""\ | ||
_tasks: | ||
- command: echo {{{{ _operation }}}} >> {json.dumps(str(task_counter))} | ||
when: "{{{{ _operation == 'copy' }}}}" | ||
""" | ||
), | ||
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}", | ||
} | ||
) | ||
git_save(tag="1.0.0") | ||
|
||
copier.run_copy(str(src), dst, defaults=True, overwrite=True, unsafe=True) | ||
assert task_counter.exists() | ||
assert len(task_counter.read_text().splitlines()) == 1 | ||
|
||
with local.cwd(dst): | ||
git_save() | ||
|
||
copier.run_recopy(dst, defaults=True, overwrite=True, unsafe=True) | ||
assert len(task_counter.read_text().splitlines()) == 2 | ||
|
||
copier.run_update(dst, defaults=True, overwrite=True, unsafe=True) | ||
assert len(task_counter.read_text().splitlines()) == 2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.