This is an automated email from the ASF dual-hosted git repository.
zhangliang2022 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new 62cd8dcb1 feat: csv file extension check (#4522)
62cd8dcb1 is described below
commit 62cd8dcb10ff0fab48614b62b27968546bf3a603
Author: Igor Izvekov <[email protected]>
AuthorDate: Wed Mar 1 04:27:28 2023 +0300
feat: csv file extension check (#4522)
* feat: added csv file extension check
* fix: error code
* fix: changed from panic to return error
* fix: changed default error to incubator-devlake error
---------
Co-authored-by: mindlesscloud <[email protected]>
---
backend/helpers/e2ehelper/data_flow_tester.go | 17 +++++++++--------
backend/helpers/pluginhelper/csv_file_test.go | 13 ++++++++++++-
backend/helpers/pluginhelper/csv_file_writer.go | 10 ++++++++--
3 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/backend/helpers/e2ehelper/data_flow_tester.go
b/backend/helpers/e2ehelper/data_flow_tester.go
index bc0723eb8..e52a27726 100644
--- a/backend/helpers/e2ehelper/data_flow_tester.go
+++ b/backend/helpers/e2ehelper/data_flow_tester.go
@@ -22,6 +22,13 @@ import (
"database/sql"
"encoding/json"
"fmt"
+ "os"
+ "strconv"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
"github.com/apache/incubator-devlake/core/config"
"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
@@ -34,12 +41,6 @@ import (
contextimpl "github.com/apache/incubator-devlake/impls/context"
"github.com/apache/incubator-devlake/impls/dalgorm"
"github.com/apache/incubator-devlake/impls/logruslog"
- "os"
- "strconv"
- "strings"
- "sync"
- "testing"
- "time"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -238,7 +239,7 @@ func (t *DataFlowTester) CreateSnapshot(dst schema.Tabler,
opts TableOptions) {
if err != nil {
panic(errors.Default.Wrap(err, fmt.Sprintf("unable to get
columns from table %s", dst.TableName())))
}
- csvWriter := pluginhelper.NewCsvFileWriter(opts.CSVRelPath, columns)
+ csvWriter, _ := pluginhelper.NewCsvFileWriter(opts.CSVRelPath, columns)
defer csvWriter.Close()
// define how to scan value
@@ -317,7 +318,7 @@ func (t *DataFlowTester) ExportRawTable(rawTableName
string, csvRelPath string)
panic(err)
}
- csvWriter := pluginhelper.NewCsvFileWriter(csvRelPath, allFields)
+ csvWriter, _ := pluginhelper.NewCsvFileWriter(csvRelPath, allFields)
defer csvWriter.Close()
for _, rawRow := range *rawRows {
diff --git a/backend/helpers/pluginhelper/csv_file_test.go
b/backend/helpers/pluginhelper/csv_file_test.go
index 42ac6c97f..b24448688 100644
--- a/backend/helpers/pluginhelper/csv_file_test.go
+++ b/backend/helpers/pluginhelper/csv_file_test.go
@@ -28,7 +28,7 @@ func TestExampleCsvFile(t *testing.T) {
filename := fmt.Sprintf(`%s/foobar.csv`, tmpPath)
println(filename)
- writer := NewCsvFileWriter(filename, []string{"id", "name", "json",
"created_at"})
+ writer, _ := NewCsvFileWriter(filename, []string{"id", "name", "json",
"created_at"})
writer.Write([]string{"123", "foobar", `{"url":
"https://example.com"}`, "2022-05-05 09:56:43.438000000"})
writer.Close()
@@ -40,3 +40,14 @@ func TestExampleCsvFile(t *testing.T) {
assert.Equal(t, row["json"], `{"url": "https://example.com"}`,
"json not euqal")
}
}
+
+func TestWrongCsvPath(t *testing.T) {
+ tmpPath := t.TempDir()
+ filename := fmt.Sprintf(`%s/foobar.txt`, tmpPath)
+ println(filename)
+
+ _, err := NewCsvFileWriter(filename, []string{})
+ if err == nil {
+ t.Fatal("the code did not return error")
+ }
+}
diff --git a/backend/helpers/pluginhelper/csv_file_writer.go
b/backend/helpers/pluginhelper/csv_file_writer.go
index 4241a3cdd..472e54668 100644
--- a/backend/helpers/pluginhelper/csv_file_writer.go
+++ b/backend/helpers/pluginhelper/csv_file_writer.go
@@ -19,7 +19,9 @@ package pluginhelper
import (
"encoding/csv"
+ "github.com/apache/incubator-devlake/core/errors"
"os"
+ "path/filepath"
)
// CsvFileWriter make writer for saving csv file easier, it write tuple to csv
file
@@ -35,8 +37,12 @@ type CsvFileWriter struct {
}
// NewCsvFileWriter create a `*CsvFileWriter` based on path to saving csv file
-func NewCsvFileWriter(csvPath string, fields []string) *CsvFileWriter {
+func NewCsvFileWriter(csvPath string, fields []string) (*CsvFileWriter,
errors.Error) {
// open csv file
+ if filepath.Ext(csvPath) != ".csv" {
+ return nil, errors.BadInput.New("the file does not have
\".csv\" extension")
+ }
+
csvFile, err := os.Create(csvPath)
if err != nil {
panic(err)
@@ -55,7 +61,7 @@ func NewCsvFileWriter(csvPath string, fields []string)
*CsvFileWriter {
file: csvFile,
writer: csvWriter,
fields: fields,
- }
+ }, nil
}
// Close releases resource