Skip to content

Commit d6fbfe1

Browse files
authored
Merge pull request #99 from arduino/per1234/improve-output
Improve output
2 parents 8d0714b + 23d32fd commit d6fbfe1

File tree

9 files changed

+49
-39
lines changed

9 files changed

+49
-39
lines changed

Diff for: check/check.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package check
1818

1919
import (
2020
"fmt"
21-
"os"
2221

2322
"github.com/arduino/arduino-check/check/checkconfigurations"
2423
"github.com/arduino/arduino-check/check/checkdata"
@@ -33,15 +32,14 @@ import (
3332

3433
// RunChecks runs all checks for the given project and outputs the results.
3534
func RunChecks(project project.Type) {
36-
feedback.Printf("Checking %s in %s\n", project.ProjectType, project.Path)
35+
feedback.Printf("\nChecking %s in %s\n", project.ProjectType, project.Path)
3736

3837
checkdata.Initialize(project, configuration.SchemasPath())
3938

4039
for _, checkConfiguration := range checkconfigurations.Configurations() {
4140
runCheck, err := shouldRun(checkConfiguration, project)
4241
if err != nil {
43-
feedback.Errorf("Error while determining whether to run check: %v", err)
44-
os.Exit(1)
42+
panic(err)
4543
}
4644

4745
if !runCheck {
@@ -55,15 +53,9 @@ func RunChecks(project project.Type) {
5553
checkResult, checkOutput := checkConfiguration.CheckFunction()
5654
reportText := result.Results.Record(project, checkConfiguration, checkResult, checkOutput)
5755
if (checkResult == checkresult.Fail) || configuration.Verbose() {
58-
feedback.Print(reportText)
56+
feedback.Println(reportText)
5957
}
6058
}
61-
62-
// Checks are finished for this project, so summarize its check results in the report.
63-
result.Results.AddProjectSummary(project)
64-
65-
// Print the project check results summary.
66-
feedback.Print(result.Results.ProjectSummaryText(project))
6759
}
6860

6961
// shouldRun returns whether a given check should be run for the given project under the current tool configuration.

Diff for: check/checkdata/schema/parsevalidationresult.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ func validationErrorSchemaPointerValueMatch(
189189
schemasPath *paths.Path,
190190
) bool {
191191
marshalledSchemaPointerValue, err := json.Marshal(schemaPointerValue(schemaURL, schemaPointer, schemasPath))
192-
logrus.Tracef("Checking schema pointer value: %s match with regexp: %s", marshalledSchemaPointerValue, schemaPointerValueRegexp)
193192
if err != nil {
194193
panic(err)
195194
}
195+
logrus.Tracef("Checking schema pointer value: %s match with regexp: %s", marshalledSchemaPointerValue, schemaPointerValueRegexp)
196196
return schemaPointerValueRegexp.Match(marshalledSchemaPointerValue)
197197
}
198198

Diff for: command/command.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ func ArduinoCheck(rootCommand *cobra.Command, cliArguments []string) {
6767

6868
for _, project := range projects {
6969
check.RunChecks(project)
70+
71+
// Checks are finished for this project, so summarize its check results in the report.
72+
result.Results.AddProjectSummary(project)
73+
74+
// Print the project check results summary.
75+
feedback.Printf("\n%s\n", result.Results.ProjectSummaryText(project))
7076
}
7177

7278
// All projects have been checked, so summarize their check results in the report.
@@ -75,7 +81,7 @@ func ArduinoCheck(rootCommand *cobra.Command, cliArguments []string) {
7581
if configuration.OutputFormat() == outputformat.Text {
7682
if len(projects) > 1 {
7783
// There are multiple projects, print the summary of check results for all projects.
78-
fmt.Print(result.Results.SummaryText())
84+
fmt.Printf("\n%s\n", result.Results.SummaryText())
7985
}
8086
} else {
8187
// Print the complete JSON formatted report.

Diff for: configuration/configuration.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
5656
}
5757
}
5858

59+
logrus.SetOutput(defaultLogOutput)
60+
5961
if logFormatString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_FORMAT"); ok {
6062
logFormat, err := logFormatFromString(logFormatString)
6163
if err != nil {
6264
return fmt.Errorf("--log-format flag value %s not valid", logFormatString)
6365
}
6466
logrus.SetFormatter(logFormat)
67+
logrus.SetOutput(os.Stderr) // Enable log output.
6568
}
6669

6770
if logLevelString, ok := os.LookupEnv("ARDUINO_CHECK_LOG_LEVEL"); ok {
@@ -70,8 +73,7 @@ func Initialize(flags *pflag.FlagSet, projectPaths []string) error {
7073
return fmt.Errorf("--log-level flag value %s not valid", logLevelString)
7174
}
7275
logrus.SetLevel(logLevel)
73-
} else {
74-
logrus.SetLevel(defaultLogLevel)
76+
logrus.SetOutput(os.Stderr) // Enable log output.
7577
}
7678

7779
superprojectTypeFilterString, _ := flags.GetString("project-type")

Diff for: configuration/configuration_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func TestInitializeLogFormat(t *testing.T) {
120120

121121
func TestInitializeLogLevel(t *testing.T) {
122122
require.Nil(t, Initialize(test.ConfigurationFlags(), projectPaths))
123-
assert.Equal(t, defaultLogLevel, logrus.GetLevel(), "Default level")
124123

125124
os.Setenv("ARDUINO_CHECK_LOG_LEVEL", "foo")
126125
assert.Error(t, Initialize(test.ConfigurationFlags(), projectPaths), "Invalid level")

Diff for: configuration/defaults.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package configuration
1818
// The default configuration settings.
1919

2020
import (
21+
"io/ioutil"
22+
2123
"github.com/arduino/arduino-check/configuration/checkmode"
2224
"github.com/arduino/arduino-check/project/projecttype"
23-
"github.com/sirupsen/logrus"
2425
)
2526

2627
// Default check modes for each superproject type.
@@ -60,4 +61,4 @@ var defaultCheckModes = map[projecttype.Type]map[checkmode.Type]bool{
6061
},
6162
}
6263

63-
var defaultLogLevel = logrus.FatalLevel
64+
var defaultLogOutput = ioutil.Discard // Default to no log output.

Diff for: result/feedback/feedback.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,56 @@ package feedback
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/arduino/arduino-check/configuration"
2324
"github.com/arduino/arduino-check/result/outputformat"
2425
"github.com/sirupsen/logrus"
2526
)
2627

28+
// VerbosePrintln behaves like Println but only prints when verbosity is enabled.
29+
func VerbosePrintln(v ...interface{}) {
30+
VerbosePrint(v...)
31+
VerbosePrint("\n")
32+
}
33+
2734
// VerbosePrintf behaves like Printf but only prints when verbosity is enabled.
2835
func VerbosePrintf(format string, v ...interface{}) {
2936
VerbosePrint(fmt.Sprintf(format, v...))
3037
}
3138

3239
// VerbosePrint behaves like Print but only prints when verbosity is enabled.
33-
func VerbosePrint(message string) {
40+
func VerbosePrint(v ...interface{}) {
3441
if configuration.Verbose() && (configuration.OutputFormat() == outputformat.Text) {
35-
Printf(message)
42+
Print(v...)
3643
}
3744
}
3845

46+
// Println behaves like fmt.Println but only prints when output format is set to `text`.
47+
func Println(v ...interface{}) {
48+
Print(v...)
49+
Print("\n")
50+
}
51+
3952
// Printf behaves like fmt.Printf but only prints when output format is set to `text`.
4053
func Printf(format string, v ...interface{}) {
4154
Print(fmt.Sprintf(format, v...))
4255
}
4356

4457
// Print behaves like fmt.Print but only prints when output format is set to `text`.
45-
func Print(message string) {
58+
func Print(v ...interface{}) {
4659
if configuration.OutputFormat() == outputformat.Text {
47-
fmt.Printf(message)
60+
fmt.Print(v...)
4861
}
4962
}
5063

51-
// Errorf behaves like fmt.Printf but also logs the error.
64+
// Errorf behaves like fmt.Printf but adds a newline and also logs the error.
5265
func Errorf(format string, v ...interface{}) {
5366
Error(fmt.Sprintf(format, v...))
5467
}
5568

56-
// Error behaves like fmt.Print but also logs the error.
57-
func Error(errorMessage string) {
58-
fmt.Printf(errorMessage)
59-
logrus.Error(fmt.Sprint(errorMessage))
69+
// Error behaves like fmt.Print but adds a newline and also logs the error.
70+
func Error(v ...interface{}) {
71+
fmt.Fprintln(os.Stderr, v...)
72+
logrus.Error(fmt.Sprint(v...))
6073
}

Diff for: result/result.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"html/template"
24-
"os"
2524

2625
"github.com/arduino/arduino-check/check/checkconfigurations"
2726
"github.com/arduino/arduino-check/check/checklevel"
2827
"github.com/arduino/arduino-check/check/checkresult"
2928
"github.com/arduino/arduino-check/configuration"
3029
"github.com/arduino/arduino-check/configuration/checkmode"
3130
"github.com/arduino/arduino-check/project"
32-
"github.com/arduino/arduino-check/result/feedback"
3331
"github.com/arduino/go-paths-helper"
3432
)
3533

@@ -95,11 +93,10 @@ func (results *Type) Initialize() {
9593
func (results *Type) Record(checkedProject project.Type, checkConfiguration checkconfigurations.Type, checkResult checkresult.Type, checkOutput string) string {
9694
checkLevel, err := checklevel.CheckLevel(checkConfiguration, checkResult)
9795
if err != nil {
98-
feedback.Errorf("Error while determining check level: %v", err)
99-
os.Exit(1)
96+
panic(fmt.Errorf("Error while determining check level: %v", err))
10097
}
10198

102-
summaryText := fmt.Sprintf("Check %s result: %s\n", checkConfiguration.ID, checkResult)
99+
summaryText := fmt.Sprintf("Check %s result: %s", checkConfiguration.ID, checkResult)
103100

104101
checkMessage := ""
105102
if checkResult == checkresult.Fail {
@@ -112,7 +109,7 @@ func (results *Type) Record(checkedProject project.Type, checkConfiguration chec
112109

113110
// Add explanation of check result if present.
114111
if checkMessage != "" {
115-
summaryText += fmt.Sprintf("%s: %s\n", checkLevel, checkMessage)
112+
summaryText += fmt.Sprintf("\n%s: %s", checkLevel, checkMessage)
116113
}
117114

118115
reportExists, projectReportIndex := results.getProjectReportIndex(checkedProject.Path)
@@ -187,7 +184,7 @@ func (results Type) ProjectSummaryText(checkedProject project.Type) string {
187184
}
188185

189186
projectSummaryReport := results.Projects[projectReportIndex].Summary
190-
return fmt.Sprintf("\nFinished checking project. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v\n\n", projectSummaryReport.WarningCount, projectSummaryReport.ErrorCount, projectSummaryReport.Pass)
187+
return fmt.Sprintf("Finished checking project. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v", projectSummaryReport.WarningCount, projectSummaryReport.ErrorCount, projectSummaryReport.Pass)
191188
}
192189

193190
// AddSummary summarizes the check results for all projects and adds it to the report.
@@ -212,7 +209,7 @@ func (results *Type) AddSummary() {
212209

213210
// SummaryText returns a text summary of the cumulative check results.
214211
func (results Type) SummaryText() string {
215-
return fmt.Sprintf("Finished checking projects. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v\n", results.Summary.WarningCount, results.Summary.ErrorCount, results.Summary.Pass)
212+
return fmt.Sprintf("Finished checking projects. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v", results.Summary.WarningCount, results.Summary.ErrorCount, results.Summary.Pass)
216213
}
217214

218215
// JSONReport returns a JSON formatted report of checks on all projects.

Diff for: result/result_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ func TestRecord(t *testing.T) {
7575
checkConfiguration := checkconfigurations.Configurations()[0]
7676
checkOutput := "foo"
7777
summaryText := results.Record(checkedProject, checkConfiguration, checkresult.Fail, checkOutput)
78-
assert.Equal(t, fmt.Sprintf("Check %s result: %s\n%s: %s\n", checkConfiguration.ID, checkresult.Fail, checklevel.Error, message(checkConfiguration.MessageTemplate, checkOutput)), summaryText)
78+
assert.Equal(t, fmt.Sprintf("Check %s result: %s\n%s: %s", checkConfiguration.ID, checkresult.Fail, checklevel.Error, message(checkConfiguration.MessageTemplate, checkOutput)), summaryText)
7979
summaryText = results.Record(checkedProject, checkConfiguration, checkresult.NotRun, checkOutput)
80-
assert.Equal(t, fmt.Sprintf("Check %s result: %s\n%s: %s\n", checkConfiguration.ID, checkresult.NotRun, checklevel.Notice, checkOutput), summaryText, "Non-fail result should not use message")
80+
assert.Equal(t, fmt.Sprintf("Check %s result: %s\n%s: %s", checkConfiguration.ID, checkresult.NotRun, checklevel.Notice, checkOutput), summaryText, "Non-fail result should not use message")
8181
summaryText = results.Record(checkedProject, checkConfiguration, checkresult.Pass, "")
8282
assert.Equal(t, "", "", summaryText, "Non-failure result with no check function output should result in an empty summary")
8383

@@ -198,7 +198,7 @@ func TestAddProjectSummary(t *testing.T) {
198198
assert.Equal(t, testTable.expectedPass, results.Projects[0].Summary.Pass)
199199
assert.Equal(t, testTable.expectedWarningCount, results.Projects[0].Summary.WarningCount)
200200
assert.Equal(t, testTable.expectedErrorCount, results.Projects[0].Summary.ErrorCount)
201-
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))
201+
assert.Equal(t, fmt.Sprintf("Finished checking project. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v", testTable.expectedWarningCount, testTable.expectedErrorCount, testTable.expectedPass), results.ProjectSummaryText(checkedProject))
202202
}
203203
}
204204

@@ -281,7 +281,7 @@ func TestAddSummary(t *testing.T) {
281281
assert.Equal(t, testTable.expectedPass, results.Passed())
282282
assert.Equal(t, testTable.expectedWarningCount, results.Summary.WarningCount)
283283
assert.Equal(t, testTable.expectedErrorCount, results.Summary.ErrorCount)
284-
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())
284+
assert.Equal(t, fmt.Sprintf("Finished checking projects. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v", testTable.expectedWarningCount, testTable.expectedErrorCount, testTable.expectedPass), results.SummaryText())
285285
}
286286
}
287287

0 commit comments

Comments
 (0)