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
|
// this is in a separate package so WriteFile can be more thoroughly tested
// without involving an import cycle with datafs
package iohelpers_test
import (
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/hack-pad/hackpadfs"
osfs "github.com/hack-pad/hackpadfs/os"
"github.com/hairyhenderson/gomplate/v4/internal/datafs"
"github.com/hairyhenderson/gomplate/v4/internal/iohelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tfs "gotest.tools/v3/fs"
)
func TestWrite(t *testing.T) {
oldwd, _ := os.Getwd()
defer os.Chdir(oldwd)
rootDir := tfs.NewDir(t, "gomplate-test")
t.Cleanup(rootDir.Remove)
// we want to use a real filesystem here, so we can test interactions with
// the current working directory
fsys := datafs.WrapWdFS(osfs.NewFS())
newwd := rootDir.Join("the", "path", "we", "want")
badwd := rootDir.Join("some", "other", "dir")
hackpadfs.MkdirAll(fsys, newwd, 0o755)
hackpadfs.MkdirAll(fsys, badwd, 0o755)
newwd, _ = filepath.EvalSymlinks(newwd)
badwd, _ = filepath.EvalSymlinks(badwd)
err := os.Chdir(newwd)
require.NoError(t, err)
err = iohelpers.WriteFile(fsys, "/foo", []byte("Hello world"))
require.Error(t, err)
rel, err := filepath.Rel(newwd, badwd)
require.NoError(t, err)
err = iohelpers.WriteFile(fsys, rel, []byte("Hello world"))
require.Error(t, err)
foopath := filepath.Join(newwd, "foo")
err = iohelpers.WriteFile(fsys, foopath, []byte("Hello world"))
require.NoError(t, err)
out, err := fs.ReadFile(fsys, foopath)
require.NoError(t, err)
assert.Equal(t, "Hello world", string(out))
err = iohelpers.WriteFile(fsys, foopath, []byte("truncate"))
require.NoError(t, err)
out, err = fs.ReadFile(fsys, foopath)
require.NoError(t, err)
assert.Equal(t, "truncate", string(out))
foopath = filepath.Join(newwd, "nonexistent", "subdir", "foo")
err = iohelpers.WriteFile(fsys, foopath, []byte("Hello subdirranean world!"))
require.NoError(t, err)
out, err = fs.ReadFile(fsys, foopath)
require.NoError(t, err)
assert.Equal(t, "Hello subdirranean world!", string(out))
}
|