|
16 | 16 | package result
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "fmt" |
19 | 20 | "io/ioutil"
|
20 | 21 | "os"
|
21 | 22 | "testing"
|
22 | 23 |
|
| 24 | + "github.com/arduino/arduino-check/check/checkconfigurations" |
| 25 | + "github.com/arduino/arduino-check/check/checklevel" |
| 26 | + "github.com/arduino/arduino-check/check/checkresult" |
23 | 27 | "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" |
24 | 31 | "github.com/arduino/arduino-check/util/test"
|
25 | 32 | "github.com/arduino/go-paths-helper"
|
26 | 33 | "github.com/stretchr/testify/assert"
|
27 | 34 | "github.com/stretchr/testify/require"
|
28 | 35 | )
|
29 | 36 |
|
| 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 | + |
30 | 243 | func TestWriteReport(t *testing.T) {
|
31 | 244 | flags := test.ConfigurationFlags()
|
32 | 245 |
|
|
0 commit comments