1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build go1.18
// +build go1.18
package hclsyntax
import (
"fmt"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
// This file contains some additional tests that only make sense when using
// a Go compiler which supports type parameters (Go 1.18 or later).
func TestExpressionDiagnosticExtra(t *testing.T) {
tests := []struct {
input string
ctx *hcl.EvalContext
assert func(t *testing.T, diags hcl.Diagnostics)
}{
// Errors for unknown function calls
{
"boop()",
&hcl.EvalContext{
Functions: map[string]function.Function{
"zap": function.New(&function.Spec{
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.DynamicVal, fmt.Errorf("the expected error")
},
}),
},
},
func(t *testing.T, diags hcl.Diagnostics) {
t.Helper()
for _, diag := range diags {
extra, ok := hcl.DiagnosticExtra[FunctionCallUnknownDiagExtra](diag)
if !ok {
continue
}
if got, want := extra.CalledFunctionName(), "boop"; got != want {
t.Errorf("wrong called function name %q; want %q", got, want)
}
ns := extra.CalledFunctionNamespace()
if ns != "" {
t.Fatal("expected no namespace, got", ns)
}
return
}
t.Fatalf("None of the returned diagnostics implement FunctionCallUnknownDiagExtra\n%s", diags.Error())
},
},
{
"ns::source::boop()",
&hcl.EvalContext{
Functions: map[string]function.Function{
"zap": function.New(&function.Spec{
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.DynamicVal, fmt.Errorf("the expected error")
},
}),
},
},
func(t *testing.T, diags hcl.Diagnostics) {
t.Helper()
for _, diag := range diags {
extra, ok := hcl.DiagnosticExtra[FunctionCallUnknownDiagExtra](diag)
if !ok {
continue
}
if got, want := extra.CalledFunctionName(), "boop"; got != want {
t.Errorf("wrong called function name %q; want %q", got, want)
}
ns := extra.CalledFunctionNamespace()
if ns != "ns::source::" {
t.Fatal("expected namespace ns::source::, got", ns)
}
return
}
t.Fatalf("None of the returned diagnostics implement FunctionCallUnknownDiagExtra\n%s", diags.Error())
},
},
// Error messages describing inconsistent result types for conditional expressions.
{
"boop()",
&hcl.EvalContext{
Functions: map[string]function.Function{
"boop": function.New(&function.Spec{
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.DynamicVal, fmt.Errorf("the expected error")
},
}),
},
},
func(t *testing.T, diags hcl.Diagnostics) {
try := func(diags hcl.Diagnostics) {
t.Helper()
for _, diag := range diags {
extra, ok := hcl.DiagnosticExtra[FunctionCallDiagExtra](diag)
if !ok {
continue
}
if got, want := extra.CalledFunctionName(), "boop"; got != want {
t.Errorf("wrong called function name %q; want %q", got, want)
}
err := extra.FunctionCallError()
if err == nil {
t.Fatal("FunctionCallError returned nil")
}
if got, want := err.Error(), "the expected error"; got != want {
t.Errorf("wrong error message\ngot: %q\nwant: %q", got, want)
}
return
}
t.Fatalf("None of the returned diagnostics implement FunctionCallDiagError\n%s", diags.Error())
}
t.Run("unwrapped", func(t *testing.T) {
try(diags)
})
// It should also work if we wrap up the "extras" in wrapper types.
for _, diag := range diags {
diag.Extra = diagnosticExtraWrapper{diag.Extra}
}
t.Run("wrapped", func(t *testing.T) {
try(diags)
})
},
},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
var diags hcl.Diagnostics
expr, parseDiags := ParseExpression([]byte(test.input), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
diags = append(diags, parseDiags...)
_, valDiags := expr.Value(test.ctx)
diags = append(diags, valDiags...)
if !diags.HasErrors() {
t.Fatal("unexpected success")
}
test.assert(t, diags)
})
}
}
type diagnosticExtraWrapper struct {
wrapped interface{}
}
var _ hcl.DiagnosticExtraUnwrapper = diagnosticExtraWrapper{}
func (w diagnosticExtraWrapper) UnwrapDiagnosticExtra() interface{} {
return w.wrapped
}
|