Skip to content

Commit

Permalink
fmt: Gate formatting command on 0.7.7
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Sep 25, 2020
1 parent 1100100 commit 78025b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tfexec/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions tfexec/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions tfexec/fmt_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
1 change: 1 addition & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
Expand Down

0 comments on commit 78025b1

Please sign in to comment.