github.com/lamielle/terraform@v0.3.2-0.20141121070651-81f008ba53d5/command/init_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestInit(t *testing.T) { 12 dir := tempDir(t) 13 14 ui := new(cli.MockUi) 15 c := &InitCommand{ 16 Meta: Meta{ 17 ContextOpts: testCtxConfig(testProvider()), 18 Ui: ui, 19 }, 20 } 21 22 args := []string{ 23 testFixturePath("init"), 24 dir, 25 } 26 if code := c.Run(args); code != 0 { 27 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 28 } 29 30 if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil { 31 t.Fatalf("err: %s", err) 32 } 33 } 34 35 func TestInit_cwd(t *testing.T) { 36 dir := tempDir(t) 37 if err := os.MkdirAll(dir, 0755); err != nil { 38 t.Fatalf("err: %s", err) 39 } 40 41 // Change to the temporary directory 42 cwd, err := os.Getwd() 43 if err != nil { 44 t.Fatalf("err: %s", err) 45 } 46 if err := os.Chdir(dir); err != nil { 47 t.Fatalf("err: %s", err) 48 } 49 defer os.Chdir(cwd) 50 51 ui := new(cli.MockUi) 52 c := &InitCommand{ 53 Meta: Meta{ 54 ContextOpts: testCtxConfig(testProvider()), 55 Ui: ui, 56 }, 57 } 58 59 args := []string{ 60 testFixturePath("init"), 61 } 62 if code := c.Run(args); code != 0 { 63 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 64 } 65 66 if _, err := os.Stat("hello.tf"); err != nil { 67 t.Fatalf("err: %s", err) 68 } 69 } 70 71 func TestInit_multipleArgs(t *testing.T) { 72 ui := new(cli.MockUi) 73 c := &InitCommand{ 74 Meta: Meta{ 75 ContextOpts: testCtxConfig(testProvider()), 76 Ui: ui, 77 }, 78 } 79 80 args := []string{ 81 "bad", 82 "bad", 83 } 84 if code := c.Run(args); code != 1 { 85 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 86 } 87 } 88 89 func TestInit_noArgs(t *testing.T) { 90 ui := new(cli.MockUi) 91 c := &InitCommand{ 92 Meta: Meta{ 93 ContextOpts: testCtxConfig(testProvider()), 94 Ui: ui, 95 }, 96 } 97 98 args := []string{} 99 if code := c.Run(args); code != 1 { 100 t.Fatalf("bad: \n%s", ui.OutputWriter.String()) 101 } 102 } 103 104 // https://github.com/hashicorp/terraform/issues/518 105 func TestInit_dstInSrc(t *testing.T) { 106 dir := tempDir(t) 107 if err := os.MkdirAll(dir, 0755); err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 111 // Change to the temporary directory 112 cwd, err := os.Getwd() 113 if err != nil { 114 t.Fatalf("err: %s", err) 115 } 116 if err := os.Chdir(dir); err != nil { 117 t.Fatalf("err: %s", err) 118 } 119 defer os.Chdir(cwd) 120 121 if _, err := os.Create("issue518.tf"); err != nil { 122 t.Fatalf("err: %s", err) 123 } 124 125 ui := new(cli.MockUi) 126 c := &InitCommand{ 127 Meta: Meta{ 128 ContextOpts: testCtxConfig(testProvider()), 129 Ui: ui, 130 }, 131 } 132 133 args := []string{ 134 ".", 135 "foo", 136 } 137 if code := c.Run(args); code != 0 { 138 t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) 139 } 140 141 if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil { 142 t.Fatalf("err: %s", err) 143 } 144 }