8
8
"strings"
9
9
"testing"
10
10
11
+ "github.com/google/go-cmp/cmp"
11
12
"github.com/hashicorp/terraform/internal/configs/configschema"
12
13
"github.com/hashicorp/terraform/internal/providers"
13
14
testing_provider "github.com/hashicorp/terraform/internal/providers/testing"
@@ -26,34 +27,58 @@ func TestQuery(t *testing.T) {
26
27
name : "basic query" ,
27
28
directory : "basic" ,
28
29
expectedOut : `list.test_instance.example id=test-instance-1 Test Instance 1
29
- list.test_instance.example id=test-instance-2 Test Instance 2` ,
30
+ list.test_instance.example id=test-instance-2 Test Instance 2` ,
30
31
},
31
32
{
32
33
name : "query referencing local variable" ,
33
34
directory : "with-locals" ,
34
35
expectedOut : `list.test_instance.example id=test-instance-1 Test Instance 1
35
- list.test_instance.example id=test-instance-2 Test Instance 2` ,
36
+ list.test_instance.example id=test-instance-2 Test Instance 2` ,
36
37
},
37
38
{
38
39
name : "config with no query block" ,
39
40
directory : "no-list-block" ,
40
41
expectedOut : "" ,
41
- expectedErr : []string {`Warning: No resources to query
42
+ expectedErr : []string {`
43
+ Error: No resources to query
42
44
43
- The configuration does not contain any resources that can be queried.` },
45
+ The configuration does not contain any resources that can be queried.
46
+ ` },
44
47
},
45
48
{
46
49
name : "missing query file" ,
47
50
directory : "missing-query-file" ,
48
51
expectedOut : "" ,
49
- expectedErr : []string {"No resources to query" },
52
+ expectedErr : []string {`
53
+ Error: No resources to query
54
+
55
+ The configuration does not contain any resources that can be queried.
56
+ ` },
57
+ },
58
+ {
59
+ name : "missing configuration" ,
60
+ directory : "missing-configuration" ,
61
+ expectedOut : "" ,
62
+ expectedErr : []string {`
63
+ Error: No configuration files
64
+
65
+ Query Plan requires a query configuration to be present. Create a Terraform
66
+ query configuration file (.tfquery.hcl file) and try again.
67
+ ` },
50
68
},
51
69
{
52
70
name : "invalid query syntax" ,
53
71
directory : "invalid-syntax" ,
54
72
expectedOut : "" ,
55
73
initCode : 1 ,
56
- expectedErr : []string {"Unsupported block type" , "on query.tfquery.hcl line 11" , `resource "test_instance" "example"` },
74
+ expectedErr : []string {`
75
+ Error: Unsupported block type
76
+
77
+ on query.tfquery.hcl line 11:
78
+ 11: resource "test_instance" "example" {
79
+
80
+ Blocks of type "resource" are not expected here.
81
+ ` },
57
82
},
58
83
}
59
84
@@ -76,20 +101,20 @@ The configuration does not contain any resources that can be queried.`},
76
101
ProviderSource : providerSource ,
77
102
}
78
103
79
- init := & InitCommand {
80
- Meta : meta ,
81
- }
82
- if code := init .Run (nil ); code != ts .initCode {
83
- output := done (t )
104
+ init := & InitCommand {Meta : meta }
105
+ code := init .Run (nil )
106
+ output := done (t )
107
+ if code != ts .initCode {
84
108
t .Fatalf ("expected status code %d but got %d: %s" , ts .initCode , code , output .All ())
85
109
}
86
110
87
- c := & QueryCommand {
88
- Meta : meta ,
89
- }
111
+ view , done = testView (t )
112
+ meta .View = view
113
+
114
+ c := & QueryCommand {Meta : meta }
90
115
args := []string {"-no-color" }
91
- code : = c .Run (args )
92
- output : = done (t )
116
+ code = c .Run (args )
117
+ output = done (t )
93
118
actual := output .All ()
94
119
if len (ts .expectedErr ) == 0 {
95
120
if code != 0 && len (ts .expectedErr ) == 0 {
@@ -102,8 +127,8 @@ The configuration does not contain any resources that can be queried.`},
102
127
}
103
128
} else {
104
129
for _ , expected := range ts .expectedErr {
105
- if ! strings . Contains ( actual , expected ) {
106
- t .Errorf ("expected error message to contain '%s', got : %s" , expected , actual )
130
+ if diff := cmp . Diff ( expected , actual ); diff != "" {
131
+ t .Errorf ("expected error message to contain '%s', \n got : %s, diff: %s " , expected , actual , diff )
107
132
}
108
133
}
109
134
}
0 commit comments