-
-
Notifications
You must be signed in to change notification settings - Fork 254
fix: resolve path between dst_path and answers_file to handle dst_path not already existing #2352
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,3 +333,49 @@ def test_undefined_phase_in_external_data( | |
| copier.run_copy(str(src), dst, defaults=True, overwrite=True) | ||
| answers = load_answersfile_data(dst, ".copier-answers.yml") | ||
| assert answers["key"] == "value" | ||
|
|
||
|
|
||
|
|
||
| def test_answersfile_relative_path_nonexistent_dst( | ||
| tmp_path_factory: pytest.TempPathFactory, | ||
| ) -> None: | ||
| """Test that relative answers_file paths work when dst_path doesn't exist yet.""" | ||
| src = tmp_path_factory.mktemp("src") | ||
| # Create a simple template | ||
| build_file_tree( | ||
| { | ||
| (src / "copier.yml"): ( | ||
| """\ | ||
| project_name: | ||
| type: str | ||
| """ | ||
| ), | ||
| (src / ".copier-answers.yml"): ( | ||
| """\ | ||
| project_name: "test project" | ||
| """ | ||
| ), | ||
| (src / "README.txt.jinja"): ( | ||
| "Project: {{ project_name }}" | ||
| ), | ||
| } | ||
| ) | ||
| git_save(src, tag="v1") | ||
|
|
||
| # Destination path that doesn't exist yet | ||
| dst_dir = src / "my" / "dest" / "path" | ||
|
Member
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. The result might be same, but this setup is a bit unintuitive because you're generating the project in a subdirectory of the template's sources. I'd follow the usual setup with src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))and generate in
Author
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. you mean
Member
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. Sorry, yes, I mistyped. |
||
|
|
||
| # Answers file in parent directory (relative path of dst_dir) | ||
| answers_file = "../../../.copier-answers.yml" | ||
|
|
||
| # This should work even though dst_dir doesn't exist yet | ||
| copier.run_copy( | ||
| str(src), | ||
| dst_dir, | ||
| answers_file=answers_file, | ||
| defaults=True, | ||
| overwrite=True, | ||
| ) | ||
|
|
||
| # Verify the project was created | ||
| assert (dst_dir / "README.txt").read_text() == "Project: test project" | ||
|
Member
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. I think you should also assert that the |
||
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.
This setup is not representative of a real one.
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.
where is defined the
_copier_answers?Uh oh!
There was an error while loading. Please reload this page.
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.
This is a Jinja variable available in our render context.
https://copier.readthedocs.io/en/stable/creating/#_copier_answers
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 but where do I define that
project_name: "test project"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.
You can either set it as the default answer in
copier.yml(src / "copier.yml"): ( """\ project_name: type: str + default: test project """ ),and run
(as you're doing already) or instead pass the answer via the
dataargument:copier.run_copy(..., data={"project_name": "test project"}, ...)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.
Ok but I don't test my case where I want to be dure that the existing answers file is loaded, no?
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.
Does your use case assume an existing answers file? If so, why? This seems like an unusual pattern. The answers file is generated by Copier to record previous answers plus metadata to enable updating to new template versions. It looks like you're assuming the answers file to exist before the first
copier copycall. Are you trying to pass answers tocopier copyfrom a file instead of via interactive prompting or inline answers via the-d,--dataflag? If so, perhaps--data-fileis what you're looking for.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.
one of our use case is to have a template for the repository, then we can add terraform stack in subdirs, and we call the initial answer file to not repeat common questions
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.
I think
--data-filemight work for this use case as well although you'd be passing an answers file, which is a superset of a typical data file.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.
Or
_external_datamight be worth checking out.