forked from mercari/hcledit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_test.go
88 lines (73 loc) · 1.61 KB
/
write_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 hcledit_test
import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"go.mercari.io/hcledit"
)
func TestWriteFile(t *testing.T) {
originalContent := `
attribute1 = "str1"
`
editor, err := hcledit.Read(strings.NewReader(originalContent), "")
if err != nil {
t.Fatal(err)
}
if err := editor.Create("attribute2", "C", hcledit.WithAfter("attribute1")); err != nil {
t.Fatal(err)
}
if err := editor.Update("attribute1", "U"); err != nil {
t.Fatal(err)
}
tempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "test.hcl")
if err := editor.WriteFile(tempFile); err != nil {
t.Fatal(err)
}
want := []byte(`
attribute1 = "U"
attribute2 = "C"
`)
got, err := ioutil.ReadFile(tempFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Errorf("WriteFile() mismatch:\ngot:%s\nwant:%s\n", got, want)
}
}
func TestOverWriteFile(t *testing.T) {
tempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "test.hcl")
if err := ioutil.WriteFile(tempFile, []byte(`
attribute1 = "str1"
`), 0o600); err != nil {
t.Fatal(err)
}
editor, err := hcledit.ReadFile(tempFile)
if err != nil {
t.Fatal(err)
}
if err := editor.Create("attribute2", "C", hcledit.WithAfter("attribute1")); err != nil {
t.Fatal(err)
}
if err := editor.Update("attribute1", "U"); err != nil {
t.Fatal(err)
}
if err := editor.OverWriteFile(); err != nil {
t.Fatal(err)
}
want := []byte(`
attribute1 = "U"
attribute2 = "C"
`)
got, err := ioutil.ReadFile(tempFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Errorf("WriteFile() mismatch:\ngot:%s\nwant:%s\n", got, want)
}
}