Skip to content
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

(PUP-927) Read preserving line endings #3616

Conversation

ferventcoder
Copy link
Contributor

Previously, puppet's built in file and template functions would
read file contents using File.read. This would not cause problems
on *nix but could cause EOL conversion from \r\n to \n on Windows.

Add Puppet::FileSystem.read_preserve_line_endings for cases where line
endings are important and use File.readlines method, which is known to
preserve original line endings (from reading the source, read has defined
calls for Windows where readlines does not).

References:

Add Puppet::FileSystem.read_preserve_line_endings for cases where line
endings are important and use :mode => 'rb' checking first against utf-8
encoding, falling back to default_external when not valid, and falling back
to File.read when all else fails.

Supersedes #2397

@ferventcoder ferventcoder changed the title [DO NOT MERGE](PUP-927) Read preserving line endings [DO NOT MERGE](WIP)(PUP-927) Read preserving line endings Feb 17, 2015
@puppetcla
Copy link

CLA signed by all contributors.

@kylog kylog added the PL label Feb 18, 2015
@ferventcoder
Copy link
Contributor Author

Strangely enough in Ruby 1.9.3 and Ruby 2.1.5 I couldn't get File.read to return incorrect line endings. So this may not quite be sufficient (since I was unable to get a failure case in specs prior to changing the behavior).

In that case prefer the read_preserve_line_endings method to do something similar to https://tickets.puppetlabs.com/browse/PUP-927?focusedCommentId=130424&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-130424 with checking valid_encoding? - see https://github.com/ferventcoder/puppet/blob/99abd2c78ba3b697521c9bc4911e6fd8266d914c/lib/puppet/file_system/windows.rb#L51-L53

@ferventcoder ferventcoder changed the title [DO NOT MERGE](WIP)(PUP-927) Read preserving line endings [DO NOT MERGE](PUP-927) Read preserving line endings Feb 18, 2015
@ferventcoder
Copy link
Contributor Author

So I have a repro proving File.readlines is not sufficient. Will look at switching to binary.

@ferventcoder ferventcoder changed the title [DO NOT MERGE](PUP-927) Read preserving line endings (PUP-927) Read preserving line endings Feb 18, 2015
@@ -5,6 +5,14 @@
describe "Puppet::FileSystem" do
include PuppetSpec::Files

def with_file_content(content)
path = tmpfile('file-system')
file = File.new(path, 'w')
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the mode needs to be 'wb', otherwise ruby will strip '\r' while it's writing the test file that it tries to read back in later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Previously, puppet's built in `file` and `template` functions would
read file contents using `File.read`. This would not cause problems
on *nix but could cause EOL conversion from `\r\n` to `\n` on Windows.

Add `Puppet::FileSystem.read_preserve_line_endings` for cases where line
endings are important and use `:mode => 'rb'` checking first against utf-8
encoding, falling back to default_external when not valid, and falling back
to `File.read` when all else fails.
@ferventcoder
Copy link
Contributor Author

The question I have is should we just switch Puppet::FileSystem.read to use this method and not have a separate method? I wasn't trying to be too intrusive and wanted to catch the specific cases where this was occurring. Or maybe that should just wait until PUP-2564?

@ferventcoder
Copy link
Contributor Author

This is ready to go again.

@Iristyle
Copy link
Contributor

Test repo from @ferventcoder at https://github.com/ferventcoder/puppet-simple_templates (note it's missing .gitattributes so you're subject to your global Git settings)

@ferventcoder
Copy link
Contributor Author

@Iristyle I take pull requests! 👍 I updated this.

@Iristyle
Copy link
Contributor

@ferventcoder let's retarget this at the new 3.8 branch. Thanks!

@ferventcoder
Copy link
Contributor Author

Closing, will be retargeting soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants