mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	* 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>
This commit is contained in:
		| @@ -22,7 +22,11 @@ var quoteRegexp = regexp.MustCompile(`["'][\s\S]+?["']`) | |||||||
| func CreateReader(input io.Reader, delimiter rune) *stdcsv.Reader { | func CreateReader(input io.Reader, delimiter rune) *stdcsv.Reader { | ||||||
| 	rd := stdcsv.NewReader(input) | 	rd := stdcsv.NewReader(input) | ||||||
| 	rd.Comma = delimiter | 	rd.Comma = delimiter | ||||||
| 	rd.TrimLeadingSpace = true | 	if delimiter != '\t' && delimiter != ' ' { | ||||||
|  | 		// TrimLeadingSpace can't be true when delimiter is a tab or a space as the value for a column might be empty, | ||||||
|  | 		// thus would change `\t\t` to just `\t` or `  ` (two spaces) to just ` ` (single space) | ||||||
|  | 		rd.TrimLeadingSpace = true | ||||||
|  | 	} | ||||||
| 	return rd | 	return rd | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -17,16 +17,57 @@ func TestCreateReader(t *testing.T) { | |||||||
| 	assert.Equal(t, ',', rd.Comma) | 	assert.Equal(t, ',', rd.Comma) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | //nolint | ||||||
| func TestCreateReaderAndGuessDelimiter(t *testing.T) { | func TestCreateReaderAndGuessDelimiter(t *testing.T) { | ||||||
| 	input := "a;b;c\n1;2;3\n4;5;6" | 	var csvToRowsMap = map[string][][]string{ | ||||||
|  | 		`a;b;c | ||||||
|  | 1;2;3 | ||||||
|  | 4;5;6`: {{"a", "b", "c"}, {"1", "2", "3"}, {"4", "5", "6"}}, | ||||||
|  | 		`col1	col2	col3 | ||||||
|  | a	b	c | ||||||
|  | 	e	f | ||||||
|  | g	h	i | ||||||
|  | j		l | ||||||
|  | m	n	 | ||||||
|  | p	q	r | ||||||
|  | 		u | ||||||
|  | v	w	x | ||||||
|  | y		 | ||||||
|  | 		`: {{"col1", "col2", "col3"}, | ||||||
|  | 			{"a", "b", "c"}, | ||||||
|  | 			{"", "e", "f"}, | ||||||
|  | 			{"g", "h", "i"}, | ||||||
|  | 			{"j", "", "l"}, | ||||||
|  | 			{"m", "n", ""}, | ||||||
|  | 			{"p", "q", "r"}, | ||||||
|  | 			{"", "", "u"}, | ||||||
|  | 			{"v", "w", "x"}, | ||||||
|  | 			{"y", "", ""}, | ||||||
|  | 			{"", "", ""}}, | ||||||
|  | 		` col1,col2,col3 | ||||||
|  |  a, b, c | ||||||
|  | d,e,f | ||||||
|  |  ,h, i | ||||||
|  | j, ,  | ||||||
|  |  , , `: {{"col1", "col2", "col3"}, | ||||||
|  | 			{"a", "b", "c"}, | ||||||
|  | 			{"d", "e", "f"}, | ||||||
|  | 			{"", "h", "i"}, | ||||||
|  | 			{"j", "", ""}, | ||||||
|  | 			{"", "", ""}}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	rd, err := CreateReaderAndGuessDelimiter(strings.NewReader(input)) | 	for csv, expectedRows := range csvToRowsMap { | ||||||
| 	assert.NoError(t, err) | 		rd, err := CreateReaderAndGuessDelimiter(strings.NewReader(csv)) | ||||||
| 	assert.Equal(t, ';', rd.Comma) | 		assert.NoError(t, err) | ||||||
|  | 		rows, err := rd.ReadAll() | ||||||
|  | 		assert.NoError(t, err) | ||||||
|  | 		assert.EqualValues(t, rows, expectedRows) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func TestGuessDelimiter(t *testing.T) { | func TestGuessDelimiter(t *testing.T) { | ||||||
| 	var kases = map[string]rune{ | 	var csvToDelimiterMap = map[string]rune{ | ||||||
| 		"a":                         ',', | 		"a":                         ',', | ||||||
| 		"1,2":                       ',', | 		"1,2":                       ',', | ||||||
| 		"1;2":                       ';', | 		"1;2":                       ';', | ||||||
| @@ -37,7 +78,7 @@ func TestGuessDelimiter(t *testing.T) { | |||||||
| 		"<br/>":                     ',', | 		"<br/>":                     ',', | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for k, v := range kases { | 	for csv, expectedDelimiter := range csvToDelimiterMap { | ||||||
| 		assert.EqualValues(t, guessDelimiter([]byte(k)), v) | 		assert.EqualValues(t, guessDelimiter([]byte(csv)), expectedDelimiter) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user