mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
147 lines
2.8 KiB
Go
147 lines
2.8 KiB
Go
package command
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestMetaColorize(t *testing.T) {
|
|
var m *Meta
|
|
var args, args2 []string
|
|
|
|
// Test basic, color
|
|
m = new(Meta)
|
|
m.Color = true
|
|
args = []string{"foo", "bar"}
|
|
args2 = []string{"foo", "bar"}
|
|
args = m.process(args, false)
|
|
if !reflect.DeepEqual(args, args2) {
|
|
t.Fatalf("bad: %#v", args)
|
|
}
|
|
if m.Colorize().Disable {
|
|
t.Fatal("should not be disabled")
|
|
}
|
|
|
|
// Test basic, no change
|
|
m = new(Meta)
|
|
args = []string{"foo", "bar"}
|
|
args2 = []string{"foo", "bar"}
|
|
args = m.process(args, false)
|
|
if !reflect.DeepEqual(args, args2) {
|
|
t.Fatalf("bad: %#v", args)
|
|
}
|
|
if !m.Colorize().Disable {
|
|
t.Fatal("should be disabled")
|
|
}
|
|
|
|
// Test disable #1
|
|
m = new(Meta)
|
|
m.Color = true
|
|
args = []string{"foo", "-no-color", "bar"}
|
|
args2 = []string{"foo", "bar"}
|
|
args = m.process(args, false)
|
|
if !reflect.DeepEqual(args, args2) {
|
|
t.Fatalf("bad: %#v", args)
|
|
}
|
|
if !m.Colorize().Disable {
|
|
t.Fatal("should be disabled")
|
|
}
|
|
}
|
|
|
|
func TestMetaInputMode(t *testing.T) {
|
|
test = false
|
|
defer func() { test = true }()
|
|
|
|
m := new(Meta)
|
|
args := []string{}
|
|
|
|
fs := m.flagSet("foo")
|
|
if err := fs.Parse(args); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if m.InputMode() != terraform.InputModeStd {
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
|
}
|
|
}
|
|
|
|
func TestMetaInputMode_disable(t *testing.T) {
|
|
test = false
|
|
defer func() { test = true }()
|
|
|
|
m := new(Meta)
|
|
args := []string{"-input=false"}
|
|
|
|
fs := m.flagSet("foo")
|
|
if err := fs.Parse(args); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if m.InputMode() > 0 {
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
|
}
|
|
}
|
|
|
|
func TestMetaInputMode_defaultVars(t *testing.T) {
|
|
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)
|
|
}
|
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
|
}
|
|
}
|
|
|
|
func TestMetaInputMode_vars(t *testing.T) {
|
|
test = false
|
|
defer func() { test = true }()
|
|
|
|
m := new(Meta)
|
|
args := []string{"-var", "foo=bar"}
|
|
|
|
fs := m.flagSet("foo")
|
|
if err := fs.Parse(args); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if m.InputMode()&terraform.InputModeVar != 0 {
|
|
t.Fatalf("bad: %#v", m.InputMode())
|
|
}
|
|
}
|