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
(PUP-927) Read preserving line endings #3616
Conversation
CLA signed by all contributors. |
033169b
to
3b4f0ac
Compare
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 |
So I have a repro proving File.readlines is not sufficient. Will look at switching to binary. |
3b4f0ac
to
bcb062d
Compare
@@ -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') |
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 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.
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.
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.
bcb062d
to
52f447a
Compare
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? |
This is ready to go again. |
Test repo from @ferventcoder at https://github.com/ferventcoder/puppet-simple_templates |
|
@ferventcoder let's retarget this at the new |
Closing, will be retargeting soon. |
Previously, puppet's built in
file
andtemplate
functions wouldread file contents using
File.read
. This would not cause problemson *nix but could cause EOL conversion from
\r\n
to\n
on Windows.AddPuppet::FileSystem.read_preserve_line_endings
for cases where lineendings are important and use
File.readlines
method, which is known topreserve original line endings (from reading the source, read has defined
calls for Windows where readlines does not).
References:IO.Read - https://github.com/ruby/ruby/blob/v2_1_5/io.c#L9638IO.Readlines - https://github.com/ruby/ruby/blob/v2_1_5/io.c#L3324IO.Read Windows - https://github.com/ruby/ruby/blob/v2_1_5/io.c#L2819-L2828https://www.ruby-forum.com/topic/132198Add
Puppet::FileSystem.read_preserve_line_endings
for cases where lineendings are important and use
:mode => 'rb'
checking first against utf-8encoding, falling back to default_external when not valid, and falling back
to
File.read
when all else fails.Supersedes #2397