github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/providers_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestProviders(t *testing.T) { 12 cwd, err := os.Getwd() 13 if err != nil { 14 t.Fatalf("err: %s", err) 15 } 16 if err := os.Chdir(testFixturePath("providers")); err != nil { 17 t.Fatalf("err: %s", err) 18 } 19 defer os.Chdir(cwd) 20 21 ui := new(cli.MockUi) 22 c := &ProvidersCommand{ 23 Meta: Meta{ 24 Ui: ui, 25 }, 26 } 27 28 args := []string{} 29 if code := c.Run(args); code != 0 { 30 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 31 } 32 33 output := ui.OutputWriter.String() 34 if !strings.Contains(output, "provider.foo") { 35 t.Errorf("output missing provider.foo\n\n%s", output) 36 } 37 if !strings.Contains(output, "provider.bar") { 38 t.Errorf("output missing provider.bar\n\n%s", output) 39 } 40 if !strings.Contains(output, "provider.baz") { 41 t.Errorf("output missing provider.baz\n\n%s", output) 42 } 43 } 44 45 func TestProviders_noConfigs(t *testing.T) { 46 cwd, err := os.Getwd() 47 if err != nil { 48 t.Fatalf("err: %s", err) 49 } 50 if err := os.Chdir(testFixturePath("")); err != nil { 51 t.Fatalf("err: %s", err) 52 } 53 defer os.Chdir(cwd) 54 55 ui := new(cli.MockUi) 56 c := &ProvidersCommand{ 57 Meta: Meta{ 58 Ui: ui, 59 }, 60 } 61 62 args := []string{} 63 if code := c.Run(args); code == 0 { 64 t.Fatal("expected command to return non-zero exit code" + 65 " when no configs are available") 66 } 67 68 output := ui.ErrorWriter.String() 69 expectedErrMsg := "No configuration files found" 70 if !strings.Contains(output, expectedErrMsg) { 71 t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output) 72 } 73 }