Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: report directory is not exist #207

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
nargov marked this conversation as resolved.
Show resolved Hide resolved

os.RemoveAll(filepath.Join(tempDir, "test_temp_dir"))
baruchiro marked this conversation as resolved.
Show resolved Hide resolved
}
Loading