Repository navigation
Expand file tree
/
Copy pathtemplate_unix_test.go
More file actions
132 lines (104 loc) · 3.96 KB
/
Copy pathtemplate_unix_test.go
File metadata and controls
132 lines (104 loc) · 3.96 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build !windows
package gomplate
import (
"context"
"fmt"
"io/fs"
"testing"
"github.com/hack-pad/hackpadfs"
"github.com/hack-pad/hackpadfs/mem"
"github.com/hairyhenderson/gomplate/v5/internal/datafs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWalkDir_UNIX(t *testing.T) {
memfs, _ := mem.NewFS()
fsys := datafs.WrapWdFS(memfs)
ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file"))
cfg := &Config{}
_, err := walkDir(ctx, cfg, "/indir", simpleNamer("/outdir"), nil, nil, 0, false)
require.Error(t, err)
err = hackpadfs.MkdirAll(fsys, "/indir/one", 0o777)
require.NoError(t, err)
err = hackpadfs.MkdirAll(fsys, "/indir/two", 0o777)
require.NoError(t, err)
err = hackpadfs.WriteFullFile(fsys, "/indir/one/foo", []byte("foo"), 0o644)
require.NoError(t, err)
err = hackpadfs.WriteFullFile(fsys, "/indir/one/bar", []byte("bar"), 0o644)
require.NoError(t, err)
err = hackpadfs.WriteFullFile(fsys, "/indir/two/baz", []byte("baz"), 0o644)
require.NoError(t, err)
templates, err := walkDir(ctx, cfg, "/indir", simpleNamer("/outdir"), []string{"*/two"}, []string{}, 0, false)
require.NoError(t, err)
expected := []Template{
{
Name: "/indir/one/bar",
Text: "bar",
},
{
Name: "/indir/one/foo",
Text: "foo",
},
}
assert.Len(t, templates, 2)
for i, tmpl := range templates {
assert.Equal(t, expected[i].Name, tmpl.Name)
assert.Equal(t, expected[i].Text, tmpl.Text)
}
}
func TestWalkDir_IncludeIgnored(t *testing.T) {
memfs, _ := mem.NewFS()
fsys := datafs.WrapWdFS(memfs)
ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file"))
require.NoError(t, hackpadfs.MkdirAll(fsys, "/indir/vendor", 0o777))
require.NoError(t, hackpadfs.WriteFullFile(fsys, "/indir/.gomplateignore", []byte("vendor/\n"), 0o644))
require.NoError(t, hackpadfs.WriteFullFile(fsys, "/indir/a.tmpl", []byte("a"), 0o644))
require.NoError(t, hackpadfs.WriteFullFile(fsys, "/indir/vendor/b.tmpl", []byte("b"), 0o644))
// with no negated patterns, files in the ignorefile are just excluded
templates, err := walkDir(ctx, &Config{}, "/indir", simpleNamer("/outdir"), nil, nil, 0, false)
require.NoError(t, err)
assert.Contains(t, templateNames(templates), "/indir/a.tmpl")
assert.NotContains(t, templateNames(templates), "/indir/vendor/b.tmpl")
_, err = hackpadfs.Stat(fsys, "/outdir/vendor/b.tmpl")
require.ErrorIs(t, err, fs.ErrNotExist)
// a file that the ignorefile excludes but --include brings back is copied
// without being rendered. This documents the current behaviour.
templates, err = walkDir(ctx, &Config{}, "/indir", simpleNamer("/outdir"), []string{"*", "!*.tmpl"}, nil, 0, false)
require.NoError(t, err)
assert.Equal(t, []string{"/indir/a.tmpl"}, templateNames(templates))
b, err := hackpadfs.ReadFile(fsys, "/outdir/vendor/b.tmpl")
require.NoError(t, err)
assert.Equal(t, "b", string(b))
}
func templateNames(templates []Template) []string {
names := make([]string, len(templates))
for i, t := range templates {
names[i] = t.Name
}
return names
}
var walkDirSink []Template
func BenchmarkWalkDir(b *testing.B) {
memfs, _ := mem.NewFS()
fsys := datafs.WrapWdFS(memfs)
ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file"))
// 1000 files in 10 directories, with an ignorefile
require.NoError(b, hackpadfs.MkdirAll(fsys, "/indir", 0o777))
require.NoError(b, hackpadfs.WriteFullFile(fsys, "/indir/.gomplateignore", []byte("*.bak\n"), 0o644))
for d := range 10 {
dir := fmt.Sprintf("/indir/d%d", d)
require.NoError(b, hackpadfs.MkdirAll(fsys, dir, 0o777))
for f := range 100 {
name := fmt.Sprintf("%s/f%d.txt", dir, f)
require.NoError(b, hackpadfs.WriteFullFile(fsys, name, []byte("hello"), 0o644))
}
}
cfg := &Config{}
for b.Loop() {
var err error
walkDirSink, err = walkDir(ctx, cfg, "/indir", simpleNamer("/outdir"), nil, nil, 0, false)
if err != nil {
b.Fatal(err)
}
}
}