Skip to content

Commit

Permalink
Merge pull request #4766 from hashicorp/phinze/write-empty-planfiles
Browse files Browse the repository at this point in the history
core: write planfile even on empty plans
  • Loading branch information
phinze committed Jan 20, 2016
2 parents 5c16b47 + 4a51e4f commit 859bea5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
18 changes: 9 additions & 9 deletions command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ func (c *PlanCommand) Run(args []string) int {
return 1
}

if plan.Diff.Empty() {
c.Ui.Output(
"No changes. Infrastructure is up-to-date. This means that Terraform\n" +
"could not detect any differences between your configuration and\n" +
"the real physical resources that exist. As a result, Terraform\n" +
"doesn't need to do anything.")
return 0
}

if outPath != "" {
log.Printf("[INFO] Writing plan output to: %s", outPath)
f, err := os.Create(outPath)
Expand All @@ -124,6 +115,15 @@ func (c *PlanCommand) Run(args []string) int {
}
}

if plan.Diff.Empty() {
c.Ui.Output(
"No changes. Infrastructure is up-to-date. This means that Terraform\n" +
"could not detect any differences between your configuration and\n" +
"the real physical resources that exist. As a result, Terraform\n" +
"doesn't need to do anything.")
return 0
}

if outPath == "" {
c.Ui.Output(strings.TrimSpace(planHeaderNoOutput) + "\n")
} else {
Expand Down
49 changes: 49 additions & 0 deletions command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,55 @@ func TestPlan_outPath(t *testing.T) {
}
}

func TestPlan_outPathNoChange(t *testing.T) {
originalState := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
Path: []string{"root"},
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
Type: "test_instance",
Primary: &terraform.InstanceState{
ID: "bar",
},
},
},
},
},
}
statePath := testStateFile(t, originalState)

tf, err := ioutil.TempFile("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
outPath := tf.Name()
os.Remove(tf.Name())

p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

args := []string{
"-out", outPath,
"-state", statePath,
testFixturePath("plan"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

plan := testReadPlan(t, outPath)
if !plan.Diff.Empty() {
t.Fatalf("Expected empty plan to be written to plan file, got: %s", plan)
}
}

func TestPlan_refresh(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
Expand Down

0 comments on commit 859bea5

Please sign in to comment.