forked from jumppad-labs/hclconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_variable_test.go
More file actions
158 lines (139 loc) · 3.78 KB
/
Copy pathparser_variable_test.go
File metadata and controls
158 lines (139 loc) · 3.78 KB
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
package hclconfig
import (
"testing"
"github.com/stretchr/testify/require"
"go.instruqt.com/hclconfig/resources"
)
func parseVariableBlock(t *testing.T, hcl string) (*Config, error) {
t.Helper()
file := CreateTestFile(t, hcl)
p := NewParser(nil)
return p.ParseFile(file)
}
// A variable block may declare any combination of type, default and
// description. Only the label is required.
func TestParseVariableAcceptsEveryAttributeCombination(t *testing.T) {
tests := []struct {
name string
hcl string
}{
{
name: "type and description, no default",
hcl: `
variable "v" {
type = string
description = "a variable"
}`,
},
{
name: "description only",
hcl: `
variable "v" {
description = "a variable"
}`,
},
{
name: "type and default",
hcl: `
variable "v" {
type = string
default = "value"
}`,
},
{
name: "default only",
hcl: `
variable "v" {
default = "value"
}`,
},
{
name: "no attributes",
hcl: `
variable "v" {
}`,
},
{
name: "complex type constraint",
hcl: `
variable "v" {
type = list(string)
}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := parseVariableBlock(t, tt.hcl)
require.NoError(t, err)
vars, err := c.FindResourcesByType(resources.TypeVariable)
require.NoError(t, err)
require.Len(t, vars, 1)
})
}
}
// A declared default seeds the evaluation context so other resources can
// reference the variable without the caller supplying a value.
func TestParseVariableDefaultIsResolvable(t *testing.T) {
c, err := parseVariableBlock(t, `
variable "v" {
type = string
default = "from_default"
}
output "o" {
value = variable.v
}`)
require.NoError(t, err)
outputs, err := c.FindResourcesByType(resources.TypeOutput)
require.NoError(t, err)
require.Len(t, outputs, 1)
out := outputs[0].(*resources.Output)
require.Equal(t, "from_default", out.Value)
}
// A variable declared without a default must still parse; it simply contributes
// no value to the context. Regression test: this previously panicked on an
// unchecked *hcl.Attribute assertion once default became optional.
func TestParseVariableWithoutDefaultDoesNotPanic(t *testing.T) {
require.NotPanics(t, func() {
_, err := parseVariableBlock(t, `
variable "v" {
type = string
}`)
require.NoError(t, err)
})
}
// The reserved instruqt_* variables the lab editor writes into variables.hcl.
// This is the exact shape that reached a sandbox and failed to parse, so it
// stands in as the contract between the editor's writer and this parser.
func TestParseVariableAcceptsEditorGeneratedReservedVariables(t *testing.T) {
c, err := parseVariableBlock(t, `
variable "instruqt_session_id" {
type = string
description = "Reserved: unique ID for this lab session, provided by the platform"
}
variable "instruqt_team_id" {
type = string
description = "Reserved: ID of the team running this session, provided by the platform"
}
variable "instruqt_sandbox_domain" {
type = string
description = "Reserved: base domain used to construct public URLs for this sandbox, provided by the platform"
}`)
require.NoError(t, err)
vars, err := c.FindResourcesByType(resources.TypeVariable)
require.NoError(t, err)
require.Len(t, vars, 3)
v := vars[0].(*resources.Variable)
require.Equal(t, "Reserved: unique ID for this lab session, provided by the platform", v.Description)
require.NotNil(t, v.Type)
}
// A genuine schema violation must still be reported, with the underlying
// diagnostic rather than a formatting artefact.
func TestParseVariableReportsRealDiagnostic(t *testing.T) {
_, err := parseVariableBlock(t, `
variable "v" {
not_a_real_attribute = true
}`)
require.Error(t, err)
require.Contains(t, err.Error(), "not_a_real_attribute")
require.NotContains(t, err.Error(), "%!s(<nil>)")
}