Skip to content

Commit

Permalink
fixes #200 by adding a check that the path exists and if not create it
Browse files Browse the repository at this point in the history
  • Loading branch information
nargov committed Feb 18, 2024
1 parent 5a7ac9d commit 309dc3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions reporting/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (r *Report) ShowReport(format string, cfg *config.Config) error {

func (r *Report) WriteFile(reportPath []string, cfg *config.Config) error {
for _, path := range reportPath {
os.MkdirAll(filepath.Dir(path), 0600)

Check failure on line 44 in reporting/report.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Error return value of `os.MkdirAll` is not checked (errcheck)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.

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()
resultFile := filepath.Join(tempDir, "test_temp_dir", "sub_dir", "report.yaml")
err := report.WriteFile([]string{resultFile}, &config.Config{Name: "report", Version: "5"})
if err != nil {
t.Error(err)
}

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

0 comments on commit 309dc3c

Please sign in to comment.