Skip to content

Gracefully handle files with mixed lined endings#1942

Merged
ericof merged 2 commits intocookiecutter:mainfrom
EricHripko:fix-mixed-line-endings
Sep 28, 2023
Merged

Gracefully handle files with mixed lined endings#1942
ericof merged 2 commits intocookiecutter:mainfrom
EricHripko:fix-mixed-line-endings

Conversation

@EricHripko
Copy link
Copy Markdown
Contributor

Overview

According to Python docs, newlines can be one of:

  • None
  • string (if line endings are consistent)
  • Tuple of strings (if multiple line endings are used)

Meanwhile, open builtin takes None or 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. cookiecutter will get something like newlines=("\n", "\r\n") and try to pass that to open function. In turn, this will throw an error that looks like this:

TypeError: open() argument 'newline' must be str or None, not tuple

This PR proposes that cookiecutter takes 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 a TypeError. It should also make cookiecutter more resilient against (arguably) malformed inputs.

Fixes #1737.

Testing

  • Added a test test_generate_file_handles_mixed_line_endings to reproduce the issue, it only passes with the proposed patch ✅
  • Existing tests pass locally too ✅
  • Confirmed that proposed changes are covered via the coverage report ✅

Notes

  • New test_generate_file_handles_mixed_line_endings test 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.

@ericof ericof self-requested a review September 28, 2023 17:24
@ericof ericof added this to the 2.4.0 milestone Sep 28, 2023
@ericof ericof added the enhancement This issue/PR relates to a feature request. label Sep 28, 2023
Copy link
Copy Markdown
Member

@ericof ericof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ericof ericof merged commit 539f341 into cookiecutter:main Sep 28, 2023
@EricHripko EricHripko deleted the fix-mixed-line-endings branch September 29, 2023 11:06
@EricHripko
Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This issue/PR relates to a feature request.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error due to multiple newline symbols in one file

2 participants