Skip to content

Commit

Permalink
Merge pull request #2 from kspurgin/line-specific-opts
Browse files Browse the repository at this point in the history
Line specific opts
  • Loading branch information
kspurgin authored Jul 30, 2024
2 parents 88ef2a7 + 7200004 commit 4cb0fb5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions features/fixtures/quoted-cr-linebrks-crlf-line-endings.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Short,Multi,Bool
a,"bc",0
d,"ef",1
Expand Down
5 changes: 5 additions & 0 deletions features/fixtures/quoted-crlf-linebrks-crlf-line-endings.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Short,Multi,Bool
a,"b
c",0
d,"e
f",1
5 changes: 5 additions & 0 deletions features/fixtures/quoted-lf-linebrks-crlf-line-endings.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Short,Multi,Bool
a,"b
c",0
d,"e
f",1
5 changes: 4 additions & 1 deletion features/step_definitions/validation_errors_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions features/validation_errors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion lib/csvlint/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/csvlint/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Csvlint
VERSION = "1.4.0"
VERSION = "1.4.1"
end

0 comments on commit 4cb0fb5

Please sign in to comment.