-
Notifications
You must be signed in to change notification settings - Fork 122
Fix: Allow \r in unquoted fields when row separator doesn't contain \r #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b3f7932
fix: accept \r in unquoted fields when row_sep excludes \r
jsxs0 440c545
fix: accept \r in unquoted fields when row_sep excludes \r - simplifi…
jsxs0 196efe4
Merge branch 'fix-issue-60-accept-cr-without-quotes' of https://githu…
jsxs0 5b8f693
Update .gitignore
jsxs0 c237450
style: fix indentation in prepare_unquoted method
jsxs0 dd88061
reviewers' feedback
jsxs0 750531a
refactor(parser): simplify row separator escaping per code review
jsxs0 f323873
Updated test_unquoted_cr_with_crlf_row_separator
jsxs0 cb1084d
test_unquoted_cr_with_cr_row_separator test: logically problematic
jsxs0 f2a2f8f
Update test/csv/parse/test_invalid.rb
jsxs0 313f849
Following better organization principles
jsxs0 b455a09
test: consolidate unquoted CR tests, remove duplication
jsxs0 9be946f
Apply maintainer feedback: improve tests and clean up code
jsxs0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -138,28 +138,55 @@ def test_non_regex_edge_cases | |||||||
end | ||||||||
end | ||||||||
|
||||||||
def test_malformed_csv_cr_first_line | ||||||||
error = assert_raise(CSV::MalformedCSVError) do | ||||||||
CSV.parse_line("1,2\r,3", row_sep: "\n") | ||||||||
def test_unquoted_cr_with_lf_row_separator | ||||||||
data = "field1,field\rwith\rcr,field3\nrow2,data,here\n" | ||||||||
expected = [ | ||||||||
["field1", "field\rwith\rcr", "field3"], | ||||||||
["row2", "data", "here"] | ||||||||
] | ||||||||
assert_equal(expected, CSV.parse(data, row_sep: "\n")) | ||||||||
end | ||||||||
|
||||||||
def test_unquoted_cr_with_custom_row_separator | ||||||||
data = "field1,field\rwith\rcr,field3|row2,data,here|" | ||||||||
expected = [ | ||||||||
["field1", "field\rwith\rcr", "field3"], | ||||||||
["row2", "data", "here"] | ||||||||
] | ||||||||
assert_equal(expected, CSV.parse(data, row_sep: "|")) | ||||||||
end | ||||||||
|
||||||||
def test_unquoted_cr_rejected_when_included_in_row_separator | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the same naming rule for parse error case.
Suggested change
|
||||||||
data = "field1,field\r2,field3\r\nrow2,data,here\r\n" | ||||||||
assert_raise(CSV::MalformedCSVError) do | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also check the error message like other tests?
Suggested change
|
||||||||
CSV.parse(data, row_sep: "\r\n") | ||||||||
end | ||||||||
assert_equal("Unquoted fields do not allow new line <\"\\r\"> in line 1.", | ||||||||
error.message) | ||||||||
end | ||||||||
|
||||||||
def test_malformed_csv_cr_middle_line | ||||||||
csv = <<-CSV | ||||||||
line,1,abc | ||||||||
line,2,"def\nghi" | ||||||||
def test_quoted_cr_with_custom_row_separator | ||||||||
data = "field1,\"field\rwith\rcr\",field3|row2,data,here|" | ||||||||
expected = [ | ||||||||
["field1", "field\rwith\rcr", "field3"], | ||||||||
["row2", "data", "here"] | ||||||||
] | ||||||||
assert_equal(expected, CSV.parse(data, row_sep: "|")) | ||||||||
end | ||||||||
|
||||||||
line,4,some\rjunk | ||||||||
line,5,jkl | ||||||||
CSV | ||||||||
def test_unquoted_cr_in_middle_line | ||||||||
csv = "line,1,abc\nline,2,\"def\nghi\"\nline,4,some\rjunk\nline,5,jkl\n" | ||||||||
result = CSV.parse(csv) | ||||||||
expected = [ | ||||||||
["line", "1", "abc"], | ||||||||
["line", "2", "def\nghi"], | ||||||||
["line", "4", "some\rjunk"], | ||||||||
["line", "5", "jkl"] | ||||||||
] | ||||||||
assert_equal(expected, result) | ||||||||
end | ||||||||
|
||||||||
error = assert_raise(CSV::MalformedCSVError) do | ||||||||
CSV.parse(csv) | ||||||||
end | ||||||||
assert_equal("Unquoted fields do not allow new line <\"\\r\"> in line 4.", | ||||||||
error.message) | ||||||||
def test_empty_rows_with_cr | ||||||||
result = CSV.parse("\n" + "\r") | ||||||||
assert_equal([[], ["\r"]], result) | ||||||||
end | ||||||||
|
||||||||
def test_malformed_csv_unclosed_quote | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that we can remove this comment.
I feel that it's useful for commit message (the PR description in this repository) because it describes why we do this change but it may not be useful for readers of new code. (Nobody will not try using
"\r\n"
here.)