command: fix TestMeta_process

The mission of this process method used to include dealing with
auto-loaded tfvars files, but it doesn't do that anymore.

It does still deal with the -no-color option, but the test wasn't
exercising that part before.

Now the test here focuses on the -no-color behavior.

The process method still has a "vars" flag argument which is no longer
used. Since this is an unexported method we could potentially address this
but this commit is intentionally limited only to fixing the test.
This commit is contained in:
Martin Atkins 2018-11-07 15:45:06 -08:00
parent fcf3f643ce
commit e20346bf4f
2 changed files with 59 additions and 27 deletions

View File

@ -413,7 +413,9 @@ func (m *Meta) moduleStorage(root string, mode module.GetMode) *module.Storage {
// will potentially modify the args in-place. It will return the resulting
// slice.
//
// vars says whether or not we support variables.
// vars is now ignored. It used to control whether to process variables, but
// that is no longer the responsibility of this function. (That happens
// instead in Meta.collectVariableValues.)
func (m *Meta) process(args []string, vars bool) ([]string, error) {
// We do this so that we retain the ability to technically call
// process multiple times, even if we have no plans to do so

View File

@ -1,12 +1,15 @@
package command
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/terraform"
)
@ -269,7 +272,13 @@ func TestMeta_process(t *testing.T) {
defer os.RemoveAll(d)
defer testChdir(t, d)()
// Create two vars files
// At one point it was the responsibility of this process function to
// insert fake additional -var-file options into the command line
// if the automatic tfvars files were present. This is no longer the
// responsibility of process (it happens in collectVariableValues instead)
// but we're still testing with these files in place to verify that
// they _aren't_ being interpreted by process, since that could otherwise
// cause them to be added more than once and mess up the precedence order.
defaultVarsfile := "terraform.tfvars"
err := ioutil.WriteFile(
filepath.Join(d, defaultVarsfile),
@ -304,33 +313,54 @@ func TestMeta_process(t *testing.T) {
t.Fatalf("err: %s", err)
}
m := new(Meta)
args := []string{}
args, err = m.process(args, true)
if err != nil {
t.Fatalf("err: %s", err)
tests := []struct {
GivenArgs []string
FilteredArgs []string
ExtraCheck func(*testing.T, *Meta)
}{
{
[]string{},
[]string{},
func(t *testing.T, m *Meta) {
if got, want := m.color, true; got != want {
t.Errorf("wrong m.color value %#v; want %#v", got, want)
}
if got, want := m.Color, true; got != want {
t.Errorf("wrong m.Color value %#v; want %#v", got, want)
}
},
},
{
[]string{"-no-color"},
[]string{},
func(t *testing.T, m *Meta) {
if got, want := m.color, false; got != want {
t.Errorf("wrong m.color value %#v; want %#v", got, want)
}
if got, want := m.Color, false; got != want {
t.Errorf("wrong m.Color value %#v; want %#v", got, want)
}
},
},
}
if len(args) != 6 {
t.Fatalf("expected 6 args, got %v", args)
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s", test.GivenArgs), func(t *testing.T) {
m := new(Meta)
m.Color = true // this is the default also for normal use, overridden by -no-color
args := test.GivenArgs
args, err = m.process(args, true)
if err != nil {
t.Fatalf("err: %s", err)
}
if args[0] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[0])
}
if args[1] != defaultVarsfile {
t.Fatalf("expected %q, got %q", defaultVarsfile, args[1])
}
if args[2] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[2])
}
if args[3] != fileFirstAlphabetical {
t.Fatalf("expected %q, got %q", fileFirstAlphabetical, args[3])
}
if args[4] != "-var-file-default" {
t.Fatalf("expected %q, got %q", "-var-file-default", args[4])
}
if args[5] != fileLastAlphabetical {
t.Fatalf("expected %q, got %q", fileLastAlphabetical, args[5])
if !cmp.Equal(test.FilteredArgs, args) {
t.Errorf("wrong filtered arguments\n%s", cmp.Diff(test.FilteredArgs, args))
}
if test.ExtraCheck != nil {
test.ExtraCheck(t, m)
}
})
}
}