Skip to content

Commit

Permalink
Add UpdateHCL tests and remove debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Sep 14, 2019
1 parent b33dea5 commit 18436e5
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
10 changes: 1 addition & 9 deletions tfupdate/update.go
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"strings"

"github.com/hashicorp/hcl2/hcl"
Expand Down Expand Up @@ -59,40 +58,33 @@ func NewOption(updateType string, target string) Option {
// UpdateHCL reads HCL from io.Reader, updates version constraints
// and writes updated contents to io.Writer.
// Note that a filename is used only for an error message.
// If input HCL doesn't match a target of option, nothing is written to the output.
func UpdateHCL(r io.Reader, w io.Writer, filename string, o Option) error {
src, err := ioutil.ReadAll(r)
if err != nil {
return fmt.Errorf("failed to read input: %s", err)
}

log.Printf("[DEBUG] Parse HCL: %s", filename)
f, diags := hclwrite.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
return fmt.Errorf("failed to parse input: %s", diags)
}

log.Printf("[DEBUG] Initialize updater: %s, %#v", filename, o)
u, err := NewUpdater(o)
if err != nil {
return err
}

log.Printf("[DEBUG] Update HCL: %s", filename)
u.Update(f)
updated := f.BuildTokens(nil).Bytes()

// Write contents to buffer if changed.
if !bytes.Equal(src, updated) {
log.Printf("[DEBUG] Detect changes: %s", filename)
log.Printf("[DEBUG] Execute fmt: %s", filename)
result := hclwrite.Format(updated)

log.Printf("[DEBUG] Write buffer: %s", filename)
if _, err := w.Write(result); err != nil {
return fmt.Errorf("failed to write output: %s", err)
}
} else {
log.Printf("[DEBUG] No changes. Skip writing buffer: %s", filename)
}

return nil
Expand Down
100 changes: 100 additions & 0 deletions tfupdate/update_test.go
@@ -1,6 +1,7 @@
package tfupdate

import (
"bytes"
"reflect"
"testing"
)
Expand Down Expand Up @@ -65,3 +66,102 @@ func TestNewUpdater(t *testing.T) {
}
}
}

func TestUpdateHCL(t *testing.T) {
cases := []struct {
src string
o Option
want string
ok bool
}{
{
src: `
terraform {
required_version = "0.12.4"
}
`,
o: Option{
updateType: "terraform",
target: "0.12.7",
},
want: `
terraform {
required_version = "0.12.7"
}
`,
ok: true,
},
{
src: `
provider "aws" {
version = "2.11.0"
}
`,
o: Option{
updateType: "provider",
target: "aws@2.23.0",
},
want: `
provider "aws" {
version = "2.23.0"
}
`,
ok: true,
},
{
src: `
provider "aws" {
version = "2.11.0"
}
`,
o: Option{
updateType: "provider",
target: "hoge@2.23.0",
},
want: "",
ok: true,
},
{
src: `
provider "invalid" {
`,
o: Option{
updateType: "provider",
target: "hoge@2.23.0",
},
want: "",
ok: false,
},
{
src: `
provider "aws" {
version = "2.11.0"
}
`,
o: Option{
updateType: "hoge",
target: "0.0.1",
},
want: "",
ok: false,
},
}

for _, tc := range cases {
r := bytes.NewBufferString(tc.src)
w := &bytes.Buffer{}
err := UpdateHCL(r, w, "test", tc.o)
if tc.ok && err != nil {
t.Errorf("UpdateHCL() with src = %s, o = %#v returns unexpected err: %+v", tc.src, tc.o, err)
}

if !tc.ok && err == nil {
t.Errorf("UpdateHCL() with src = %s, o = %#v expects to return an error, but no error: %+v", tc.src, tc.o, err)
}

got := string(w.Bytes())
if got != tc.want {
t.Errorf("UpdateHCL() with src = %s, o = %#v returns %s, but want = %s", tc.src, tc.o, got, tc.want)
}
}
}

0 comments on commit 18436e5

Please sign in to comment.