summaryrefslogtreecommitdiffstats
path: root/deploy/deploy_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'deploy/deploy_test.go')
-rw-r--r--deploy/deploy_test.go69
1 files changed, 66 insertions, 3 deletions
diff --git a/deploy/deploy_test.go b/deploy/deploy_test.go
index 12967fba0..17dffc25a 100644
--- a/deploy/deploy_test.go
+++ b/deploy/deploy_test.go
@@ -216,8 +216,9 @@ func TestFindDiffs(t *testing.T) {
func TestWalkLocal(t *testing.T) {
tests := map[string]struct {
- Given []string
- Expect []string
+ Given []string
+ Expect []string
+ MapPath func(string) string
}{
"Empty": {
Given: []string{},
@@ -235,6 +236,11 @@ func TestWalkLocal(t *testing.T) {
Given: []string{"file.txt", ".hidden_dir/file.txt", ".well-known/file.txt"},
Expect: []string{"file.txt", ".well-known/file.txt"},
},
+ "StripIndexHTML": {
+ Given: []string{"index.html", "file.txt", "dir/index.html", "dir/file.txt"},
+ Expect: []string{"index.html", "file.txt", "dir/", "dir/file.txt"},
+ MapPath: stripIndexHTML,
+ },
}
for desc, tc := range tests {
@@ -254,7 +260,7 @@ func TestWalkLocal(t *testing.T) {
}
}
d := newDeployer()
- if got, err := d.walkLocal(fs, nil, nil, nil, media.DefaultTypes); err != nil {
+ if got, err := d.walkLocal(fs, nil, nil, nil, media.DefaultTypes, tc.MapPath); err != nil {
t.Fatal(err)
} else {
expect := map[string]any{}
@@ -274,6 +280,63 @@ func TestWalkLocal(t *testing.T) {
}
}
+func TestStripIndexHTML(t *testing.T) {
+ tests := map[string]struct {
+ Input string
+ Output string
+ }{
+ "Unmapped": {Input: "normal_file.txt", Output: "normal_file.txt"},
+ "Stripped": {Input: "directory/index.html", Output: "directory/"},
+ "NoSlash": {Input: "prefix_index.html", Output: "prefix_index.html"},
+ "Root": {Input: "index.html", Output: "index.html"},
+ }
+ for desc, tc := range tests {
+ t.Run(desc, func(t *testing.T) {
+ got := stripIndexHTML(tc.Input)
+ if got != tc.Output {
+ t.Errorf("got %q, expect %q", got, tc.Output)
+ }
+ })
+ }
+}
+
+func TestStripIndexHTMLMatcher(t *testing.T) {
+ // StripIndexHTML should not affect matchers.
+ fs := afero.NewMemMapFs()
+ if err := fs.Mkdir("dir", 0o755); err != nil {
+ t.Fatal(err)
+ }
+ for _, name := range []string{"index.html", "dir/index.html", "file.txt"} {
+ if fd, err := fs.Create(name); err != nil {
+ t.Fatal(err)
+ } else {
+ fd.Close()
+ }
+ }
+ d := newDeployer()
+ const pattern = `\.html$`
+ matcher := &deployconfig.Matcher{Pattern: pattern, Gzip: true, Re: regexp.MustCompile(pattern)}
+ if got, err := d.walkLocal(fs, []*deployconfig.Matcher{matcher}, nil, nil, media.DefaultTypes, stripIndexHTML); err != nil {
+ t.Fatal(err)
+ } else {
+ for _, name := range []string{"index.html", "dir/"} {
+ lf := got[name]
+ if lf == nil {
+ t.Errorf("missing file %q", name)
+ } else if lf.matcher == nil {
+ t.Errorf("file %q has nil matcher, expect %q", name, pattern)
+ }
+ }
+ const name = "file.txt"
+ lf := got[name]
+ if lf == nil {
+ t.Errorf("missing file %q", name)
+ } else if lf.matcher != nil {
+ t.Errorf("file %q has matcher %q, expect nil", name, lf.matcher.Pattern)
+ }
+ }
+}
+
func TestLocalFile(t *testing.T) {
const (
content = "hello world!"