github.com/rhenning/terraform@v0.8.0-beta2/command/fmt_test.go (about) 1 package command 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "github.com/mitchellh/cli" 13 ) 14 15 func TestFmt_errorReporting(t *testing.T) { 16 tempDir, err := fmtFixtureWriteDir() 17 if err != nil { 18 t.Fatalf("err: %s", err) 19 } 20 defer os.RemoveAll(tempDir) 21 22 ui := new(cli.MockUi) 23 c := &FmtCommand{ 24 Meta: Meta{ 25 ContextOpts: testCtxConfig(testProvider()), 26 Ui: ui, 27 }, 28 } 29 30 dummy_file := filepath.Join(tempDir, "doesnotexist") 31 args := []string{dummy_file} 32 if code := c.Run(args); code != 2 { 33 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 34 } 35 36 expected := fmt.Sprintf("Error running fmt: stat %s: no such file or directory", dummy_file) 37 if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) { 38 t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) 39 } 40 } 41 42 func TestFmt_tooManyArgs(t *testing.T) { 43 ui := new(cli.MockUi) 44 c := &FmtCommand{ 45 Meta: Meta{ 46 ContextOpts: testCtxConfig(testProvider()), 47 Ui: ui, 48 }, 49 } 50 51 args := []string{ 52 "one", 53 "two", 54 } 55 if code := c.Run(args); code != 1 { 56 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 57 } 58 59 expected := "The fmt command expects at most one argument." 60 if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) { 61 t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) 62 } 63 } 64 65 func TestFmt_workingDirectory(t *testing.T) { 66 tempDir, err := fmtFixtureWriteDir() 67 if err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 defer os.RemoveAll(tempDir) 71 72 cwd, err := os.Getwd() 73 if err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 err = os.Chdir(tempDir) 77 if err != nil { 78 t.Fatalf("err: %s", err) 79 } 80 defer os.Chdir(cwd) 81 82 ui := new(cli.MockUi) 83 c := &FmtCommand{ 84 Meta: Meta{ 85 ContextOpts: testCtxConfig(testProvider()), 86 Ui: ui, 87 }, 88 } 89 90 args := []string{} 91 if code := c.Run(args); code != 0 { 92 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 93 } 94 95 expected := fmt.Sprintf("%s\n", fmtFixture.filename) 96 if actual := ui.OutputWriter.String(); actual != expected { 97 t.Fatalf("got: %q\nexpected: %q", actual, expected) 98 } 99 } 100 101 func TestFmt_directoryArg(t *testing.T) { 102 tempDir, err := fmtFixtureWriteDir() 103 if err != nil { 104 t.Fatalf("err: %s", err) 105 } 106 defer os.RemoveAll(tempDir) 107 108 ui := new(cli.MockUi) 109 c := &FmtCommand{ 110 Meta: Meta{ 111 ContextOpts: testCtxConfig(testProvider()), 112 Ui: ui, 113 }, 114 } 115 116 args := []string{tempDir} 117 if code := c.Run(args); code != 0 { 118 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 119 } 120 121 expected := fmt.Sprintf("%s\n", filepath.Join(tempDir, fmtFixture.filename)) 122 if actual := ui.OutputWriter.String(); actual != expected { 123 t.Fatalf("got: %q\nexpected: %q", actual, expected) 124 } 125 } 126 127 func TestFmt_stdinArg(t *testing.T) { 128 input := new(bytes.Buffer) 129 input.Write(fmtFixture.input) 130 131 ui := new(cli.MockUi) 132 c := &FmtCommand{ 133 Meta: Meta{ 134 ContextOpts: testCtxConfig(testProvider()), 135 Ui: ui, 136 }, 137 input: input, 138 } 139 140 args := []string{"-"} 141 if code := c.Run(args); code != 0 { 142 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 143 } 144 145 expected := fmtFixture.golden 146 if actual := ui.OutputWriter.Bytes(); !bytes.Equal(actual, expected) { 147 t.Fatalf("got: %q\nexpected: %q", actual, expected) 148 } 149 } 150 151 func TestFmt_nonDefaultOptions(t *testing.T) { 152 tempDir, err := fmtFixtureWriteDir() 153 if err != nil { 154 t.Fatalf("err: %s", err) 155 } 156 defer os.RemoveAll(tempDir) 157 158 ui := new(cli.MockUi) 159 c := &FmtCommand{ 160 Meta: Meta{ 161 ContextOpts: testCtxConfig(testProvider()), 162 Ui: ui, 163 }, 164 } 165 166 args := []string{ 167 "-list=false", 168 "-write=false", 169 "-diff", 170 tempDir, 171 } 172 if code := c.Run(args); code != 0 { 173 t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 174 } 175 176 expected := fmt.Sprintf("-%s+%s", fmtFixture.input, fmtFixture.golden) 177 if actual := ui.OutputWriter.String(); !strings.Contains(actual, expected) { 178 t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) 179 } 180 } 181 182 var fmtFixture = struct { 183 filename string 184 input, golden []byte 185 }{ 186 "main.tf", 187 []byte(` foo = "bar" 188 `), 189 []byte(`foo = "bar" 190 `), 191 } 192 193 func fmtFixtureWriteDir() (string, error) { 194 dir, err := ioutil.TempDir("", "tf") 195 if err != nil { 196 return "", err 197 } 198 199 err = ioutil.WriteFile(filepath.Join(dir, fmtFixture.filename), fmtFixture.input, 0644) 200 if err != nil { 201 os.RemoveAll(dir) 202 return "", err 203 } 204 205 return dir, nil 206 }