Skip to content

Commit 7822e33

Browse files
dsa0xdbanck
authored andcommitted
fix tests
1 parent ac99726 commit 7822e33

File tree

5 files changed

+72
-31
lines changed

5 files changed

+72
-31
lines changed

internal/backend/local/backend_plan.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,30 @@ func (b *Local) opPlan(
4141
return
4242
}
4343

44-
// Local planning requires a config, unless we're planning to destroy.
45-
if op.PlanMode != plans.DestroyMode && !op.HasConfig() {
46-
diags = diags.Append(tfdiags.Sourceless(
47-
tfdiags.Error,
48-
"No configuration files",
49-
"Plan requires configuration to be present. Planning without a configuration would "+
50-
"mark everything for destruction, which is normally not what is desired. If you "+
51-
"would like to destroy everything, run plan with the -destroy option. Otherwise, "+
52-
"create a Terraform configuration file (.tf file) and try again.",
53-
))
54-
op.ReportResult(runningOp, diags)
55-
return
44+
if !op.HasConfig() {
45+
switch {
46+
case op.Query:
47+
// Special diag for terraform query command
48+
diags = diags.Append(tfdiags.Sourceless(
49+
tfdiags.Error,
50+
"No configuration files",
51+
"Query Plan requires a query configuration to be present. Create a Terraform query configuration file (.tfquery.hcl file) and try again.",
52+
))
53+
op.ReportResult(runningOp, diags)
54+
return
55+
case op.PlanMode != plans.DestroyMode:
56+
// Local planning requires a config, unless we're planning to destroy.
57+
diags = diags.Append(tfdiags.Sourceless(
58+
tfdiags.Error,
59+
"No configuration files",
60+
"Plan requires configuration to be present. Planning without a configuration would "+
61+
"mark everything for destruction, which is normally not what is desired. If you "+
62+
"would like to destroy everything, run plan with the -destroy option. Otherwise, "+
63+
"create a Terraform configuration file (.tf file) and try again.",
64+
))
65+
op.ReportResult(runningOp, diags)
66+
return
67+
}
5668
}
5769

5870
if len(op.GenerateConfigOut) > 0 {

internal/command/query_test.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/google/go-cmp/cmp"
1112
"github.com/hashicorp/terraform/internal/configs/configschema"
1213
"github.com/hashicorp/terraform/internal/providers"
1314
testing_provider "github.com/hashicorp/terraform/internal/providers/testing"
@@ -26,34 +27,58 @@ func TestQuery(t *testing.T) {
2627
name: "basic query",
2728
directory: "basic",
2829
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`,
3031
},
3132
{
3233
name: "query referencing local variable",
3334
directory: "with-locals",
3435
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`,
3637
},
3738
{
3839
name: "config with no query block",
3940
directory: "no-list-block",
4041
expectedOut: "",
41-
expectedErr: []string{`Warning: No resources to query
42+
expectedErr: []string{`
43+
Error: No resources to query
4244
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+
`},
4447
},
4548
{
4649
name: "missing query file",
4750
directory: "missing-query-file",
4851
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+
`},
5068
},
5169
{
5270
name: "invalid query syntax",
5371
directory: "invalid-syntax",
5472
expectedOut: "",
5573
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+
`},
5782
},
5883
}
5984

@@ -76,20 +101,20 @@ The configuration does not contain any resources that can be queried.`},
76101
ProviderSource: providerSource,
77102
}
78103

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 {
84108
t.Fatalf("expected status code %d but got %d: %s", ts.initCode, code, output.All())
85109
}
86110

87-
c := &QueryCommand{
88-
Meta: meta,
89-
}
111+
view, done = testView(t)
112+
meta.View = view
113+
114+
c := &QueryCommand{Meta: meta}
90115
args := []string{"-no-color"}
91-
code := c.Run(args)
92-
output := done(t)
116+
code = c.Run(args)
117+
output = done(t)
93118
actual := output.All()
94119
if len(ts.expectedErr) == 0 {
95120
if code != 0 && len(ts.expectedErr) == 0 {
@@ -102,8 +127,8 @@ The configuration does not contain any resources that can be queried.`},
102127
}
103128
} else {
104129
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', \ngot: %s, diff: %s", expected, actual, diff)
107132
}
108133
}
109134
}

internal/command/testdata/query/missing-configuration/.gitkeep

Whitespace-only changes.

internal/configs/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ func (c *Config) DeepEach(cb func(c *Config)) {
148148
// nodes in the module tree until the iterator is exhausted or terminated.
149149
func (c *Config) AllModules() iter.Seq[*Config] {
150150
return func(yield func(*Config) bool) {
151+
if !yield(c) {
152+
return
153+
}
151154
for _, ch := range c.Children {
152155
if !yield(ch) {
153156
return

internal/terraform/context_plan.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,11 @@ The -target option is not for routine use, and is provided only for exceptional
281281

282282
if !hasQuery {
283283
diags = diags.Append(tfdiags.Sourceless(
284-
tfdiags.Warning,
284+
tfdiags.Error,
285285
"No resources to query",
286286
`The configuration does not contain any resources that can be queried.`,
287287
))
288+
return nil, nil, diags
288289
}
289290
}
290291

0 commit comments

Comments
 (0)