Gracefully handle files with mixed lined endings#1942
Merged
ericof merged 2 commits intocookiecutter:mainfrom Sep 28, 2023
Merged
Gracefully handle files with mixed lined endings#1942ericof merged 2 commits intocookiecutter:mainfrom
ericof merged 2 commits intocookiecutter:mainfrom
Conversation
Contributor
Author
|
Thank you very much for landing this 🙌 |
david-abn
pushed a commit
to david-abn/cookiecutter
that referenced
this pull request
Oct 4, 2023
Co-authored-by: Érico Andrei <ericof@gmail.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
According to Python docs,
newlinescan be one of:NoneMeanwhile,
openbuiltin takesNoneor a string representing the new line according to Python docs. Hence, there is a mismatch between the two for the case of multiple line endings.cookiecutterwill get something likenewlines=("\n", "\r\n")and try to pass that toopenfunction. In turn, this will throw an error that looks like this:This PR proposes that
cookiecuttertakes the first detected new line when it encounters template files with mixed line endings. Whilst this means that original line endings aren't completed preserved, this seems like a better user experience than throwing aTypeError. It should also makecookiecuttermore resilient against (arguably) malformed inputs.Fixes #1737.
Testing
test_generate_file_handles_mixed_line_endingsto reproduce the issue, it only passes with the proposed patch ✅Notes
test_generate_file_handles_mixed_line_endingstest asserts that either CRLF or LF newlines are returned, as Python docs seem to make no guarantees on what order line endings are returned in case of file having mixed newlines.