From 78025b1bcabcba12fa4b4bcde2761655c43f509c Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 24 Sep 2020 19:46:39 +0100 Subject: [PATCH 1/3] fmt: Gate formatting command on 0.7.7 --- tfexec/errors.go | 10 ++++++++++ tfexec/fmt.go | 5 +++++ tfexec/fmt_test.go | 37 +++++++++++++++++++++++++++++++++++++ tfexec/version.go | 1 + 4 files changed, 53 insertions(+) create mode 100644 tfexec/fmt_test.go diff --git a/tfexec/errors.go b/tfexec/errors.go index 010b3887..e7a6c804 100644 --- a/tfexec/errors.go +++ b/tfexec/errors.go @@ -129,6 +129,16 @@ func (e *ErrVersionMismatch) Error() string { return fmt.Sprintf("unexpected version %s (min: %s, max: %s)", e.Actual, e.MinInclusive, e.MaxExclusive) } +func (e *ErrVersionMismatch) Is(target error) bool { + err, ok := target.(*ErrVersionMismatch) + if !ok { + return false + } + return err.Actual == e.Actual && + err.MaxExclusive == e.MaxExclusive && + err.MinInclusive == e.MinInclusive +} + type ErrNoInit struct { stderr string } diff --git a/tfexec/fmt.go b/tfexec/fmt.go index 64473a5e..d80a39b7 100644 --- a/tfexec/fmt.go +++ b/tfexec/fmt.go @@ -121,6 +121,11 @@ func (tf *Terraform) FormatCheck(ctx context.Context, opts ...FormatOption) (boo } func (tf *Terraform) formatCmd(ctx context.Context, args []string, opts ...FormatOption) (*exec.Cmd, error) { + err := tf.compatible(ctx, tf0_7_7, nil) + if err != nil { + return nil, fmt.Errorf("fmt was first introduced in Terraform 0.7.7: %w", err) + } + c := defaultFormatConfig for _, o := range opts { diff --git a/tfexec/fmt_test.go b/tfexec/fmt_test.go new file mode 100644 index 00000000..932c0f09 --- /dev/null +++ b/tfexec/fmt_test.go @@ -0,0 +1,37 @@ +package tfexec + +import ( + "context" + "errors" + "os" + "testing" +) + +func TestFormat(t *testing.T) { + td := testTempDir(t) + defer os.RemoveAll(td) + + tf, err := NewTerraform(td, tfVersion(t, "0.7.6")) + if err != nil { + t.Fatal(err) + } + + // empty env, to avoid environ mismatch in testing + tf.SetEnv(map[string]string{}) + + t.Run("too old version", func(t *testing.T) { + _, err := tf.formatCmd(context.Background(), []string{}) + if err == nil { + t.Fatal("expected old version to fail") + } + + expectedErr := &ErrVersionMismatch{ + Actual: "0.7.6", + MinInclusive: "0.7.7", + MaxExclusive: "-", + } + if !errors.Is(err, expectedErr) { + t.Fatalf("error doesn't match: %#v", err) + } + }) +} diff --git a/tfexec/version.go b/tfexec/version.go index ae1dd5fa..6f3f1395 100644 --- a/tfexec/version.go +++ b/tfexec/version.go @@ -12,6 +12,7 @@ import ( ) var ( + tf0_7_7 = version.Must(version.NewVersion("0.7.7")) tf0_12_0 = version.Must(version.NewVersion("0.12.0")) tf0_13_0 = version.Must(version.NewVersion("0.13.0")) ) From 5f1461e1232e6f8a612e702d3cee8070293a46ee Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 25 Sep 2020 10:25:22 +0100 Subject: [PATCH 2/3] avoid error field checking --- tfexec/errors.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tfexec/errors.go b/tfexec/errors.go index e7a6c804..75e62ae1 100644 --- a/tfexec/errors.go +++ b/tfexec/errors.go @@ -130,13 +130,8 @@ func (e *ErrVersionMismatch) Error() string { } func (e *ErrVersionMismatch) Is(target error) bool { - err, ok := target.(*ErrVersionMismatch) - if !ok { - return false - } - return err.Actual == e.Actual && - err.MaxExclusive == e.MaxExclusive && - err.MinInclusive == e.MinInclusive + _, ok := target.(*ErrVersionMismatch) + return ok } type ErrNoInit struct { From 1792c628d5a4ee1f88a51cfe385b1e64ec117e40 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 25 Sep 2020 14:49:41 +0100 Subject: [PATCH 3/3] replace errors.Is with errors.As --- tfexec/errors.go | 5 ----- tfexec/fmt_test.go | 8 ++------ 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/tfexec/errors.go b/tfexec/errors.go index 75e62ae1..010b3887 100644 --- a/tfexec/errors.go +++ b/tfexec/errors.go @@ -129,11 +129,6 @@ func (e *ErrVersionMismatch) Error() string { return fmt.Sprintf("unexpected version %s (min: %s, max: %s)", e.Actual, e.MinInclusive, e.MaxExclusive) } -func (e *ErrVersionMismatch) Is(target error) bool { - _, ok := target.(*ErrVersionMismatch) - return ok -} - type ErrNoInit struct { stderr string } diff --git a/tfexec/fmt_test.go b/tfexec/fmt_test.go index 932c0f09..517c2d04 100644 --- a/tfexec/fmt_test.go +++ b/tfexec/fmt_test.go @@ -25,12 +25,8 @@ func TestFormat(t *testing.T) { t.Fatal("expected old version to fail") } - expectedErr := &ErrVersionMismatch{ - Actual: "0.7.6", - MinInclusive: "0.7.7", - MaxExclusive: "-", - } - if !errors.Is(err, expectedErr) { + var expectedErr *ErrVersionMismatch + if !errors.As(err, &expectedErr) { t.Fatalf("error doesn't match: %#v", err) } })