2017-09-11 23:40:37 +00:00
|
|
|
package hcl
|
2017-05-19 02:01:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2018-07-28 21:42:53 +00:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-05-19 02:01:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiagnosticTextWriter(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Input *Diagnostic
|
|
|
|
Want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&Diagnostic{
|
|
|
|
Severity: DiagError,
|
|
|
|
Summary: "Splines not reticulated",
|
|
|
|
Detail: "All splines must be pre-reticulated.",
|
|
|
|
Subject: &Range{
|
|
|
|
Start: Pos{
|
|
|
|
Byte: 0,
|
|
|
|
Column: 1,
|
|
|
|
Line: 1,
|
|
|
|
},
|
|
|
|
End: Pos{
|
|
|
|
Byte: 3,
|
|
|
|
Column: 4,
|
|
|
|
Line: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Error: Splines not reticulated
|
|
|
|
|
2017-05-20 01:56:39 +00:00
|
|
|
on line 1, in hardcoded-context:
|
2017-05-19 02:01:41 +00:00
|
|
|
1: foo = 1
|
|
|
|
|
|
|
|
All splines must be pre-reticulated.
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Diagnostic{
|
|
|
|
Severity: DiagError,
|
|
|
|
Summary: "Unsupported attribute",
|
|
|
|
Detail: `"baz" is not a supported top-level attribute. Did you mean "bam"?`,
|
|
|
|
Subject: &Range{
|
|
|
|
Start: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 16,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 1,
|
|
|
|
Line: 3,
|
|
|
|
},
|
|
|
|
End: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 19,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 4,
|
|
|
|
Line: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Error: Unsupported attribute
|
|
|
|
|
2017-05-20 01:56:39 +00:00
|
|
|
on line 3, in hardcoded-context:
|
2017-05-19 02:01:41 +00:00
|
|
|
3: baz = 3
|
|
|
|
|
|
|
|
"baz" is not a supported top-level
|
|
|
|
attribute. Did you mean "bam"?
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Diagnostic{
|
|
|
|
Severity: DiagError,
|
|
|
|
Summary: "Unsupported attribute",
|
|
|
|
Detail: `"pizza" is not a supported attribute. Did you mean "pizzetta"?`,
|
|
|
|
Subject: &Range{
|
|
|
|
Start: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 42,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 3,
|
|
|
|
Line: 5,
|
|
|
|
},
|
|
|
|
End: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 47,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 8,
|
|
|
|
Line: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// This is actually not a great example of a context, but is here to test
|
|
|
|
// whether we're able to show a multi-line context when needed.
|
|
|
|
Context: &Range{
|
|
|
|
Start: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 24,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 1,
|
|
|
|
Line: 4,
|
|
|
|
},
|
|
|
|
End: Pos{
|
2018-01-14 20:07:33 +00:00
|
|
|
Byte: 60,
|
2017-05-19 02:01:41 +00:00
|
|
|
Column: 2,
|
|
|
|
Line: 6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Error: Unsupported attribute
|
|
|
|
|
2017-05-20 01:56:39 +00:00
|
|
|
on line 5, in hardcoded-context:
|
2017-05-19 02:01:41 +00:00
|
|
|
4: block "party" {
|
|
|
|
5: pizza = "cheese"
|
|
|
|
6: }
|
|
|
|
|
|
|
|
"pizza" is not a supported attribute.
|
|
|
|
Did you mean "pizzetta"?
|
|
|
|
|
2018-07-28 21:42:53 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Diagnostic{
|
|
|
|
Severity: DiagError,
|
|
|
|
Summary: "Test of including relevant variable values",
|
|
|
|
Detail: `This diagnostic includes an expression and an evalcontext.`,
|
|
|
|
Subject: &Range{
|
|
|
|
Start: Pos{
|
|
|
|
Byte: 42,
|
|
|
|
Column: 3,
|
|
|
|
Line: 5,
|
|
|
|
},
|
|
|
|
End: Pos{
|
|
|
|
Byte: 47,
|
|
|
|
Column: 8,
|
|
|
|
Line: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Expression: &diagnosticTestExpr{
|
|
|
|
vars: []Traversal{
|
|
|
|
{
|
|
|
|
TraverseRoot{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraverseRoot{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
TraverseAttr{
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraverseRoot{
|
|
|
|
Name: "missing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraverseRoot{
|
|
|
|
Name: "boz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EvalContext: &EvalContext{
|
|
|
|
parent: &EvalContext{
|
|
|
|
Variables: map[string]cty.Value{
|
|
|
|
"foo": cty.StringVal("foo value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variables: map[string]cty.Value{
|
|
|
|
"bar": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"baz": cty.ListValEmpty(cty.String),
|
|
|
|
}),
|
|
|
|
"boz": cty.NumberIntVal(5),
|
|
|
|
"unused": cty.True,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
`Error: Test of including relevant variable values
|
|
|
|
|
|
|
|
on line 5, in hardcoded-context:
|
|
|
|
5: pizza = "cheese"
|
|
|
|
|
|
|
|
with bar.baz as empty list of string,
|
|
|
|
boz as 5,
|
|
|
|
foo as "foo value".
|
|
|
|
|
|
|
|
This diagnostic includes an expression
|
|
|
|
and an evalcontext.
|
|
|
|
|
2017-05-19 02:01:41 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-20 01:56:39 +00:00
|
|
|
files := map[string]*File{
|
|
|
|
"": &File{
|
|
|
|
Bytes: []byte(testDiagnosticTextWriterSource),
|
|
|
|
Nav: &diagnosticTestNav{},
|
|
|
|
},
|
2017-05-19 02:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
|
|
|
bwr := &bytes.Buffer{}
|
2017-05-20 01:56:39 +00:00
|
|
|
dwr := NewDiagnosticTextWriter(bwr, files, 40, false)
|
2017-05-19 02:01:41 +00:00
|
|
|
err := dwr.WriteDiagnostic(test.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
got := bwr.String()
|
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf("wrong result\n\ngot:\n%swant:\n%s", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testDiagnosticTextWriterSource = `foo = 1
|
|
|
|
bar = 2
|
|
|
|
baz = 3
|
|
|
|
block "party" {
|
|
|
|
pizza = "cheese"
|
|
|
|
}
|
|
|
|
`
|
2017-05-20 01:56:39 +00:00
|
|
|
|
|
|
|
type diagnosticTestNav struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tn *diagnosticTestNav) ContextString(offset int) string {
|
|
|
|
return "hardcoded-context"
|
|
|
|
}
|
2018-07-28 21:42:53 +00:00
|
|
|
|
|
|
|
type diagnosticTestExpr struct {
|
|
|
|
vars []Traversal
|
|
|
|
staticExpr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *diagnosticTestExpr) Variables() []Traversal {
|
|
|
|
return e.vars
|
|
|
|
}
|