-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
110 lines (95 loc) · 2.46 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gofs
import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"text/template"
)
func TestFile(t *testing.T) {
a := assert.New(t)
fs := afero.NewMemMapFs()
f := FileAt("/tmp/testfile.log")
f.fs = fs
parentDir := DirAt("/tmp")
testContent := "go test\nanother line"
// test non existing file
a.False(f.Exists())
a.True(f.NotExists())
a.True(f.IsEmpty())
a.False(f.IsReadable())
a.True(f.IsWritable())
a.True(f.Dir().Equals(parentDir))
f.MustEnsureDir(0750)
a.True(f.HasAnyExtension())
a.True(f.HasExtension(ExtLog))
f.AssertExtension(ExtLog)
a.Equal("/tmp/testfile.pdf", f.WithExtension(ExtPdf).Path())
// test assertions
f.AssertNotExists()
f.AssertWritable()
f.AssertEmpty()
// create file
err := f.SetContentString(testContent)
a.Nil(err)
a.True(f.Exists())
a.False(f.NotExists())
a.False(f.IsEmpty())
f.AssertExists()
f.AssertReadable()
f.AssertNotEmpty()
// read content
byteContent := []byte(testContent)
b, err := f.Content()
a.Nil(err)
a.Equal(byteContent, b)
// read content (string)
c, err := f.ContentString()
a.Nil(err)
a.Equal(testContent, c)
extraContent := "third line"
err = f.AppendStringln(extraContent)
a.Nil(err)
a.Equal([]byte(testContent+extraContent+"\n"), f.MustContent())
a.Equal(testContent+extraContent+"\n", f.MustContentString())
// test io
fCopy := FileAt("/tmp/copy")
fCopy.fs = fs
err = f.CopyTo(fCopy)
a.Nil(err)
a.True(fCopy.Exists())
a.Equal(f.MustContentString(), fCopy.MustContentString())
// test hashing
expectedMD5Hash := "9656e885da2a0510f480c5ff8a6a57b5"
a.Equal(expectedMD5Hash, f.MustMd5Hash())
f.AssertMd5Hash(expectedMD5Hash)
// test template
outFile := FileAt("/tmp/template.out")
outFile.fs = fs
err = f.SetContentString("")
a.Nil(err)
err = f.AssertEmpty().AppendString("{{ .Name }} was here")
a.Nil(err)
err = f.MustRenderer().WithData(map[string]string{
"Name": "John",
}).RenderToFile(outFile)
a.Nil(err)
c, err = outFile.ContentString()
a.True(outFile.Exists())
a.False(outFile.IsEmpty())
a.Nil(err)
a.Equal("John was here", c)
// template with custom funcs
err = f.MustClear().AppendString("{{ .Name | toLower | toUpper }} was here")
a.Nil(err)
var buf strings.Builder
err = f.MustRenderer().WithData(map[string]string{
"Name": "Tom",
}).AddFuncs(template.FuncMap{
"toUpper": strings.ToUpper,
}).AddFuncs(template.FuncMap{
"toLower": strings.ToLower,
}).RenderTo(&buf)
a.Nil(err)
a.Equal("TOM was here", buf.String())
}