Convert Line Endings of Mac and Windows to Unix in Rails and Test with RSpec
Line endings or newline is a special character(s) to define the end of a line. The line endings special character(s) vary across the operating systems. Let’s take an example, we are developing a Rails feedback application which will be used by a wide range of users. The users might submit the feedback from different operating systems which has different kind of line end character(s). The content should have formatted for standard line endings before storing into backend.
Mostly two special characters used to define the line endings in most of the operating systems.
-
Line Feed (LF) - \n
-
Carriage Return (CR) - \r
The usage of these two special characters for Unix, Mac and Windows are
OS | Characters | Name |
Unix | \n | LF |
Mac | \r | CR |
Windows | \r\n | CRLF |
Note:- \r is the newline character up to Mac OS version 9, after that Mac uses Unix line endings.
It is a developer’s job to convert all kinds of line endings to Unix line ending format to maintain the standard. We can achieve this by a regex pattern replace. The regex pattern should convert \r(Mac) or \r\n(Windows) to \n(Unix).
standard_content = non_standard_content.gsub(/\r\n?/,"\n")
We can see the conversion of line endings to Unix from Mac and Windows using irb (Interactive Ruby) shell.
1. Mac
irb(main):001:0> mac ="Hello \r Mac"
=> "Hello \r Mac"
irb(main):002:0> mac.gsub(/\r\n?/,"\n")
=> "Hello \n Mac"
2. Windows
irb(main):001:0> windows="Hello \r\n Windows"
=> "Hello \r\n Windows"
irb(main):002:0> windows.gsub(/\r\n?/,"\n")
=> "Hello \n Windows"
RSpec Tests
After the implementation of line endings conversion, it should covered with test cases for the best practices of development. Here is the bunch of Rspec code to test both Mac and Windows line endings conversion.
describe "POST feedback requests" do
it "validates Mac line endings converted to Unix" do
_params = { :content => "Hello \r Mac", :user => "myuser"}
post '/feedback.json', _params
response.status.should == 200
result = JSON.parse(response.body)
result['content'].should_not include("\r")
end
it "validates Windows line endings converted to Unix" do
_params = { :content => "Hello \r\n Windows", :user => "myuser"}
post '/feedback.json', _params
response.status.should == 200
result = JSON.parse(response.body)
result['content'].should_not include("\r\n")
end
end
The line endings conversion plays a crucial role in standardising the content. It is recommended to convert line endings to Unix style when providing web service features.
Comments