|
| 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