Skip to content

Commit d1a5dfa

Browse files
authored
command: test plan -refresh= arg ordering (#33483)
* main: disambiguate arg ordering test Make it extra clear what order of args we are asserting. * command: fix plan -refresh=false test The test for plan -refresh=false was not functioning, since ReadResource will not be called if the resource is not in prior state. Add a new fixture directory with state, and also test the converse, to prevent regression. * command: add test for refresh flag precedence A consumer relies on the fact that running terraform plan -refresh=false -refresh true gives the same result as terraform plan -refresh=true.
1 parent ea162f6 commit d1a5dfa

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

internal/command/plan_test.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func TestPlan_outBackend(t *testing.T) {
472472
func TestPlan_refreshFalse(t *testing.T) {
473473
// Create a temporary working directory that is empty
474474
td := t.TempDir()
475-
testCopyDir(t, testFixturePath("plan"), td)
475+
testCopyDir(t, testFixturePath("plan-existing-state"), td)
476476
defer testChdir(t, td)()
477477

478478
p := planFixtureProvider()
@@ -498,6 +498,71 @@ func TestPlan_refreshFalse(t *testing.T) {
498498
}
499499
}
500500

501+
func TestPlan_refreshTrue(t *testing.T) {
502+
// Create a temporary working directory that is empty
503+
td := t.TempDir()
504+
testCopyDir(t, testFixturePath("plan-existing-state"), td)
505+
defer testChdir(t, td)()
506+
507+
p := planFixtureProvider()
508+
view, done := testView(t)
509+
c := &PlanCommand{
510+
Meta: Meta{
511+
testingOverrides: metaOverridesForProvider(p),
512+
View: view,
513+
},
514+
}
515+
516+
args := []string{
517+
"-refresh=true",
518+
}
519+
code := c.Run(args)
520+
output := done(t)
521+
if code != 0 {
522+
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
523+
}
524+
525+
if !p.ReadResourceCalled {
526+
t.Fatalf("ReadResource should have been called")
527+
}
528+
}
529+
530+
// A consumer relies on the fact that running
531+
// terraform plan -refresh=false -refresh=true gives the same result as
532+
// terraform plan -refresh=true.
533+
// While the flag logic itself is handled by the stdlib flags package (and code
534+
// in main() that is tested elsewhere), we verify the overall plan command
535+
// behaviour here in case we accidentally break this with additional logic.
536+
func TestPlan_refreshFalseRefreshTrue(t *testing.T) {
537+
// Create a temporary working directory that is empty
538+
td := t.TempDir()
539+
testCopyDir(t, testFixturePath("plan-existing-state"), td)
540+
defer testChdir(t, td)()
541+
542+
p := planFixtureProvider()
543+
view, done := testView(t)
544+
c := &PlanCommand{
545+
Meta: Meta{
546+
testingOverrides: metaOverridesForProvider(p),
547+
View: view,
548+
},
549+
}
550+
551+
args := []string{
552+
"-refresh=false",
553+
"-refresh=true",
554+
}
555+
code := c.Run(args)
556+
output := done(t)
557+
if code != 0 {
558+
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
559+
}
560+
561+
if !p.ReadResourceCalled {
562+
t.Fatal("ReadResource should have been called")
563+
}
564+
}
565+
501566
func TestPlan_state(t *testing.T) {
502567
// Create a temporary working directory that is empty
503568
td := t.TempDir()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "test_instance" "foo" {
2+
ami = "bar"
3+
4+
# This is here because at some point it caused a test failure
5+
network_interface {
6+
device_index = 0
7+
description = "Main network interface"
8+
}
9+
}
10+
11+
data "test_data_source" "a" {
12+
id = "zzzzz"
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": 4,
3+
"terraform_version": "1.6.0",
4+
"serial": 1,
5+
"lineage": "d496625c-bde2-aebc-f5f4-ebbf54eabed2",
6+
"outputs": {},
7+
"resources": [
8+
{
9+
"module": "module.child",
10+
"mode": "managed",
11+
"type": "test_instance",
12+
"name": "test",
13+
"provider": "provider[\"registry.terraform.io/hashicorp/test\"]",
14+
"instances": [
15+
{
16+
"schema_version": 0,
17+
"attributes": {}
18+
}
19+
]
20+
}
21+
],
22+
"check_results": null
23+
}

main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
4949
{
5050
"both env var and CLI",
5151
[]string{testCommandName, "foo", "bar"},
52-
"-foo bar",
53-
[]string{"-foo", "bar", "foo", "bar"},
52+
"-foo baz",
53+
[]string{"-foo", "baz", "foo", "bar"},
5454
false,
5555
},
5656

@@ -143,7 +143,7 @@ func TestMain_cliArgsFromEnv(t *testing.T) {
143143

144144
// Verify
145145
if !reflect.DeepEqual(testCommand.Args, tc.Expected) {
146-
t.Fatalf("bad: %#v", testCommand.Args)
146+
t.Fatalf("expected args %#v but got %#v", tc.Expected, testCommand.Args)
147147
}
148148
})
149149
}

0 commit comments

Comments
 (0)