2014-07-13 05:21:46 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-10-01 17:37:57 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-07-13 05:21:46 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-10-08 19:29:54 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-13 05:21:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetaColorize(t *testing.T) {
|
|
|
|
var m *Meta
|
|
|
|
var args, args2 []string
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, color
|
2014-07-13 05:21:46 +02:00
|
|
|
m = new(Meta)
|
2014-07-17 18:34:32 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if m.Colorize().Disable {
|
|
|
|
t.Fatal("should not be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:34:32 +02:00
|
|
|
// Test basic, no change
|
|
|
|
m = new(Meta)
|
|
|
|
args = []string{"foo", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-17 18:34:32 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
|
2014-07-13 05:21:46 +02:00
|
|
|
// Test disable #1
|
|
|
|
m = new(Meta)
|
2014-09-09 05:41:10 +02:00
|
|
|
m.Color = true
|
2014-07-13 05:21:46 +02:00
|
|
|
args = []string{"foo", "-no-color", "bar"}
|
|
|
|
args2 = []string{"foo", "bar"}
|
2014-08-05 18:32:01 +02:00
|
|
|
args = m.process(args, false)
|
2014-07-13 05:21:46 +02:00
|
|
|
if !reflect.DeepEqual(args, args2) {
|
|
|
|
t.Fatalf("bad: %#v", args)
|
|
|
|
}
|
|
|
|
if !m.Colorize().Disable {
|
|
|
|
t.Fatal("should be disabled")
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 20:11:35 +02:00
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode() != terraform.InputModeStd {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_disable(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-input=false"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode() > 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
func TestMetaInputMode_defaultVars(t *testing.T) {
|
2014-10-01 17:37:57 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
// Create a temporary directory for our cwd
|
|
|
|
d := tempDir(t)
|
|
|
|
if err := os.MkdirAll(d, 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(d); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
// Create the default vars file
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
filepath.Join(d, DefaultVarsFilename),
|
|
|
|
[]byte(""),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := new(Meta)
|
|
|
|
args := []string{}
|
|
|
|
args = m.process(args, true)
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-10-01 17:37:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 19:29:54 +02:00
|
|
|
|
|
|
|
func TestMetaInputMode_vars(t *testing.T) {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2014-09-29 20:11:35 +02:00
|
|
|
m := new(Meta)
|
|
|
|
args := []string{"-var", "foo=bar"}
|
|
|
|
|
|
|
|
fs := m.flagSet("foo")
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:29:54 +02:00
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
2014-09-29 20:11:35 +02:00
|
|
|
}
|
|
|
|
}
|