-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathresult_test.go
342 lines (313 loc) · 13.1 KB
/
result_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// This file is part of Arduino Lint.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of Arduino Lint.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package result
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/arduino/arduino-lint/internal/configuration"
"github.com/arduino/arduino-lint/internal/configuration/rulemode"
"github.com/arduino/arduino-lint/internal/project"
"github.com/arduino/arduino-lint/internal/project/projecttype"
"github.com/arduino/arduino-lint/internal/rule/ruleconfiguration"
"github.com/arduino/arduino-lint/internal/rule/rulelevel"
"github.com/arduino/arduino-lint/internal/rule/ruleresult"
"github.com/arduino/arduino-lint/internal/util/test"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var projectPaths []string
func init() {
projectPath, err := os.Getwd() // Path to an arbitrary folder that is guaranteed to exist.
if err != nil {
panic(err)
}
projectPaths = []string{projectPath}
}
func TestInitialize(t *testing.T) {
flags := test.ConfigurationFlags()
flags.Set("project-type", "sketch")
flags.Set("recursive", "false")
workingDirectoryPath, err := os.Getwd() // A convenient path that is guaranteed to exist.
require.Nil(t, err)
err = configuration.Initialize(flags, []string{workingDirectoryPath})
require.Nil(t, err)
var results Type
results.Initialize()
assert.Equal(t, paths.NewPathList(workingDirectoryPath), results.Configuration.Paths)
assert.Equal(t, projecttype.Sketch.String(), results.Configuration.ProjectType)
assert.False(t, results.Configuration.Recursive)
}
func TestRecord(t *testing.T) {
flags := test.ConfigurationFlags()
require.Nil(t, configuration.Initialize(flags, projectPaths))
lintedProject := project.Type{
Path: paths.New("/foo/bar"),
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Library,
}
var results Type
results.Initialize()
ruleConfiguration := ruleconfiguration.Configurations()[0]
ruleOutput := "foo"
flags.Set("verbose", "true")
require.Nil(t, configuration.Initialize(flags, projectPaths))
summaryText := results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
outputAssertion := "Rule LS001 result: fail\nERROR: Path does not contain a valid Arduino library. See: \n https://arduino.github.io/arduino-cli/latest/library-specification \n"
assert.Equal(t, outputAssertion, summaryText)
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
assert.Equal(t, fmt.Sprintf("Rule %s result: %s\n%s: %s\n", ruleConfiguration.ID, ruleresult.NotRun, rulelevel.Notice, ruleOutput), summaryText, "Non-fail result should not use message")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")
assert.Equal(t, fmt.Sprintf("Rule %s result: %s\n", ruleConfiguration.ID, ruleresult.Pass), summaryText, "Non-failure result with no rule function output should only use preface")
flags.Set("verbose", "false")
require.Nil(t, configuration.Initialize(flags, projectPaths))
ruleConfigurationCopy := ruleConfiguration
ruleConfigurationCopy.MessageTemplate = "bar"
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
outputAssertion = "ERROR: bar (Rule LS001)\n"
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is single line")
ruleConfigurationCopy.MessageTemplate = "bar\nbaz"
summaryText = results.Record(lintedProject, ruleConfigurationCopy, ruleresult.Fail, ruleOutput)
outputAssertion = "ERROR: bar \n baz \n (Rule LS001)\n"
assert.Equal(t, outputAssertion, summaryText, "Rule ID is appended to non-verbose fail message on same line when rule message is multiple lines")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.NotRun, ruleOutput)
assert.Equal(t, "", summaryText, "Non-fail result should not result in output in non-verbose mode")
summaryText = results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, "")
assert.Equal(t, "", summaryText, "Non-fail result should not result in output in non-verbose mode")
flags.Set("verbose", "true")
require.Nil(t, configuration.Initialize(flags, projectPaths))
ruleResult := ruleresult.Pass
results.Initialize()
results.Record(lintedProject, ruleConfiguration, ruleResult, ruleOutput)
projectReport := results.Projects[0]
assert.Equal(t, lintedProject.Path, projectReport.Path)
assert.Equal(t, lintedProject.ProjectType.String(), projectReport.ProjectType)
projectConfigurationReport := projectReport.Configuration
assert.Equal(t, rulemode.Compliance(configuration.RuleModes(lintedProject.ProjectType)), projectConfigurationReport.Compliance)
assert.Equal(t, rulemode.LibraryManager(configuration.RuleModes(lintedProject.ProjectType)), projectConfigurationReport.LibraryManager)
assert.Equal(t, configuration.RuleModes(lintedProject.ProjectType)[rulemode.Official], projectConfigurationReport.Official)
assert.Equal(t, 1, len(results.Projects[0].Rules), "Passing rule reports should be written to report in verbose mode")
ruleReport := projectReport.Rules[0]
assert.Equal(t, ruleConfiguration.Category, ruleReport.Category)
assert.Equal(t, ruleConfiguration.Subcategory, ruleReport.Subcategory)
assert.Equal(t, ruleConfiguration.ID, ruleReport.ID)
assert.Equal(t, ruleConfiguration.Brief, ruleReport.Brief)
assert.Equal(t, ruleConfiguration.Description, ruleReport.Description)
assert.Equal(t, ruleResult.String(), ruleReport.Result)
ruleLevel, _ := rulelevel.RuleLevel(ruleConfiguration, ruleResult, lintedProject)
assert.Equal(t, ruleLevel.String(), ruleReport.Level)
assert.Equal(t, ruleOutput, ruleReport.Message)
flags.Set("verbose", "false")
require.Nil(t, configuration.Initialize(flags, projectPaths))
results.Initialize()
results.Record(lintedProject, ruleConfiguration, ruleresult.Pass, ruleOutput)
assert.Equal(t, 0, len(results.Projects[0].Rules), "Passing rule reports should not be written to report in non-verbose mode")
results.Initialize()
results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
require.Equal(t, 1, len(projectReport.Rules), "Failing rule reports should be written to report in non-verbose mode")
assert.Len(t, results.Projects, 1)
previousProjectPath := lintedProject.Path
lintedProject.Path = paths.New("/foo/baz")
results.Record(lintedProject, ruleConfiguration, ruleresult.Fail, ruleOutput)
assert.Len(t, results.Projects, 2)
assert.Len(t, results.Projects[0].Rules, 1)
lintedProject.Path = previousProjectPath
results.Record(lintedProject, ruleconfiguration.Configurations()[1], ruleresult.Fail, ruleOutput)
assert.Len(t, results.Projects[0].Rules, 2)
}
func TestAddProjectSummary(t *testing.T) {
lintedProject := project.Type{
Path: paths.New("/foo/bar"),
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Library,
}
testTables := []struct {
results []ruleresult.Type
levels []rulelevel.Type
verbose string
expectedPass bool
expectedWarningCount int
expectedErrorCount int
}{
{
[]ruleresult.Type{ruleresult.Pass, ruleresult.Pass},
[]rulelevel.Type{rulelevel.Info, rulelevel.Info},
"true",
true,
0,
0,
},
{
[]ruleresult.Type{ruleresult.Pass, ruleresult.Pass},
[]rulelevel.Type{rulelevel.Info, rulelevel.Info},
"false",
true,
0,
0,
},
{
[]ruleresult.Type{ruleresult.Pass, ruleresult.Fail},
[]rulelevel.Type{rulelevel.Info, rulelevel.Warning},
"false",
true,
1,
0,
},
{
[]ruleresult.Type{ruleresult.Fail, ruleresult.Fail},
[]rulelevel.Type{rulelevel.Error, rulelevel.Warning},
"false",
false,
1,
1,
},
}
for _, testTable := range testTables {
flags := test.ConfigurationFlags()
flags.Set("verbose", testTable.verbose)
require.Nil(t, configuration.Initialize(flags, projectPaths))
var results Type
results.Initialize()
ruleIndex := 0
for testDataIndex, result := range testTable.results {
results.Record(lintedProject, ruleconfiguration.Configurations()[0], result, "")
if (result == ruleresult.Fail) || configuration.Verbose() {
level := testTable.levels[testDataIndex].String()
results.Projects[0].Rules[ruleIndex].Level = level
ruleIndex += 1
}
}
results.AddProjectSummary(lintedProject)
assert.Equal(t, testTable.expectedPass, results.Projects[0].Summary.Pass)
assert.Equal(t, testTable.expectedWarningCount, results.Projects[0].Summary.WarningCount)
assert.Equal(t, testTable.expectedErrorCount, results.Projects[0].Summary.ErrorCount)
if testTable.expectedErrorCount == 0 && testTable.expectedWarningCount == 0 {
assert.Equal(t, "Linter results for project: no errors or warnings", results.ProjectSummaryText(lintedProject))
} else {
assert.Equal(t, fmt.Sprintf("Linter results for project: %v ERRORS, %v WARNINGS", testTable.expectedErrorCount, testTable.expectedWarningCount), results.ProjectSummaryText(lintedProject))
}
}
}
func TestAddSummary(t *testing.T) {
lintedProject := project.Type{
Path: paths.New("/foo/bar"),
ProjectType: projecttype.Sketch,
SuperprojectType: projecttype.Library,
}
testTables := []struct {
projectSummaries []summaryReportType
expectedPass bool
expectedWarningCount int
expectedErrorCount int
}{
{
[]summaryReportType{
{
Pass: true,
WarningCount: 0,
ErrorCount: 0,
},
{
Pass: true,
WarningCount: 0,
ErrorCount: 0,
},
},
true,
0,
0,
},
{
[]summaryReportType{
{
Pass: true,
WarningCount: 1,
ErrorCount: 0,
},
{
Pass: true,
WarningCount: 2,
ErrorCount: 0,
},
},
true,
3,
0,
},
{
[]summaryReportType{
{
Pass: false,
WarningCount: 1,
ErrorCount: 0,
},
{
Pass: true,
WarningCount: 2,
ErrorCount: 2,
},
},
false,
3,
2,
},
}
for _, testTable := range testTables {
var results Type
for projectIndex, projectSummary := range testTable.projectSummaries {
lintedProject.Path = paths.New(fmt.Sprintf("/foo/bar%v", projectIndex)) // Use a unique path to generate a new project report.
results.Record(lintedProject, ruleconfiguration.Configurations()[0], ruleresult.Pass, "")
results.AddProjectSummary(lintedProject)
results.Projects[projectIndex].Summary = projectSummary
}
results.AddSummary()
assert.Equal(t, testTable.expectedPass, results.Summary.Pass)
assert.Equal(t, testTable.expectedPass, results.Passed())
assert.Equal(t, testTable.expectedWarningCount, results.Summary.WarningCount)
assert.Equal(t, testTable.expectedErrorCount, results.Summary.ErrorCount)
if testTable.expectedErrorCount == 0 && testTable.expectedWarningCount == 0 {
assert.Equal(t, "Linter results for projects: no errors or warnings", results.SummaryText())
} else {
assert.Equal(t, fmt.Sprintf("Linter results for projects: %v ERRORS, %v WARNINGS", testTable.expectedErrorCount, testTable.expectedWarningCount), results.SummaryText())
}
}
}
func TestWriteReport(t *testing.T) {
flags := test.ConfigurationFlags()
reportFolderPathString, err := ioutil.TempDir("", "arduino-lint-result-TestWriteReport")
require.Nil(t, err)
defer os.RemoveAll(reportFolderPathString) // clean up
reportFolderPath := paths.New(reportFolderPathString)
reportFilePath := reportFolderPath.Join("report-file.json")
_, err = reportFilePath.Create() // Create file using the report folder path.
require.Nil(t, err)
flags.Set("report-file", reportFilePath.Join("report-file.json").String())
require.Nil(t, configuration.Initialize(flags, projectPaths))
assert.Error(t, Results.WriteReport(), "Parent folder creation should fail due to a collision with an existing file at that path")
reportFilePath = reportFolderPath.Join("report-file-subfolder", "report-file-subsubfolder", "report-file.json")
flags.Set("report-file", reportFilePath.String())
require.Nil(t, configuration.Initialize(flags, projectPaths))
assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders")
reportFile, err := reportFilePath.Open()
require.Nil(t, err)
reportFileInfo, err := reportFile.Stat()
require.Nil(t, err)
reportFileBytes := make([]byte, reportFileInfo.Size())
_, err = reportFile.Read(reportFileBytes)
require.Nil(t, err)
assert.True(t, assert.ObjectsAreEqualValues(reportFileBytes, Results.jsonReportRaw()), "Report file contents are correct")
}