-
-
Notifications
You must be signed in to change notification settings - Fork 226
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?
Conversation
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.
Could you add a test case, please?
…h not already existing
2ad5958 to
c9c1031
Compare
done |
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.
Thanks for adding the test! 👍 I've left a few remarks.
| (src / ".copier-answers.yml"): ( | ||
| """\ | ||
| 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.
This setup is not representative of a real one.
| (src / ".copier-answers.yml"): ( | |
| """\ | |
| project_name: "test project" | |
| """ | |
| ), | |
| (src / "{{ _copier_conf.answers_file }}.jinja"): ( | |
| "{{ _copier_answers|to_nice_yaml }}" | |
| ), |
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?
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
copier.run_copy(..., defaults=True, ...)(as you're doing already) or instead pass the answer via the data argument:
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 copy call. Are you trying to pass answers to copier copy from a file instead of via interactive prompting or inline answers via the -d,--data flag? If so, perhaps --data-file is 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-file might 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_data might be worth checking out.
| git_save(src, tag="v1") | ||
|
|
||
| # Destination path that doesn't exist yet | ||
| dst_dir = src / "my" / "dest" / "path" |
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.
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 dst = src / "my" / "dest" / "path" instead.
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 mean dst_dir = dst / "my" / "dest" / "path" ? or I miss something
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, yes, I mistyped.
| ) | ||
|
|
||
| # Verify the project was created | ||
| assert (dst_dir / "README.txt").read_text() == "Project: 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.
I think you should also assert that the .copier-answers.yml file exists in the expected location.
Hello,
--answers-filemust be relative to the destination path, but when this one doesn't exist, the answers file cannot be found, example:will not found the
.copier-answers.ymlfilean other solution (or complementary) will be to search the file in the current dir.