Skip to content

Commit 845bd8a

Browse files
committed
Create parent path of report file
1 parent c916eb4 commit 845bd8a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

configuration/configuration.go

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
7070

7171
recursive, _ = flags.GetBool("recursive")
7272

73-
// TODO: validate path
7473
reportFilePathString, _ := flags.GetString("report-file")
7574
reportFilePath = paths.New(reportFilePathString)
7675

result/result.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,19 @@ func (results Type) jsonReportRaw() []byte {
225225

226226
// WriteReport writes a report for all projects to the specified file.
227227
func (results Type) WriteReport() error {
228-
err := configuration.ReportFilePath().WriteFile(results.jsonReportRaw())
228+
reportFilePath := configuration.ReportFilePath()
229+
reportFilePathParentExists, err := reportFilePath.Parent().ExistCheck()
230+
if err != nil {
231+
return fmt.Errorf("Problem processing --report-file flag value %v: %v", reportFilePath, err)
232+
}
233+
if !reportFilePathParentExists {
234+
err = reportFilePath.Parent().MkdirAll()
235+
if err != nil {
236+
return fmt.Errorf("Error while creating report file path (%v): %v", reportFilePath.Parent(), err)
237+
}
238+
}
239+
240+
err = reportFilePath.WriteFile(results.jsonReportRaw())
229241
if err != nil {
230242
return fmt.Errorf("Error while writing report: %v", err)
231243
}

result/result_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,17 @@ func TestWriteReport(t *testing.T) {
3838
reportFolderPath := paths.New(reportFolderPathString)
3939

4040
reportFilePath := reportFolderPath.Join("report-file.json")
41+
_, err = reportFilePath.Create() // Create file using the report folder path.
42+
require.Nil(t, err)
43+
44+
flags.Set("report-file", reportFilePath.Join("report-file.json").String())
45+
assert.Nil(t, configuration.Initialize(flags, projectPaths))
46+
assert.Error(t, Results.WriteReport(), "Parent folder creation should fail due to a collision with an existing file at that path")
47+
48+
reportFilePath = reportFolderPath.Join("report-file-subfolder", "report-file-subsubfolder", "report-file.json")
4149
flags.Set("report-file", reportFilePath.String())
4250
assert.Nil(t, configuration.Initialize(flags, projectPaths))
43-
assert.NoError(t, Results.WriteReport())
51+
assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders")
4452

4553
reportFile, err := reportFilePath.Open()
4654
require.Nil(t, err)

0 commit comments

Comments
 (0)