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