github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/command/get_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/copy" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestGet(t *testing.T) { 13 tmp, cwd := testCwd(t) 14 defer testFixCwd(t, tmp, cwd) 15 16 ui := new(cli.MockUi) 17 c := &GetCommand{ 18 Meta: Meta{ 19 testingOverrides: metaOverridesForProvider(testProvider()), 20 Ui: ui, 21 dataDir: tempDir(t), 22 }, 23 } 24 25 args := []string{ 26 testFixturePath("get"), 27 } 28 if code := c.Run(args); code != 0 { 29 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 30 } 31 32 output := ui.OutputWriter.String() 33 if !strings.Contains(output, "- foo in") { 34 t.Fatalf("doesn't look like get: %s", output) 35 } 36 } 37 38 func TestGet_multipleArgs(t *testing.T) { 39 ui := new(cli.MockUi) 40 c := &GetCommand{ 41 Meta: Meta{ 42 testingOverrides: metaOverridesForProvider(testProvider()), 43 Ui: ui, 44 dataDir: tempDir(t), 45 }, 46 } 47 48 args := []string{ 49 "bad", 50 "bad", 51 } 52 if code := c.Run(args); code != 1 { 53 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 54 } 55 } 56 57 func TestGet_noArgs(t *testing.T) { 58 td := tempDir(t) 59 copy.CopyDir(testFixturePath("get"), td) 60 defer os.RemoveAll(td) 61 defer testChdir(t, td)() 62 63 ui := new(cli.MockUi) 64 c := &GetCommand{ 65 Meta: Meta{ 66 testingOverrides: metaOverridesForProvider(testProvider()), 67 Ui: ui, 68 dataDir: tempDir(t), 69 }, 70 } 71 72 args := []string{} 73 if code := c.Run(args); code != 0 { 74 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 75 } 76 77 output := ui.OutputWriter.String() 78 if !strings.Contains(output, "- foo in") { 79 t.Fatalf("doesn't look like get: %s", output) 80 } 81 } 82 83 func TestGet_update(t *testing.T) { 84 tmp, cwd := testCwd(t) 85 defer testFixCwd(t, tmp, cwd) 86 87 ui := new(cli.MockUi) 88 c := &GetCommand{ 89 Meta: Meta{ 90 testingOverrides: metaOverridesForProvider(testProvider()), 91 Ui: ui, 92 dataDir: tempDir(t), 93 }, 94 } 95 96 args := []string{ 97 "-update", 98 testFixturePath("get"), 99 } 100 if code := c.Run(args); code != 0 { 101 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 102 } 103 104 output := ui.OutputWriter.String() 105 if !strings.Contains(output, `- foo in`) { 106 t.Fatalf("doesn't look like get: %s", output) 107 } 108 }