-
Notifications
You must be signed in to change notification settings - Fork 21
/
file_test.go
88 lines (78 loc) · 1.67 KB
/
file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package tfupdate
import (
"testing"
"github.com/spf13/afero"
)
func TestUpdateFile(t *testing.T) {
cases := []struct {
filename string
src string
o Option
want string
ok bool
}{
{
filename: "valid.tf",
src: `
terraform {
required_version = "0.12.6"
}
`,
o: Option{
updateType: "terraform",
target: "0.12.7",
},
want: `
terraform {
required_version = "0.12.7"
}
`,
ok: true,
},
{
filename: "invalid.tf",
src: `
terraform {
required_version = "0.12.6"
}
`,
o: Option{
updateType: "hoge",
target: "0.12.7",
},
want: `
terraform {
required_version = "0.12.6"
}
`,
ok: false,
},
}
for _, tc := range cases {
fs := afero.NewMemMapFs()
afero.WriteFile(fs, tc.filename, []byte(tc.src), 0644)
err := UpdateFile(fs, tc.filename, tc.o)
if tc.ok && err != nil {
t.Errorf("UpdateFile() with filename = %s, o = %#v returns unexpected err: %+v", tc.filename, tc.o, err)
}
if !tc.ok && err == nil {
t.Errorf("UpdateFile() with filename = %s, o = %#v expects to return an error, but no error: %+v", tc.filename, tc.o, err)
}
got, err := afero.ReadFile(fs, tc.filename)
if err != nil {
t.Fatalf("failed to read updated file: %s", err)
}
if string(got) != tc.want {
t.Errorf("UpdateFile() with filename = %s, o = %#v returns %s, but want = %s", tc.filename, tc.o, got, tc.want)
}
}
}
func TestUpdateFileNotFound(t *testing.T) {
fs := afero.NewMemMapFs()
filename := "not_found.tf"
o := Option{}
err := UpdateFile(fs, filename, o)
if err == nil {
t.Errorf("UpdateFile() with filename = %s, o = %#v expects to return an error, but no error", filename, o)
}
}