diff --git a/common/common.go b/common/common.go index 1560e5e3..0bf1791e 100644 --- a/common/common.go +++ b/common/common.go @@ -383,7 +383,8 @@ func OutputCSV(path string, data [][]string) error { return err } - defer writer.Flush() + writer.Flush() + file.Close() return nil } @@ -420,6 +421,11 @@ func WriteFile(file string, data []byte) error { return nil } +// RemoveFile removes a file +func RemoveFile(file string) error { + return os.Remove(file) +} + // GetURIPath returns the path of a URL given a URI func GetURIPath(uri string) string { urip, err := url.Parse(uri) diff --git a/common/common_test.go b/common/common_test.go index 4422ceef..806c9d85 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -652,6 +652,22 @@ func TestWriteFile(t *testing.T) { } } +func TestRemoveFile(t *testing.T) { + TestWriteFile(t) + path := "../testdata/writefiletest" + err := RemoveFile(path) + if err != nil { + t.Errorf("Test failed. Common RemoveFile error: %s", err) + } + + TestOutputCSV(t) + path = "../testdata/dump" + err = RemoveFile(path) + if err != nil { + t.Errorf("Test failed. Common RemoveFile error: %s", err) + } +} + func TestGetURIPath(t *testing.T) { t.Parallel() // mapping of input vs expected result