From 3649e432c23524101acc9b90834a9b563bd675ec Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Fri, 11 Sep 2020 15:30:56 -0400 Subject: [PATCH] wip --- tfexec/errors.go | 9 +++++- tfexec/fmt.go | 25 +++++++++++---- tfexec/internal/e2etest/fmt_test.go | 16 ++++++++-- .../e2etest/testdata/unformatted/file1.golden | Bin 100 -> 0 bytes .../testdata/unformatted/file1.golden.txt | 4 +++ .../e2etest/testdata/unformatted/file1.tf | 2 +- .../e2etest/testdata/unformatted/file2.golden | Bin 100 -> 0 bytes .../testdata/unformatted/file2.golden.txt | 4 +++ .../e2etest/testdata/unformatted/file2.tf | 2 +- tfexec/internal/e2etest/util_test.go | 30 ++++++++---------- 10 files changed, 64 insertions(+), 28 deletions(-) delete mode 100644 tfexec/internal/e2etest/testdata/unformatted/file1.golden create mode 100644 tfexec/internal/e2etest/testdata/unformatted/file1.golden.txt delete mode 100644 tfexec/internal/e2etest/testdata/unformatted/file2.golden create mode 100644 tfexec/internal/e2etest/testdata/unformatted/file2.golden.txt diff --git a/tfexec/errors.go b/tfexec/errors.go index 241fe715..26e38ed0 100644 --- a/tfexec/errors.go +++ b/tfexec/errors.go @@ -29,7 +29,9 @@ var ( ) func (tf *Terraform) parseError(err error, stderr string) error { - if _, ok := err.(*exec.ExitError); !ok { + // if not an ExitError, just return it, do not try to parse + var ee *exec.ExitError + if !errors.As(err, &ee) { return err } @@ -87,6 +89,11 @@ func (tf *Terraform) parseError(err error, stderr string) error { return &ErrWorkspaceExists{submatches[1]} } } + errString := strings.TrimSpace(stderr) + if errString == "" { + // if stderr is empty, return the ExitError directly, as it will have a better message + return ee + } return errors.New(stderr) } diff --git a/tfexec/fmt.go b/tfexec/fmt.go index 861616fd..d3b8ef43 100644 --- a/tfexec/fmt.go +++ b/tfexec/fmt.go @@ -73,8 +73,7 @@ func (tf *Terraform) FormatWrite(ctx context.Context, opts ...FormatOption) erro return err } - panic("not implemented") - panic(cmd) + return tf.runTerraformCmd(cmd) } func (tf *Terraform) FormatCheck(ctx context.Context, opts ...FormatOption) (bool, []string, error) { @@ -96,11 +95,25 @@ func (tf *Terraform) FormatCheck(ctx context.Context, opts ...FormatOption) (boo cmd.Stdout = mergeWriters(cmd.Stdout, &outBuf) err = tf.runTerraformCmd(cmd) - if err != nil { - return false, nil, err + if err == nil { + return true, nil, nil } + if cmd.ProcessState.ExitCode() == 3 { + // unformatted, parse the file list + + files := []string{} + lines := strings.Split(strings.Replace(outBuf.String(), "\r\n", "\n", -1), "\n") + for _, l := range lines { + l = strings.TrimSpace(l) + if l == "" { + continue + } + files = append(files, l) + } - panic("not implemented") + return false, files, nil + } + return false, nil, err } func (tf *Terraform) formatCmd(ctx context.Context, args []string, opts ...FormatOption) (*exec.Cmd, error) { @@ -128,5 +141,5 @@ func (tf *Terraform) formatCmd(ctx context.Context, args []string, opts ...Forma args = append(args, c.dir) } - return tf.buildTerraformCmd(ctx, args...), nil + return tf.buildTerraformCmd(ctx, nil, args...), nil } diff --git a/tfexec/internal/e2etest/fmt_test.go b/tfexec/internal/e2etest/fmt_test.go index 477439fc..b074b3dc 100644 --- a/tfexec/internal/e2etest/fmt_test.go +++ b/tfexec/internal/e2etest/fmt_test.go @@ -2,6 +2,7 @@ package e2etest import ( "context" + "path/filepath" "reflect" "strings" "testing" @@ -44,7 +45,7 @@ func TestFormatCheck(t *testing.T) { runTest(t, "unformatted", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { formatted, files, err := tf.FormatCheck(context.Background()) if err != nil { - t.Fatal(err) + t.Fatalf("error from FormatCheck: %T %q", err, err) } if formatted { @@ -54,12 +55,23 @@ func TestFormatCheck(t *testing.T) { if !reflect.DeepEqual(files, []string{"file1.tf", "file2.tf"}) { t.Fatalf("unexpected files list: %#v", files) } + + // TODO: assert files not updated to golden, mabye take hash before running }) } func TestFormatWrite(t *testing.T) { - t.Skip("not implemented") runTest(t, "unformatted", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + err := tf.FormatWrite(context.Background()) + if err != nil { + t.Fatalf("error from FormatWrite: %T %q", err, err) + } + for file, golden := range map[string]string{ + "file1.tf": "file1.golden.txt", + "file2.tf": "file2.golden.txt", + } { + textFilesEqual(t, filepath.Join(tf.WorkingDir(), golden), filepath.Join(tf.WorkingDir(), file)) + } }) } diff --git a/tfexec/internal/e2etest/testdata/unformatted/file1.golden b/tfexec/internal/e2etest/testdata/unformatted/file1.golden deleted file mode 100644 index 416a4927ed7810529594a0e702539b01d796d8a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 100 zcmezWuZSU)p_n0`p_HMBAsNV4U{GR61M>5MG>DzVkO-6kv8x$)8MuJ70$8jHNZSIj SAzZW&XhsE8)CeqE3nl@Ydk&KT diff --git a/tfexec/internal/e2etest/testdata/unformatted/file1.golden.txt b/tfexec/internal/e2etest/testdata/unformatted/file1.golden.txt new file mode 100644 index 00000000..630705e3 --- /dev/null +++ b/tfexec/internal/e2etest/testdata/unformatted/file1.golden.txt @@ -0,0 +1,4 @@ +resource "foo" "bar" { + baz = 1 + qux = 2 +} diff --git a/tfexec/internal/e2etest/testdata/unformatted/file1.tf b/tfexec/internal/e2etest/testdata/unformatted/file1.tf index e01408a9..07c72726 100644 --- a/tfexec/internal/e2etest/testdata/unformatted/file1.tf +++ b/tfexec/internal/e2etest/testdata/unformatted/file1.tf @@ -1,4 +1,4 @@ resource "foo" "bar" { baz = 1 qux = 2 -} \ No newline at end of file +} diff --git a/tfexec/internal/e2etest/testdata/unformatted/file2.golden b/tfexec/internal/e2etest/testdata/unformatted/file2.golden deleted file mode 100644 index fd028e70a486bda4bcbd539ba43ddceccfda8883..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 100 zcmezWuZSU)p_n0`p_HMBAsNV4U{GR61M>5MG>DzVkjPL4W>+)tGH?OK6rf@X47NbF UAzZW&XhsE41SDz%7Oe%70Gw+Mn*aa+ diff --git a/tfexec/internal/e2etest/testdata/unformatted/file2.golden.txt b/tfexec/internal/e2etest/testdata/unformatted/file2.golden.txt new file mode 100644 index 00000000..b5dd748a --- /dev/null +++ b/tfexec/internal/e2etest/testdata/unformatted/file2.golden.txt @@ -0,0 +1,4 @@ +resource "foo" "baz" { + baz = 1 + qux = 2 +} diff --git a/tfexec/internal/e2etest/testdata/unformatted/file2.tf b/tfexec/internal/e2etest/testdata/unformatted/file2.tf index 75eaeb71..8913b1ad 100644 --- a/tfexec/internal/e2etest/testdata/unformatted/file2.tf +++ b/tfexec/internal/e2etest/testdata/unformatted/file2.tf @@ -1,4 +1,4 @@ resource "foo" "baz" { baz = 1 qux = 2 -} \ No newline at end of file +} diff --git a/tfexec/internal/e2etest/util_test.go b/tfexec/internal/e2etest/util_test.go index ae43e604..685a177f 100644 --- a/tfexec/internal/e2etest/util_test.go +++ b/tfexec/internal/e2etest/util_test.go @@ -1,8 +1,6 @@ package e2etest import ( - "bufio" - "bytes" "context" "fmt" "io" @@ -163,27 +161,25 @@ func copyFile(path string, dstPath string) error { return nil } -// filesEqual returns true iff the two files have the same contents. -func filesEqual(file1, file2 string) (bool, error) { - sf, err := os.Open(file1) +// filesEqual asserts that two files have the same contents. +func textFilesEqual(t *testing.T, expected, actual string) { + eb, err := ioutil.ReadFile(expected) if err != nil { - return false, err + t.Fatal(err) } - df, err := os.Open(file2) + ab, err := ioutil.ReadFile(actual) if err != nil { - return false, err + t.Fatal(err) } - sscan := bufio.NewScanner(sf) - dscan := bufio.NewScanner(df) + es := string(eb) + es = strings.ReplaceAll(es, "\r\n", "\n") - for sscan.Scan() { - dscan.Scan() - if !bytes.Equal(sscan.Bytes(), dscan.Bytes()) { - return true, nil - } - } + as := string(ab) + as = strings.ReplaceAll(as, "\r\n", "\n") - return false, nil + if as != es { + t.Fatalf("expected:\n%s\n\ngot:\n%s\n", es, as) + } }