github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/console_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/mitchellh/cli" 11 ) 12 13 // ConsoleCommand is tested primarily with tests in the "repl" package. 14 // It is not tested here because the Console uses a readline-like library 15 // that takes over stdin/stdout. It is difficult to test directly. The 16 // core logic is tested in "repl" 17 // 18 // This file still contains some tests using the stdin-based input. 19 20 func TestConsole_basic(t *testing.T) { 21 tmp, cwd := testCwd(t) 22 defer testFixCwd(t, tmp, cwd) 23 24 p := testProvider() 25 ui := new(cli.MockUi) 26 c := &ConsoleCommand{ 27 Meta: Meta{ 28 testingOverrides: metaOverridesForProvider(p), 29 Ui: ui, 30 }, 31 } 32 33 var output bytes.Buffer 34 defer testStdinPipe(t, strings.NewReader("1+5\n"))() 35 outCloser := testStdoutCapture(t, &output) 36 37 args := []string{} 38 code := c.Run(args) 39 outCloser() 40 if code != 0 { 41 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 42 } 43 44 actual := output.String() 45 if actual != "6\n" { 46 t.Fatalf("bad: %q", actual) 47 } 48 } 49 50 func TestConsole_tfvars(t *testing.T) { 51 tmp, cwd := testCwd(t) 52 defer testFixCwd(t, tmp, cwd) 53 54 // Write a terraform.tvars 55 varFilePath := filepath.Join(tmp, "terraform.tfvars") 56 if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil { 57 t.Fatalf("err: %s", err) 58 } 59 60 p := testProvider() 61 ui := new(cli.MockUi) 62 c := &ConsoleCommand{ 63 Meta: Meta{ 64 testingOverrides: metaOverridesForProvider(p), 65 Ui: ui, 66 }, 67 } 68 69 var output bytes.Buffer 70 defer testStdinPipe(t, strings.NewReader("var.foo\n"))() 71 outCloser := testStdoutCapture(t, &output) 72 73 args := []string{ 74 testFixturePath("apply-vars"), 75 } 76 code := c.Run(args) 77 outCloser() 78 if code != 0 { 79 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 80 } 81 82 actual := output.String() 83 if actual != "bar\n" { 84 t.Fatalf("bad: %q", actual) 85 } 86 }