fix: resolve path between dst_path and answers_file to handle dst_path not already existing#2352
fix: resolve path between dst_path and answers_file to handle dst_path not already existing#2352Aohzan wants to merge 1 commit intocopier-org:masterfrom
Conversation
sisp
left a comment
There was a problem hiding this comment.
Could you add a test case, please?
…h not already existing
2ad5958 to
c9c1031
Compare
done |
sisp
left a comment
There was a problem hiding this comment.
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.
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.
where is defined the _copier_answers?
There was a problem hiding this comment.
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.
yes but where do I define that project_name: "test project"
There was a problem hiding this comment.
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.
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.
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.
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.
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.
| 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.
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.
you mean dst_dir = dst / "my" / "dest" / "path" ? or I miss something
| ) | ||
|
|
||
| # Verify the project was created | ||
| assert (dst_dir / "README.txt").read_text() == "Project: test project" |
There was a problem hiding this comment.
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.