2021-03-29 23:44:28 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-03-29 23:44:28 +03:00
|
|
|
|
|
|
|
package gitdiff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-03 13:30:41 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-03-29 23:44:28 +03:00
|
|
|
csv_module "code.gitea.io/gitea/modules/csv"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2021-03-29 23:44:28 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:42:06 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-29 23:44:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCSVDiff(t *testing.T) {
|
2022-01-20 20:46:10 +03:00
|
|
|
cases := []struct {
|
2021-03-29 23:44:28 +03:00
|
|
|
diff string
|
|
|
|
base string
|
|
|
|
head string
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
cells [][]TableDiffCellType
|
2021-03-29 23:44:28 +03:00
|
|
|
}{
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
// case 0 - initial commit of a csv
|
2021-03-29 23:44:28 +03:00
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -0,0 +1,2 @@
|
|
|
|
+col1,col2
|
|
|
|
+a,a`,
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
base: "",
|
|
|
|
head: `col1,col2
|
|
|
|
a,a`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellAdd, TableDiffCellAdd},
|
2022-01-20 20:46:10 +03:00
|
|
|
{TableDiffCellAdd, TableDiffCellAdd},
|
|
|
|
},
|
2021-03-29 23:44:28 +03:00
|
|
|
},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
// case 1 - adding 1 row at end
|
2021-03-29 23:44:28 +03:00
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +1,3 @@
|
|
|
|
col1,col2
|
|
|
|
-a,a
|
|
|
|
+a,a
|
|
|
|
+b,b`,
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
base: `col1,col2
|
|
|
|
a,a`,
|
|
|
|
head: `col1,col2
|
|
|
|
a,a
|
|
|
|
b,b`,
|
|
|
|
cells: [][]TableDiffCellType{
|
2022-01-20 20:46:10 +03:00
|
|
|
{TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
{TableDiffCellUnchanged, TableDiffCellUnchanged},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
{TableDiffCellAdd, TableDiffCellAdd},
|
|
|
|
},
|
2021-03-29 23:44:28 +03:00
|
|
|
},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
// case 2 - row deleted
|
2021-03-29 23:44:28 +03:00
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,3 +1,2 @@
|
|
|
|
col1,col2
|
|
|
|
-a,a
|
|
|
|
b,b`,
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
base: `col1,col2
|
|
|
|
a,a
|
|
|
|
b,b`,
|
|
|
|
head: `col1,col2
|
|
|
|
b,b`,
|
|
|
|
cells: [][]TableDiffCellType{
|
2022-01-20 20:46:10 +03:00
|
|
|
{TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
{TableDiffCellDel, TableDiffCellDel},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
{TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
},
|
2021-03-29 23:44:28 +03:00
|
|
|
},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
// case 3 - row changed
|
2021-03-29 23:44:28 +03:00
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +1,2 @@
|
|
|
|
col1,col2
|
|
|
|
-b,b
|
|
|
|
+b,c`,
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
base: `col1,col2
|
|
|
|
b,b`,
|
|
|
|
head: `col1,col2
|
|
|
|
b,c`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
{TableDiffCellUnchanged, TableDiffCellChanged},
|
|
|
|
},
|
2021-03-29 23:44:28 +03:00
|
|
|
},
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
// case 4 - all deleted
|
2021-03-29 23:44:28 +03:00
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +0,0 @@
|
|
|
|
-col1,col2
|
|
|
|
-b,c`,
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
base: `col1,col2
|
|
|
|
b,c`,
|
|
|
|
head: "",
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellDel, TableDiffCellDel},
|
|
|
|
{TableDiffCellDel, TableDiffCellDel},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// case 5 - renames first column
|
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,3 +1,3 @@
|
|
|
|
-col1,col2,col3
|
|
|
|
+cola,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
base: `col1,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
head: `cola,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellDel, TableDiffCellAdd, TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
{TableDiffCellDel, TableDiffCellAdd, TableDiffCellUnchanged, TableDiffCellUnchanged},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// case 6 - inserts a column after first, deletes last column
|
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +1,2 @@
|
|
|
|
-col1,col2,col3
|
|
|
|
-a,b,c
|
|
|
|
+col1,col1a,col2
|
|
|
|
+a,d,b`,
|
|
|
|
base: `col1,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
head: `col1,col1a,col2
|
|
|
|
a,d,b`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellUnchanged, TableDiffCellAdd, TableDiffCellDel, TableDiffCellMovedUnchanged},
|
|
|
|
{TableDiffCellUnchanged, TableDiffCellAdd, TableDiffCellDel, TableDiffCellMovedUnchanged},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// case 7 - deletes first column, inserts column after last
|
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +1,2 @@
|
|
|
|
-col1,col2,col3
|
|
|
|
-a,b,c
|
|
|
|
+col2,col3,col4
|
|
|
|
+b,c,d`,
|
|
|
|
base: `col1,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
head: `col2,col3,col4
|
|
|
|
b,c,d`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellDel, TableDiffCellUnchanged, TableDiffCellUnchanged, TableDiffCellAdd},
|
|
|
|
{TableDiffCellDel, TableDiffCellUnchanged, TableDiffCellUnchanged, TableDiffCellAdd},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// case 8 - two columns deleted, 2 added
|
|
|
|
{
|
|
|
|
diff: `diff --git a/unittest.csv b/unittest.csv
|
|
|
|
--- a/unittest.csv
|
|
|
|
+++ b/unittest.csv
|
|
|
|
@@ -1,2 +1,2 @@
|
|
|
|
-col1,col2,col
|
|
|
|
-a,b,c
|
|
|
|
+col3,col4,col5
|
|
|
|
+c,d,e`,
|
|
|
|
base: `col1,col2,col3
|
|
|
|
a,b,c`,
|
|
|
|
head: `col3,col4,col5
|
|
|
|
c,d,e`,
|
|
|
|
cells: [][]TableDiffCellType{
|
|
|
|
{TableDiffCellDel, TableDiffCellMovedUnchanged, TableDiffCellDel, TableDiffCellAdd, TableDiffCellAdd},
|
|
|
|
{TableDiffCellDel, TableDiffCellMovedUnchanged, TableDiffCellDel, TableDiffCellAdd, TableDiffCellAdd},
|
|
|
|
},
|
2021-03-29 23:44:28 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, c := range cases {
|
2023-10-03 13:30:41 +03:00
|
|
|
diff, err := ParsePatch(db.DefaultContext, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.diff), "")
|
2021-03-29 23:44:28 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ParsePatch failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseReader *csv.Reader
|
|
|
|
if len(c.base) > 0 {
|
2021-10-30 18:50:40 +03:00
|
|
|
baseReader, err = csv_module.CreateReaderAndDetermineDelimiter(nil, strings.NewReader(c.base))
|
2021-04-20 01:25:08 +03:00
|
|
|
if err != nil {
|
2021-10-30 18:50:40 +03:00
|
|
|
t.Errorf("CreateReaderAndDetermineDelimiter failed: %s", err)
|
2021-04-20 01:25:08 +03:00
|
|
|
}
|
2021-03-29 23:44:28 +03:00
|
|
|
}
|
|
|
|
var headReader *csv.Reader
|
|
|
|
if len(c.head) > 0 {
|
2021-10-30 18:50:40 +03:00
|
|
|
headReader, err = csv_module.CreateReaderAndDetermineDelimiter(nil, strings.NewReader(c.head))
|
2021-04-20 01:25:08 +03:00
|
|
|
if err != nil {
|
2021-10-30 18:50:40 +03:00
|
|
|
t.Errorf("CreateReaderAndDetermineDelimiter failed: %s", err)
|
2021-04-20 01:25:08 +03:00
|
|
|
}
|
2021-03-29 23:44:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := CreateCsvDiff(diff.Files[0], baseReader, headReader)
|
2024-07-30 22:42:06 +03:00
|
|
|
require.NoError(t, err)
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.Len(t, result, 1, "case %d: should be one section", n)
|
2021-03-29 23:44:28 +03:00
|
|
|
|
|
|
|
section := result[0]
|
2021-06-07 08:27:09 +03:00
|
|
|
assert.Len(t, section.Rows, len(c.cells), "case %d: should be %d rows", n, len(c.cells))
|
2021-03-29 23:44:28 +03:00
|
|
|
|
|
|
|
for i, row := range section.Rows {
|
Prevent NPE in CSV diff rendering when column removed (#17018)
Fixes #16837 if a column is deleted.
We were clobbering the columns that were added by looping through the aline (base) and then when bline (head) was looped through, it clobbered what was in the "cells" array that is show in the diff, and then left a nil cell because nothing was shifted.
This fix properly shifts the cells, and properly puts the b cell either at its location or after, according to what the aline placed in the cells.
This includes test, adding a new test function since adding/removing cells works best with three columns, not two, which results in 4 columns of the resulting cells because it has a deleted column and an added column. If you try this locally, you can try those cases and others, such as adding a column.
There was no need to do anything special for the rows when `aline == 0 || bline == 0` so that was removed. This allows the same code to be used for removed or added lines, with the bcell text always being the RightCell, acell text being the LeftCell.
I still added the patch zeripath gave at https://github.com/go-gitea/gitea/issues/16837#issuecomment-913007382 so that just in case for some reason a cell is nil (which shouldn't happen now) it doesn't throw a 500 error, so the user can at least view the raw diff.
Also fixes in the [view.go](https://github.com/go-gitea/gitea/pull/17018/files#diff-43a7f4747c7ba8bff888c9be11affaafd595fd55d27f3333840eb19df9fad393L521) file how if a CSV file is empty (either created empty or if you edit it and remove all contents) it throws a huge 500 error when you then save it (when you view the file). Since we allow creating, saving and pushing empty files, we shouldn't throw an error on an empty CSV file, but just show its empty contents. This doesn't happen if it is a Markdown file or other type of file that is empty.
EDIT: Now handled in the markup/csv renderer code
2021-10-20 22:10:03 +03:00
|
|
|
assert.Len(t, row.Cells, len(c.cells[i]), "case %d: row %d should have %d cells", n, i, len(c.cells[i]))
|
2021-03-29 23:44:28 +03:00
|
|
|
for j, cell := range row.Cells {
|
|
|
|
assert.Equal(t, c.cells[i][j], cell.Type, "case %d: row %d cell %d should be equal", n, i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|