Skip to content

Commit a530b08

Browse files
authored
Merge pull request #31 from arduino/per1234/create-report-file-parents
Create parent path of report file
2 parents 33b7ac0 + 845bd8a commit a530b08

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

Diff for: command/command.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func ArduinoCheck(rootCommand *cobra.Command, cliArguments []string) {
6363

6464
if configuration.ReportFilePath() != nil {
6565
// Write report file.
66-
result.Results.WriteReport()
66+
if err := result.Results.WriteReport(); err != nil {
67+
feedback.Error(err.Error())
68+
os.Exit(1)
69+
}
6770
}
6871

6972
if !result.Results.Passed() {

Diff for: 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

Diff for: result/result.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,25 @@ func (results Type) jsonReportRaw() []byte {
224224
}
225225

226226
// WriteReport writes a report for all projects to the specified file.
227-
func (results Type) WriteReport() {
228-
// Write report file
229-
err := configuration.ReportFilePath().WriteFile(results.jsonReportRaw())
227+
func (results Type) WriteReport() error {
228+
reportFilePath := configuration.ReportFilePath()
229+
reportFilePathParentExists, err := reportFilePath.Parent().ExistCheck()
230230
if err != nil {
231-
feedback.Errorf("Error while writing report: %v", err)
232-
os.Exit(1)
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+
}
233238
}
239+
240+
err = reportFilePath.WriteFile(results.jsonReportRaw())
241+
if err != nil {
242+
return fmt.Errorf("Error while writing report: %v", err)
243+
}
244+
245+
return nil
234246
}
235247

236248
// Passed returns whether the checks passed cumulatively.

Diff for: result/result_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package result
17+
18+
import (
19+
"io/ioutil"
20+
"os"
21+
"testing"
22+
23+
"github.com/arduino/arduino-check/configuration"
24+
"github.com/arduino/arduino-check/util/test"
25+
"github.com/arduino/go-paths-helper"
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
)
29+
30+
func TestWriteReport(t *testing.T) {
31+
flags := test.ConfigurationFlags()
32+
33+
projectPaths := []string{"/foo"}
34+
35+
reportFolderPathString, err := ioutil.TempDir("", "arduino-check-result-TestWriteReport")
36+
require.Nil(t, err)
37+
defer os.RemoveAll(reportFolderPathString) // clean up
38+
reportFolderPath := paths.New(reportFolderPathString)
39+
40+
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")
49+
flags.Set("report-file", reportFilePath.String())
50+
assert.Nil(t, configuration.Initialize(flags, projectPaths))
51+
assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders")
52+
53+
reportFile, err := reportFilePath.Open()
54+
require.Nil(t, err)
55+
reportFileInfo, err := reportFile.Stat()
56+
require.Nil(t, err)
57+
reportFileBytes := make([]byte, reportFileInfo.Size())
58+
_, err = reportFile.Read(reportFileBytes)
59+
require.Nil(t, err)
60+
assert.True(t, assert.ObjectsAreEqualValues(reportFileBytes, Results.jsonReportRaw()), "Report file contents are correct")
61+
}

0 commit comments

Comments
 (0)