Skip to content

Commit 8f9ac43

Browse files
richmahnzeripath
andauthored
Fixes #16559 - Do not trim leading spaces for tab delimited (#17442)
* Fixes #16559 - Do not trim leading spaces for tab delimited * Adds back semicolon delimited test * Fixes linting * Adds nolint directive to test because uses strings starting with spaces Co-authored-by: zeripath <art27@cantab.net>
1 parent 6e2c64f commit 8f9ac43

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

modules/csv/csv.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ var quoteRegexp = regexp.MustCompile(`["'][\s\S]+?["']`)
2222
func CreateReader(input io.Reader, delimiter rune) *stdcsv.Reader {
2323
rd := stdcsv.NewReader(input)
2424
rd.Comma = delimiter
25-
rd.TrimLeadingSpace = true
25+
if delimiter != '\t' && delimiter != ' ' {
26+
// TrimLeadingSpace can't be true when delimiter is a tab or a space as the value for a column might be empty,
27+
// thus would change `\t\t` to just `\t` or ` ` (two spaces) to just ` ` (single space)
28+
rd.TrimLeadingSpace = true
29+
}
2630
return rd
2731
}
2832

modules/csv/csv_test.go

+48-7
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,57 @@ func TestCreateReader(t *testing.T) {
1717
assert.Equal(t, ',', rd.Comma)
1818
}
1919

20+
//nolint
2021
func TestCreateReaderAndGuessDelimiter(t *testing.T) {
21-
input := "a;b;c\n1;2;3\n4;5;6"
22+
var csvToRowsMap = map[string][][]string{
23+
`a;b;c
24+
1;2;3
25+
4;5;6`: {{"a", "b", "c"}, {"1", "2", "3"}, {"4", "5", "6"}},
26+
`col1 col2 col3
27+
a b c
28+
e f
29+
g h i
30+
j l
31+
m n
32+
p q r
33+
u
34+
v w x
35+
y
36+
`: {{"col1", "col2", "col3"},
37+
{"a", "b", "c"},
38+
{"", "e", "f"},
39+
{"g", "h", "i"},
40+
{"j", "", "l"},
41+
{"m", "n", ""},
42+
{"p", "q", "r"},
43+
{"", "", "u"},
44+
{"v", "w", "x"},
45+
{"y", "", ""},
46+
{"", "", ""}},
47+
` col1,col2,col3
48+
a, b, c
49+
d,e,f
50+
,h, i
51+
j, ,
52+
, , `: {{"col1", "col2", "col3"},
53+
{"a", "b", "c"},
54+
{"d", "e", "f"},
55+
{"", "h", "i"},
56+
{"j", "", ""},
57+
{"", "", ""}},
58+
}
2259

23-
rd, err := CreateReaderAndGuessDelimiter(strings.NewReader(input))
24-
assert.NoError(t, err)
25-
assert.Equal(t, ';', rd.Comma)
60+
for csv, expectedRows := range csvToRowsMap {
61+
rd, err := CreateReaderAndGuessDelimiter(strings.NewReader(csv))
62+
assert.NoError(t, err)
63+
rows, err := rd.ReadAll()
64+
assert.NoError(t, err)
65+
assert.EqualValues(t, rows, expectedRows)
66+
}
2667
}
2768

2869
func TestGuessDelimiter(t *testing.T) {
29-
var kases = map[string]rune{
70+
var csvToDelimiterMap = map[string]rune{
3071
"a": ',',
3172
"1,2": ',',
3273
"1;2": ';',
@@ -37,7 +78,7 @@ func TestGuessDelimiter(t *testing.T) {
3778
"<br/>": ',',
3879
}
3980

40-
for k, v := range kases {
41-
assert.EqualValues(t, guessDelimiter([]byte(k)), v)
81+
for csv, expectedDelimiter := range csvToDelimiterMap {
82+
assert.EqualValues(t, guessDelimiter([]byte(csv)), expectedDelimiter)
4283
}
4384
}

0 commit comments

Comments
 (0)