Skip to content

Commit 6dbedc6

Browse files
authored
Merge pull request #83 from arduino/per1234/show-compliance-mode-in-report
Show compliance mode in report
2 parents 41fc8f9 + 1793293 commit 6dbedc6

File tree

5 files changed

+248
-9
lines changed

5 files changed

+248
-9
lines changed

Diff for: configuration/checkmode/checkmode.go

+10
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,13 @@ func Modes(defaultCheckModes map[projecttype.Type]map[Type]bool, customCheckMode
8686

8787
return checkModes
8888
}
89+
90+
func Compliance(checkModes map[Type]bool) string {
91+
for key, value := range checkModes {
92+
if value && (key == Strict || key == Specification || key == Permissive) {
93+
return key.String()
94+
}
95+
}
96+
97+
panic(fmt.Errorf("Unrecognized compliance configuration"))
98+
}

Diff for: configuration/checkmode/checkmode_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,21 @@ func TestMode(t *testing.T) {
5252
mergedCheckModes = Modes(defaultCheckModes, customCheckModes, testProjectType)
5353
assert.Equal(t, customCheckModes[testCheckMode], mergedCheckModes[testCheckMode], "Should be set to custom value")
5454
}
55+
56+
func TestCompliance(t *testing.T) {
57+
checkModes := map[Type]bool{
58+
Strict: false,
59+
Specification: false,
60+
Permissive: false,
61+
}
62+
63+
assert.Panics(t, func() { Compliance(checkModes) })
64+
checkModes[Strict] = true
65+
assert.Equal(t, Strict.String(), Compliance(checkModes))
66+
checkModes[Strict] = false
67+
checkModes[Specification] = true
68+
assert.Equal(t, Specification.String(), Compliance(checkModes))
69+
checkModes[Specification] = false
70+
checkModes[Permissive] = true
71+
assert.Equal(t, Permissive.String(), Compliance(checkModes))
72+
}

Diff for: configuration/configuration.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
113113
}
114114

115115
logrus.WithFields(logrus.Fields{
116-
"compliance strict mode": customCheckModes[checkmode.Strict],
117-
"compliance specification mode": customCheckModes[checkmode.Specification],
118-
"compliance permissive mode": customCheckModes[checkmode.Permissive],
116+
"compliance": checkmode.Compliance(customCheckModes),
119117
"output format": OutputFormat(),
120118
"Library Manager submission mode": customCheckModes[checkmode.LibraryManagerSubmission],
121119
"Library Manager update mode": customCheckModes[checkmode.LibraryManagerIndexed],

Diff for: result/result.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ type projectReportType struct {
5858
}
5959

6060
type projectConfigurationReportType struct {
61-
Permissive bool `json:"permissive"`
62-
LibraryManagerSubmit bool `json:"libraryManagerSubmit"`
63-
LibraryManagerUpdate bool `json:"libraryManagerUpdate"`
64-
Official bool `json:"official"`
61+
Compliance string `json:"compliance"`
62+
LibraryManagerSubmit bool `json:"libraryManagerSubmit"`
63+
LibraryManagerUpdate bool `json:"libraryManagerUpdate"`
64+
Official bool `json:"official"`
6565
}
6666

6767
type checkReportType struct {
@@ -129,8 +129,8 @@ func (results *Type) Record(checkedProject project.Type, checkConfiguration chec
129129
Path: checkedProject.Path,
130130
ProjectType: checkedProject.ProjectType.String(),
131131
Configuration: projectConfigurationReportType{
132-
Permissive: configuration.CheckModes(checkedProject.ProjectType)[checkmode.Permissive],
133-
LibraryManagerSubmit: configuration.CheckModes(checkedProject.ProjectType)[checkmode.Permissive],
132+
Compliance: checkmode.Compliance(configuration.CheckModes(checkedProject.ProjectType)),
133+
LibraryManagerSubmit: configuration.CheckModes(checkedProject.ProjectType)[checkmode.LibraryManagerSubmission],
134134
LibraryManagerUpdate: configuration.CheckModes(checkedProject.ProjectType)[checkmode.LibraryManagerIndexed],
135135
Official: configuration.CheckModes(checkedProject.ProjectType)[checkmode.Official],
136136
},

Diff for: result/result_test.go

+213
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,230 @@
1616
package result
1717

1818
import (
19+
"fmt"
1920
"io/ioutil"
2021
"os"
2122
"testing"
2223

24+
"github.com/arduino/arduino-check/check/checkconfigurations"
25+
"github.com/arduino/arduino-check/check/checklevel"
26+
"github.com/arduino/arduino-check/check/checkresult"
2327
"github.com/arduino/arduino-check/configuration"
28+
"github.com/arduino/arduino-check/configuration/checkmode"
29+
"github.com/arduino/arduino-check/project"
30+
"github.com/arduino/arduino-check/project/projecttype"
2431
"github.com/arduino/arduino-check/util/test"
2532
"github.com/arduino/go-paths-helper"
2633
"github.com/stretchr/testify/assert"
2734
"github.com/stretchr/testify/require"
2835
)
2936

37+
func TestInitialize(t *testing.T) {
38+
flags := test.ConfigurationFlags()
39+
flags.Set("project-type", "sketch")
40+
flags.Set("recursive", "false")
41+
workingDirectoryPath, err := os.Getwd() // A convenient path that is guaranteed to exist.
42+
require.Nil(t, err)
43+
44+
err = configuration.Initialize(flags, []string{workingDirectoryPath})
45+
require.Nil(t, err)
46+
var results Type
47+
results.Initialize()
48+
fmt.Printf("paths: %s", configuration.TargetPaths())
49+
assert.Equal(t, paths.NewPathList(workingDirectoryPath), results.Configuration.Paths)
50+
assert.Equal(t, projecttype.Sketch.String(), results.Configuration.ProjectType)
51+
assert.False(t, results.Configuration.Recursive)
52+
}
53+
54+
func TestRecord(t *testing.T) {
55+
checkedProject := project.Type{
56+
Path: paths.New("/foo/bar"),
57+
ProjectType: projecttype.Sketch,
58+
SuperprojectType: projecttype.Library,
59+
}
60+
61+
var results Type
62+
checkConfiguration := checkconfigurations.Configurations()[0]
63+
checkOutput := "foo"
64+
summaryText := results.Record(checkedProject, checkConfiguration, checkresult.Pass, checkOutput)
65+
assert.Equal(t, fmt.Sprintf("%s\n", checkresult.Pass), summaryText)
66+
summaryText = results.Record(checkedProject, checkConfiguration, checkresult.NotRun, checkOutput)
67+
assert.Equal(t, fmt.Sprintf("%s\n%s: %s\n", checkresult.NotRun, checklevel.Notice, checkOutput), summaryText)
68+
summaryText = results.Record(checkedProject, checkConfiguration, checkresult.Fail, checkOutput)
69+
assert.Equal(t, fmt.Sprintf("%s\n%s: %s\n", checkresult.Fail, checklevel.Error, message(checkConfiguration.MessageTemplate, checkOutput)), summaryText)
70+
71+
checkResult := checkresult.Pass
72+
results = Type{}
73+
74+
results.Record(checkedProject, checkConfiguration, checkResult, checkOutput)
75+
projectReport := results.Projects[0]
76+
assert.Equal(t, checkedProject.Path, projectReport.Path)
77+
assert.Equal(t, checkedProject.ProjectType.String(), projectReport.ProjectType)
78+
projectConfigurationReport := projectReport.Configuration
79+
assert.Equal(t, checkmode.Compliance(configuration.CheckModes(checkedProject.ProjectType)), projectConfigurationReport.Compliance)
80+
assert.Equal(t, configuration.CheckModes(checkedProject.ProjectType)[checkmode.LibraryManagerSubmission], projectConfigurationReport.LibraryManagerSubmit)
81+
assert.Equal(t, configuration.CheckModes(checkedProject.ProjectType)[checkmode.LibraryManagerIndexed], projectConfigurationReport.LibraryManagerUpdate)
82+
assert.Equal(t, configuration.CheckModes(checkedProject.ProjectType)[checkmode.Official], projectConfigurationReport.Official)
83+
84+
checkReport := projectReport.Checks[0]
85+
assert.Equal(t, checkConfiguration.Category, checkReport.Category)
86+
assert.Equal(t, checkConfiguration.Subcategory, checkReport.Subcategory)
87+
assert.Equal(t, checkConfiguration.ID, checkReport.ID)
88+
assert.Equal(t, checkConfiguration.Brief, checkReport.Brief)
89+
assert.Equal(t, checkConfiguration.Description, checkReport.Description)
90+
assert.Equal(t, checkResult.String(), checkReport.Result)
91+
checkLevel, _ := checklevel.CheckLevel(checkConfiguration)
92+
assert.Equal(t, checkLevel.String(), checkReport.Level)
93+
assert.Equal(t, message(checkConfiguration.MessageTemplate, checkOutput), checkReport.Message)
94+
95+
assert.Len(t, results.Projects, 1)
96+
previousProjectPath := checkedProject.Path
97+
checkedProject.Path = paths.New("/foo/baz")
98+
results.Record(checkedProject, checkConfiguration, checkResult, checkOutput)
99+
assert.Len(t, results.Projects, 2)
100+
101+
assert.Len(t, results.Projects[0].Checks, 1)
102+
checkedProject.Path = previousProjectPath
103+
results.Record(checkedProject, checkconfigurations.Configurations()[1], checkResult, checkOutput)
104+
assert.Len(t, results.Projects[0].Checks, 2)
105+
}
106+
107+
func TestAddProjectSummary(t *testing.T) {
108+
checkedProject := project.Type{
109+
Path: paths.New("/foo/bar"),
110+
ProjectType: projecttype.Sketch,
111+
SuperprojectType: projecttype.Library,
112+
}
113+
114+
testTables := []struct {
115+
results []checkresult.Type
116+
levels []checklevel.Type
117+
expectedPass bool
118+
expectedWarningCount int
119+
expectedErrorCount int
120+
}{
121+
{
122+
[]checkresult.Type{checkresult.Pass, checkresult.Pass},
123+
[]checklevel.Type{checklevel.Info, checklevel.Info},
124+
true,
125+
0,
126+
0,
127+
},
128+
{
129+
[]checkresult.Type{checkresult.Pass, checkresult.Fail},
130+
[]checklevel.Type{checklevel.Info, checklevel.Warning},
131+
true,
132+
1,
133+
0,
134+
},
135+
{
136+
[]checkresult.Type{checkresult.Fail, checkresult.Fail},
137+
[]checklevel.Type{checklevel.Error, checklevel.Warning},
138+
false,
139+
1,
140+
1,
141+
},
142+
}
143+
144+
for _, testTable := range testTables {
145+
var results Type
146+
for _, result := range testTable.results {
147+
results.Record(checkedProject, checkconfigurations.Configurations()[0], result, "")
148+
}
149+
for checkIndex, level := range testTable.levels {
150+
results.Projects[0].Checks[checkIndex].Level = level.String()
151+
}
152+
results.AddProjectSummary(checkedProject)
153+
assert.Equal(t, testTable.expectedPass, results.Projects[0].Summary.Pass)
154+
assert.Equal(t, testTable.expectedWarningCount, results.Projects[0].Summary.WarningCount)
155+
assert.Equal(t, testTable.expectedErrorCount, results.Projects[0].Summary.ErrorCount)
156+
assert.Equal(t, fmt.Sprintf("\nFinished checking project. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v\n\n", testTable.expectedWarningCount, testTable.expectedErrorCount, testTable.expectedPass), results.ProjectSummaryText(checkedProject))
157+
}
158+
}
159+
160+
func TestAddSummary(t *testing.T) {
161+
checkedProject := project.Type{
162+
Path: paths.New("/foo/bar"),
163+
ProjectType: projecttype.Sketch,
164+
SuperprojectType: projecttype.Library,
165+
}
166+
167+
testTables := []struct {
168+
projectSummaries []summaryReportType
169+
expectedPass bool
170+
expectedWarningCount int
171+
expectedErrorCount int
172+
}{
173+
{
174+
[]summaryReportType{
175+
{
176+
Pass: true,
177+
WarningCount: 0,
178+
ErrorCount: 0,
179+
},
180+
{
181+
Pass: true,
182+
WarningCount: 0,
183+
ErrorCount: 0,
184+
},
185+
},
186+
true,
187+
0,
188+
0,
189+
},
190+
{
191+
[]summaryReportType{
192+
{
193+
Pass: true,
194+
WarningCount: 1,
195+
ErrorCount: 0,
196+
},
197+
{
198+
Pass: true,
199+
WarningCount: 2,
200+
ErrorCount: 0,
201+
},
202+
},
203+
true,
204+
3,
205+
0,
206+
},
207+
{
208+
[]summaryReportType{
209+
{
210+
Pass: false,
211+
WarningCount: 1,
212+
ErrorCount: 0,
213+
},
214+
{
215+
Pass: true,
216+
WarningCount: 2,
217+
ErrorCount: 2,
218+
},
219+
},
220+
false,
221+
3,
222+
2,
223+
},
224+
}
225+
226+
for _, testTable := range testTables {
227+
var results Type
228+
for projectIndex, projectSummary := range testTable.projectSummaries {
229+
checkedProject.Path = paths.New(fmt.Sprintf("/foo/bar%v", projectIndex)) // Use a unique path to generate a new project report.
230+
results.Record(checkedProject, checkconfigurations.Configurations()[0], checkresult.Pass, "")
231+
results.AddProjectSummary(checkedProject)
232+
results.Projects[projectIndex].Summary = projectSummary
233+
}
234+
results.AddSummary()
235+
assert.Equal(t, testTable.expectedPass, results.Summary.Pass)
236+
assert.Equal(t, testTable.expectedPass, results.Passed())
237+
assert.Equal(t, testTable.expectedWarningCount, results.Summary.WarningCount)
238+
assert.Equal(t, testTable.expectedErrorCount, results.Summary.ErrorCount)
239+
assert.Equal(t, fmt.Sprintf("Finished checking projects. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v\n", testTable.expectedWarningCount, testTable.expectedErrorCount, testTable.expectedPass), results.SummaryText())
240+
}
241+
}
242+
30243
func TestWriteReport(t *testing.T) {
31244
flags := test.ConfigurationFlags()
32245

0 commit comments

Comments
 (0)