Skip to content

Commit

Permalink
fix: report directory is not exist (#207)
Browse files Browse the repository at this point in the history
Closes #200 

I would usually start a discussion about a change before starting, but
since this was just a tiny fix I skipped it this time. Hope that's Ok.

This adds a path existence check to the path(s) passed in and ensures
the directory exists.

I submit this contribution under the Apache-2.0 license.

Co-authored-by: Baruch Odem (Rothkoff) <baruch.odem@checkmarx.com>
  • Loading branch information
nargov and Baruch Odem (Rothkoff) committed Feb 20, 2024
1 parent 8eff32b commit 9fe6a3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (r *Report) ShowReport(format string, cfg *config.Config) error {

func (r *Report) WriteFile(reportPath []string, cfg *config.Config) error {
for _, path := range reportPath {
err := os.MkdirAll(filepath.Dir(path), 0750)
if err != nil {
return err
}

file, err := os.Create(path)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions reporting/report_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package reporting

import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/checkmarx/2ms/config"
"github.com/checkmarx/2ms/secrets"
)

Expand Down Expand Up @@ -41,3 +44,16 @@ JPcHeO7M6FohKgcEHX84koQDN98J/L7pFlSoU7WOl6f8BKavIdeSTPS9qQYWdQuT
t.Errorf("got %+v want %+v", key, results)
}
}

func TestWriteReportInNonExistingDir(t *testing.T) {
report := Init()

tempDir := os.TempDir()
path := filepath.Join(tempDir, "test_temp_dir", "sub_dir", "report.yaml")
err := report.WriteFile([]string{path}, &config.Config{Name: "report", Version: "5"})
if err != nil {
t.Error(err)
}

os.RemoveAll(filepath.Join(tempDir, "test_temp_dir"))
}

0 comments on commit 9fe6a3d

Please sign in to comment.