improve line ending detection + add tests

should hopefully work on Linux and Windows now
This commit is contained in:
Johan Holmberg 2016-11-16 22:34:00 +01:00
parent c3ee66e5c5
commit 9505af3dcc
3 changed files with 102 additions and 8 deletions

@ -92,19 +92,26 @@ module Cmdtest
raise AssertFailed, "ERROR: unexpected encoding: #{@name} not '#{encoding}'"
end
str = extern_text.encode('utf-8')
n_nl = str.scan("\n").size
n_crnl = str.scan("\r\n").size
n_windows = n_crnl
n_unix = n_nl - n_crnl
if Util.windows?
has_nl = str.gsub("\n", "") != str
has_crnl = str.gsub("\r\n", "") != str
if has_nl && ! has_crnl
if n_unix > 0 && n_windows == 0
raise AssertFailed, "ERROR: UNIX line ending: #{@name}"
elsif str.gsub("\r", "").gsub("\n", "") == str.gsub("\r\n", "")
return str.gsub("\r\n", "\n")
else
elsif n_unix > 0 && n_windows > 0
raise AssertFailed, "ERROR: mixed line ending: #{@name}"
else
return str.gsub("\r\n", "\n")
end
else
return str
if n_unix == 0 && n_windows > 0
raise AssertFailed, "ERROR: Windows line ending: #{@name}"
elsif n_unix > 0 && n_windows > 0
raise AssertFailed, "ERROR: mixed line ending: #{@name}"
else
return str
end
end
end
end

75
t/CMDTEST_crnl.rb Normal file

@ -0,0 +1,75 @@
# coding: utf-8
require "selftest_utils"
class CMDTEST_crnl < Cmdtest::Testcase
include SelftestUtils
def test_crnl
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:rn 2:rn" do',
' comment "windows line endings"',
' stdout_equal "1\n2\n"',
'end',
]
if Cmdtest::Util.windows?
cmd_cmdtest do
stdout_equal [
"### windows line endings",
]
end
else
cmd_cmdtest do
stdout_equal [
"### windows line endings",
"--- ERROR: Windows line ending: STDOUT",
]
exit_nonzero
end
end
end
def test_nl
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:n" do',
' comment "unix line endings"',
' stdout_equal "1\n2\n"',
'end',
]
if Cmdtest::Util.windows?
cmd_cmdtest do
stdout_equal [
"### unix line endings",
"--- ERROR: UNIX line ending: STDOUT",
]
exit_nonzero
end
else
cmd_cmdtest do
stdout_equal [
"### unix line endings",
]
end
end
end
def test_crnl_and_nl
create_CMDTEST_foo [
'cmd "echo_crnl.rb 1:n 2:rn" do',
' comment "mixed line endings"',
'end',
]
cmd_cmdtest do
stdout_equal [
"### mixed line endings",
"--- ERROR: mixed line ending: STDOUT",
]
exit_nonzero
end
end
end

12
t/bin/echo_crnl.rb Executable file

@ -0,0 +1,12 @@
#!/usr/bin/ruby
STDOUT.binmode
for arg in ARGV
arg = arg.dup
if arg.sub!(/:rn$/, "")
print arg + "\r\n"
elsif arg.sub!(/:n$/, "")
print arg + "\n"
end
end