From 9505af3dccd1170330eb8270037556b4ba29c6ce Mon Sep 17 00:00:00 2001 From: Johan Holmberg Date: Wed, 16 Nov 2016 22:34:00 +0100 Subject: [PATCH] improve line ending detection + add tests should hopefully work on Linux and Windows now --- lib/cmdtest/util.rb | 23 +++++++++----- t/CMDTEST_crnl.rb | 75 +++++++++++++++++++++++++++++++++++++++++++++ t/bin/echo_crnl.rb | 12 ++++++++ 3 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 t/CMDTEST_crnl.rb create mode 100755 t/bin/echo_crnl.rb diff --git a/lib/cmdtest/util.rb b/lib/cmdtest/util.rb index d5a2b2b..f91af9b 100644 --- a/lib/cmdtest/util.rb +++ b/lib/cmdtest/util.rb @@ -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 diff --git a/t/CMDTEST_crnl.rb b/t/CMDTEST_crnl.rb new file mode 100644 index 0000000..943b526 --- /dev/null +++ b/t/CMDTEST_crnl.rb @@ -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 diff --git a/t/bin/echo_crnl.rb b/t/bin/echo_crnl.rb new file mode 100755 index 0000000..fa95442 --- /dev/null +++ b/t/bin/echo_crnl.rb @@ -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