@@ -38,83 +38,99 @@ func TestParse(t *testing.T) {
38
38
}()
39
39
api := proto .NewDRPCProvisionerClient (provisionersdk .Conn (client ))
40
40
41
- for _ , testCase := range []struct {
41
+ testCases := []struct {
42
42
Name string
43
43
Files map [string ]string
44
44
Response * proto.Parse_Response
45
- }{{
46
- Name : "single-variable" ,
47
- Files : map [string ]string {
48
- "main.tf" : `variable "A" {
45
+ // If ErrorContains is not empty, then response.Recv() should return an
46
+ // error containing this string before a Complete response is returned.
47
+ ErrorContains string
48
+ }{
49
+ {
50
+ Name : "single-variable" ,
51
+ Files : map [string ]string {
52
+ "main.tf" : `variable "A" {
49
53
description = "Testing!"
50
54
}` ,
51
- },
52
- Response : & proto.Parse_Response {
53
- Type : & proto.Parse_Response_Complete {
54
- Complete : & proto.Parse_Complete {
55
- ParameterSchemas : []* proto.ParameterSchema {{
56
- Name : "A" ,
57
- RedisplayValue : true ,
58
- AllowOverrideSource : true ,
59
- Description : "Testing!" ,
60
- DefaultDestination : & proto.ParameterDestination {
61
- Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
62
- },
63
- }},
55
+ },
56
+ Response : & proto.Parse_Response {
57
+ Type : & proto.Parse_Response_Complete {
58
+ Complete : & proto.Parse_Complete {
59
+ ParameterSchemas : []* proto.ParameterSchema {{
60
+ Name : "A" ,
61
+ RedisplayValue : true ,
62
+ AllowOverrideSource : true ,
63
+ Description : "Testing!" ,
64
+ DefaultDestination : & proto.ParameterDestination {
65
+ Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
66
+ },
67
+ }},
68
+ },
64
69
},
65
70
},
66
71
},
67
- }, {
68
- Name : "default-variable-value" ,
69
- Files : map [string ]string {
70
- "main.tf" : `variable "A" {
72
+ {
73
+ Name : "default-variable-value" ,
74
+ Files : map [string ]string {
75
+ "main.tf" : `variable "A" {
71
76
default = "wow"
72
77
}` ,
73
- },
74
- Response : & proto.Parse_Response {
75
- Type : & proto.Parse_Response_Complete {
76
- Complete : & proto.Parse_Complete {
77
- ParameterSchemas : []* proto.ParameterSchema {{
78
- Name : "A" ,
79
- RedisplayValue : true ,
80
- AllowOverrideSource : true ,
81
- DefaultSource : & proto.ParameterSource {
82
- Scheme : proto .ParameterSource_DATA ,
83
- Value : "wow" ,
84
- },
85
- DefaultDestination : & proto.ParameterDestination {
86
- Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
87
- },
88
- }},
78
+ },
79
+ Response : & proto.Parse_Response {
80
+ Type : & proto.Parse_Response_Complete {
81
+ Complete : & proto.Parse_Complete {
82
+ ParameterSchemas : []* proto.ParameterSchema {{
83
+ Name : "A" ,
84
+ RedisplayValue : true ,
85
+ AllowOverrideSource : true ,
86
+ DefaultSource : & proto.ParameterSource {
87
+ Scheme : proto .ParameterSource_DATA ,
88
+ Value : "wow" ,
89
+ },
90
+ DefaultDestination : & proto.ParameterDestination {
91
+ Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
92
+ },
93
+ }},
94
+ },
89
95
},
90
96
},
91
97
},
92
- }, {
93
- Name : "variable-validation" ,
94
- Files : map [string ]string {
95
- "main.tf" : `variable "A" {
98
+ {
99
+ Name : "variable-validation" ,
100
+ Files : map [string ]string {
101
+ "main.tf" : `variable "A" {
96
102
validation {
97
103
condition = var.A == "value"
98
104
}
99
105
}` ,
100
- },
101
- Response : & proto.Parse_Response {
102
- Type : & proto.Parse_Response_Complete {
103
- Complete : & proto.Parse_Complete {
104
- ParameterSchemas : []* proto.ParameterSchema {{
105
- Name : "A" ,
106
- RedisplayValue : true ,
107
- ValidationCondition : `var.A == "value"` ,
108
- ValidationTypeSystem : proto .ParameterSchema_HCL ,
109
- AllowOverrideSource : true ,
110
- DefaultDestination : & proto.ParameterDestination {
111
- Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
112
- },
113
- }},
106
+ },
107
+ Response : & proto.Parse_Response {
108
+ Type : & proto.Parse_Response_Complete {
109
+ Complete : & proto.Parse_Complete {
110
+ ParameterSchemas : []* proto.ParameterSchema {{
111
+ Name : "A" ,
112
+ RedisplayValue : true ,
113
+ ValidationCondition : `var.A == "value"` ,
114
+ ValidationTypeSystem : proto .ParameterSchema_HCL ,
115
+ AllowOverrideSource : true ,
116
+ DefaultDestination : & proto.ParameterDestination {
117
+ Scheme : proto .ParameterDestination_PROVISIONER_VARIABLE ,
118
+ },
119
+ }},
120
+ },
114
121
},
115
122
},
116
123
},
117
- }} {
124
+ {
125
+ Name : "bad-syntax" ,
126
+ Files : map [string ]string {
127
+ "main.tf" : "a;sd;ajsd;lajsd;lasjdf;a" ,
128
+ },
129
+ ErrorContains : `The ";" character is not valid.` ,
130
+ },
131
+ }
132
+
133
+ for _ , testCase := range testCases {
118
134
testCase := testCase
119
135
t .Run (testCase .Name , func (t * testing.T ) {
120
136
t .Parallel ()
@@ -133,11 +149,21 @@ func TestParse(t *testing.T) {
133
149
134
150
for {
135
151
msg , err := response .Recv ()
136
- require .NoError (t , err )
152
+ if err != nil {
153
+ if testCase .ErrorContains != "" {
154
+ require .ErrorContains (t , err , testCase .ErrorContains )
155
+ break
156
+ }
157
+
158
+ require .NoError (t , err )
159
+ }
137
160
138
161
if msg .GetComplete () == nil {
139
162
continue
140
163
}
164
+ if testCase .ErrorContains != "" {
165
+ t .Fatal ("expected error but job completed successfully" )
166
+ }
141
167
142
168
// Ensure the want and got are equivalent!
143
169
want , err := json .Marshal (testCase .Response )
0 commit comments