add info block

This commit is contained in:
Shreyas Goenka 2024-09-24 17:29:10 +02:00
parent fa6b68c410
commit ccbd199422
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 90 additions and 6 deletions

View File

@ -14,13 +14,10 @@ import (
)
// Steps:
// 1. Return info diag here if convention not followed
// 2. Add unit test for this mutator that convention is followed. Also add mutators for the dynamic extensions computation.
// 3. Add INFO rendering to the validate command
// 4. Add unit test that the INFO rendering is correct
// 5. Manually test the info diag.
// TODO: Should we detect and enforce this convention for .yaml files as well?
// TODO: Since we are skipping environemnts here, we should return a warning
// if environemnts is used (is that already the case?). And explain in the PR that

View File

@ -56,6 +56,20 @@ const warningTemplate = `{{ "Warning" | yellow }}: {{ .Summary }}
`
const infoTemplate = `{{ "Info" | blue }}: {{ .Summary }}
{{- range $index, $element := .Paths }}
{{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.String | green }}
{{- end }}
{{- range $index, $element := .Locations }}
{{ if eq $index 0 }}in {{else}} {{ end}}{{ $element.String | cyan }}
{{- end }}
{{- if .Detail }}
{{ .Detail }}
{{- end }}
`
const summaryTemplate = `{{- if .Name -}}
Name: {{ .Name | bold }}
{{- if .Target }}
@ -94,6 +108,9 @@ func buildTrailer(diags diag.Diagnostics) string {
if warnings := len(diags.Filter(diag.Warning)); warnings > 0 {
parts = append(parts, color.YellowString(pluralize(warnings, "warning", "warnings")))
}
if infos := len(diags.Filter(diag.Info)); infos > 0 {
parts = append(parts, color.BlueString(pluralize(infos, "info", "infos")))
}
if len(parts) > 0 {
return fmt.Sprintf("Found %s", strings.Join(parts, " and "))
} else {
@ -130,6 +147,7 @@ func renderSummaryTemplate(out io.Writer, b *bundle.Bundle, diags diag.Diagnosti
func renderDiagnostics(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics) error {
errorT := template.Must(template.New("error").Funcs(renderFuncMap).Parse(errorTemplate))
warningT := template.Must(template.New("warning").Funcs(renderFuncMap).Parse(warningTemplate))
infoT := template.Must(template.New("info").Funcs(renderFuncMap).Parse(infoTemplate))
// Print errors and warnings.
for _, d := range diags {
@ -139,6 +157,8 @@ func renderDiagnostics(out io.Writer, b *bundle.Bundle, diags diag.Diagnostics)
t = errorT
case diag.Warning:
t = warningT
case diag.Info:
t = infoT
}
for i := range d.Locations {

View File

@ -45,6 +45,19 @@ func TestRenderTextOutput(t *testing.T) {
"\n" +
"Found 1 error\n",
},
{
name: "nil bundle and 1 info",
diags: diag.Diagnostics{
{
Severity: diag.Info,
Summary: "info",
},
},
opts: RenderOptions{RenderSummaryTable: true},
expected: "Info: info\n" +
"\n" +
"Found 1 info\n",
},
{
name: "bundle during 'load' and 1 error",
bundle: loadingBundle,
@ -84,7 +97,7 @@ func TestRenderTextOutput(t *testing.T) {
"Found 2 warnings\n",
},
{
name: "bundle during 'load' and 2 errors, 1 warning with details",
name: "bundle during 'load' and 2 errors, 1 warning and 1 info with details",
bundle: loadingBundle,
diags: diag.Diagnostics{
diag.Diagnostic{
@ -105,6 +118,12 @@ func TestRenderTextOutput(t *testing.T) {
Detail: "detail (3)",
Locations: []dyn.Location{{File: "foo.py", Line: 3, Column: 1}},
},
diag.Diagnostic{
Severity: diag.Info,
Summary: "info (4)",
Detail: "detail (4)",
Locations: []dyn.Location{{File: "foo.py", Line: 4, Column: 1}},
},
},
opts: RenderOptions{RenderSummaryTable: true},
expected: "Error: error (1)\n" +
@ -122,10 +141,15 @@ func TestRenderTextOutput(t *testing.T) {
"\n" +
"detail (3)\n" +
"\n" +
"Info: info (4)\n" +
" in foo.py:4:1\n" +
"\n" +
"detail (4)\n" +
"\n" +
"Name: test-bundle\n" +
"Target: test-target\n" +
"\n" +
"Found 2 errors and 1 warning\n",
"Found 2 errors and 1 warning and 1 info\n",
},
{
name: "bundle during 'init'",
@ -158,7 +182,7 @@ func TestRenderTextOutput(t *testing.T) {
"Validation OK!\n",
},
{
name: "nil bundle without summary with 1 error and 1 warning",
name: "nil bundle without summary with 1 error, 1 warning and 1 info",
bundle: nil,
diags: diag.Diagnostics{
diag.Diagnostic{
@ -173,6 +197,12 @@ func TestRenderTextOutput(t *testing.T) {
Detail: "detail (2)",
Locations: []dyn.Location{{File: "foo.py", Line: 3, Column: 1}},
},
diag.Diagnostic{
Severity: diag.Info,
Summary: "info (3)",
Detail: "detail (3)",
Locations: []dyn.Location{{File: "foo.py", Line: 5, Column: 1}},
},
},
opts: RenderOptions{RenderSummaryTable: false},
expected: "Error: error (1)\n" +
@ -184,6 +214,11 @@ func TestRenderTextOutput(t *testing.T) {
" in foo.py:3:1\n" +
"\n" +
"detail (2)\n" +
"\n" +
"Info: info (3)\n" +
" in foo.py:5:1\n" +
"\n" +
"detail (3)\n" +
"\n",
},
}
@ -304,6 +339,38 @@ func TestRenderDiagnostics(t *testing.T) {
"\n" +
"'name' is required\n\n",
},
{
name: "error with multiple paths and locations",
diags: diag.Diagnostics{
{
Severity: diag.Info,
Summary: "summary",
Detail: "detail",
Paths: []dyn.Path{
dyn.MustPathFromString("resources.jobs.xxx"),
dyn.MustPathFromString("resources.jobs.yyy"),
},
Locations: []dyn.Location{
{
File: "foo.yaml",
Line: 1,
Column: 2,
},
{
File: "bar.yaml",
Line: 3,
Column: 4,
},
},
},
},
expected: "Info: summary\n" +
" at resources.jobs.xxx\n" +
" resources.jobs.yyy\n" +
" in foo.yaml:1:2\n" +
" bar.yaml:3:4\n\n" +
"detail\n\n",
},
}
for _, tc := range testCases {