Skip to content

Commit 6527e99

Browse files
authored
fix: Ensure to create folders for outputfile (#329)
1 parent 42b8bad commit 6527e99

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

internal/utils/file.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ package utils
22

33
import (
44
"io/fs"
5+
"os"
6+
"path/filepath"
57
)
68

79
const (
810
FILEPERM_755 fs.FileMode = 0755 // Owner=rwx, Group=r-x, Other=r-x
911
FILEPERM_666 fs.FileMode = 0666 // Owner=rw-, Group=rw-, Other=rw-
1012
)
13+
14+
func CreateFilePath(path string) error {
15+
dirPath := filepath.Dir(path)
16+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
17+
err = os.MkdirAll(dirPath, FILEPERM_755)
18+
return err
19+
}
20+
return nil
21+
}

internal/utils/file_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package utils
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCreateFilePath(t *testing.T) {
11+
filePath := filepath.Join(t.TempDir(), "newDir", "file.txt")
12+
pathToCreate := filepath.Dir(filePath)
13+
err := CreateFilePath(filePath)
14+
assert.NoError(t, err)
15+
assert.DirExists(t, pathToCreate)
16+
}

pkg/local_workflows/output_workflow.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ func jsonWriteToFile(debugLogger *zerolog.Logger, input []workflow.Data, i int,
189189
if err := outputDestination.Remove(jsonFileName); err != nil {
190190
return fmt.Errorf("failed to remove existing output file: %w", err)
191191
}
192+
193+
if err := iUtils.CreateFilePath(jsonFileName); err != nil {
194+
return fmt.Errorf("failed to create output folder: %w", err)
195+
}
196+
192197
if err := outputDestination.WriteFile(jsonFileName, singleData, iUtils.FILEPERM_666); err != nil {
193198
return fmt.Errorf("failed to write json output: %w", err)
194199
}

pkg/local_workflows/output_workflow/findings_model_tools.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ func getSarifFileRenderer(config configuration.Configuration, findings []*local_
111111
return nil, nil
112112
}
113113

114+
pathError := iUtils.CreateFilePath(outputFileName)
115+
if pathError != nil {
116+
return nil, pathError
117+
}
118+
114119
file, fileErr := os.OpenFile(outputFileName, os.O_WRONLY|os.O_CREATE, 0644)
115120
if fileErr != nil {
116121
return nil, fileErr

pkg/local_workflows/output_workflow/findings_model_tools_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package output_workflow
22

33
import (
4+
"path/filepath"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -59,32 +60,35 @@ func Test_getSarifFileRenderer(t *testing.T) {
5960
t.Run("write empty file", func(t *testing.T) {
6061
localFindings := getLocalFindingsSkeleton(t, 0)
6162
config := configuration.NewWithOpts()
62-
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, t.TempDir()+"/somefile")
63+
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, filepath.Join(t.TempDir(), "not-existing", "somefile"))
6364
config.Set(OUTPUT_CONFIG_WRITE_EMPTY_FILE, true)
6465
renderer, err := getSarifFileRenderer(config, localFindings)
6566
assert.NoError(t, err)
6667
assert.NotNil(t, renderer)
6768
assert.NoError(t, renderer.closer())
69+
assert.FileExists(t, config.GetString(OUTPUT_CONFIG_KEY_SARIF_FILE))
6870
})
6971

7072
t.Run("write non empty file", func(t *testing.T) {
7173
localFindings := getLocalFindingsSkeleton(t, 1)
7274
config := configuration.NewWithOpts()
73-
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, t.TempDir()+"/somefile")
75+
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, filepath.Join(t.TempDir(), "not-existing", "somefile"))
7476
config.Set(OUTPUT_CONFIG_WRITE_EMPTY_FILE, false)
7577
renderer, err := getSarifFileRenderer(config, localFindings)
7678
assert.NoError(t, err)
7779
assert.NotNil(t, renderer)
7880
assert.NoError(t, renderer.closer())
81+
assert.FileExists(t, config.GetString(OUTPUT_CONFIG_KEY_SARIF_FILE))
7982
})
8083

8184
t.Run("don't write empty file", func(t *testing.T) {
8285
localFindings := getLocalFindingsSkeleton(t, 0)
8386
config := configuration.NewWithOpts()
84-
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, t.TempDir()+"/somefile")
87+
config.Set(OUTPUT_CONFIG_KEY_SARIF_FILE, filepath.Join(t.TempDir(), "not-existing", "somefile"))
8588
config.Set(OUTPUT_CONFIG_WRITE_EMPTY_FILE, false)
8689
renderer, err := getSarifFileRenderer(config, localFindings)
8790
assert.NoError(t, err)
8891
assert.Nil(t, renderer)
92+
assert.NoFileExists(t, config.GetString(OUTPUT_CONFIG_KEY_SARIF_FILE))
8993
})
9094
}

pkg/local_workflows/output_workflow_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,19 @@ func BenchmarkTransformationAndOutputWorkflow(b *testing.B) {
537537
}
538538
})
539539
}
540+
541+
func TestJsonWriteToFile(t *testing.T) {
542+
i := 0
543+
logger := zerolog.Nop()
544+
outputDi := utils.NewOutputDestination()
545+
fileName := filepath.Join(t.TempDir(), "not-existing", "file.json")
546+
rawData := []byte("hello world")
547+
data := []workflow.Data{
548+
workflow.NewData(workflow.NewTypeIdentifier(WORKFLOWID_OUTPUT_WORKFLOW, "output"), "content-type", rawData),
549+
}
550+
551+
assert.NoFileExists(t, fileName)
552+
err := jsonWriteToFile(&logger, data, i, rawData, fileName, outputDi)
553+
assert.NoError(t, err)
554+
assert.FileExists(t, fileName)
555+
}

0 commit comments

Comments
 (0)