diff --git a/features/fixtures/quoted-cr-linebrks-crlf-line-endings.csv b/features/fixtures/quoted-cr-linebrks-crlf-line-endings.csv new file mode 100644 index 00000000..f9925c9b --- /dev/null +++ b/features/fixtures/quoted-cr-linebrks-crlf-line-endings.csv @@ -0,0 +1,3 @@ +Short,Multi,Bool +a,"b c",0 +d,"e f",1 diff --git a/features/fixtures/quoted-crlf-linebrks-crlf-line-endings.csv b/features/fixtures/quoted-crlf-linebrks-crlf-line-endings.csv new file mode 100644 index 00000000..4974a763 --- /dev/null +++ b/features/fixtures/quoted-crlf-linebrks-crlf-line-endings.csv @@ -0,0 +1,5 @@ +Short,Multi,Bool +a,"b +c",0 +d,"e +f",1 diff --git a/features/fixtures/quoted-lf-linebrks-crlf-line-endings.csv b/features/fixtures/quoted-lf-linebrks-crlf-line-endings.csv new file mode 100644 index 00000000..3e86aee7 --- /dev/null +++ b/features/fixtures/quoted-lf-linebrks-crlf-line-endings.csv @@ -0,0 +1,5 @@ +Short,Multi,Bool +a,"b +c",0 +d,"e +f",1 diff --git a/features/step_definitions/validation_errors_steps.rb b/features/step_definitions/validation_errors_steps.rb index 7e8fac01..b9fead95 100644 --- a/features/step_definitions/validation_errors_steps.rb +++ b/features/step_definitions/validation_errors_steps.rb @@ -86,5 +86,8 @@ end Then(/^there should be no "(.*?)" errors$/) do |type| - @errors.each { |error| error.type.should_not == type.to_sym } + @errors.each { |error| + error.type.should_not + type.to_sym + } end diff --git a/features/validation_errors.feature b/features/validation_errors.feature index 8fbc0825..b646760f 100644 --- a/features/validation_errors.feature +++ b/features/validation_errors.feature @@ -145,3 +145,21 @@ Feature: Get validation errors And I ask if there are errors Then there should be 1 error And that error should have the type "line_breaks" + + Scenario: CRLF line breaks inside quotes with CRLF line endings causes no error + Given I have a CSV file called "quoted-crlf-linebrks-crlf-line-endings.csv" + And it is stored at the url "http://example.com/example1.csv" + And I ask if there are errors + Then there should be 0 error + + Scenario: LF line breaks inside quotes with CRLF line endings causes no error + Given I have a CSV file called "quoted-lf-linebrks-crlf-line-endings.csv" + And it is stored at the url "http://example.com/example1.csv" + And I ask if there are errors + Then there should be 0 error + + Scenario: CR line breaks inside quotes with CRLF line endings causes no error + Given I have a CSV file called "quoted-cr-linebrks-crlf-line-endings.csv" + And it is stored at the url "http://example.com/example1.csv" + And I ask if there are errors + Then there should be 0 error diff --git a/lib/csvlint/validate.rb b/lib/csvlint/validate.rb index 1ddb2899..2cfa0310 100644 --- a/lib/csvlint/validate.rb +++ b/lib/csvlint/validate.rb @@ -185,7 +185,8 @@ def parse_contents(stream, line = nil) @csv_options[:encoding] = @encoding begin - row = LineCSV.parse_line(stream, **@csv_options) + lineopts = csv_options_for_line(stream) + row = LineCSV.parse_line(stream, **lineopts) rescue LineCSV::MalformedCSVError => e build_exception_messages(e, stream, current_line) unless e.message.include?("UTF") && @reported_invalid_encoding end @@ -215,6 +216,19 @@ def parse_contents(stream, line = nil) @data << row end + def csv_options_for_line(line) + # If a specific row_sep has been explicitly specified, don't mess with it. + return @csv_options unless @csv_options[:row_sep] == :auto + + if line.end_with?("\r\n") + @csv_options.merge({row_sep: "\r\n"}) + elsif ["\r", "\n"].include?(line[-1, 1]) + @csv_options.merge({row_sep: line[-1, 1]}) + else + @csv_options + end + end + def finish sum = @col_counts.inject(:+) unless sum.nil? diff --git a/lib/csvlint/version.rb b/lib/csvlint/version.rb index 3aafab75..95674a7d 100644 --- a/lib/csvlint/version.rb +++ b/lib/csvlint/version.rb @@ -1,3 +1,3 @@ module Csvlint - VERSION = "1.4.0" + VERSION = "1.4.1" end