| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugofs |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"path/filepath" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/para" |
| 25 |
|
| 26 |
"github.com/spf13/afero" |
| 27 |
|
| 28 |
qt "github.com/frankban/quicktest" |
| 29 |
) |
| 30 |
|
| 31 |
func TestWalk(t *testing.T) { |
| 32 |
c := qt.New(t) |
| 33 |
|
| 34 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 35 |
|
| 36 |
afero.WriteFile(fs, "b.txt", []byte("content"), 0o777) |
| 37 |
afero.WriteFile(fs, "c.txt", []byte("content"), 0o777) |
| 38 |
afero.WriteFile(fs, "a.txt", []byte("content"), 0o777) |
| 39 |
|
| 40 |
names, err := collectPaths(fs, "") |
| 41 |
|
| 42 |
c.Assert(err, qt.IsNil) |
| 43 |
c.Assert(names, qt.DeepEquals, []string{"/a.txt", "/b.txt", "/c.txt"}) |
| 44 |
} |
| 45 |
|
| 46 |
func TestWalkRootMappingFs(t *testing.T) { |
| 47 |
c := qt.New(t) |
| 48 |
|
| 49 |
prepare := func(c *qt.C) afero.Fs { |
| 50 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 51 |
|
| 52 |
testfile := "test.txt" |
| 53 |
|
| 54 |
c.Assert(afero.WriteFile(fs, filepath.Join("a/b", testfile), []byte("some content"), 0o755), qt.IsNil) |
| 55 |
c.Assert(afero.WriteFile(fs, filepath.Join("c/d", testfile), []byte("some content"), 0o755), qt.IsNil) |
| 56 |
c.Assert(afero.WriteFile(fs, filepath.Join("e/f", testfile), []byte("some content"), 0o755), qt.IsNil) |
| 57 |
|
| 58 |
rm := []*RootMapping{ |
| 59 |
{ |
| 60 |
From: "static/b", |
| 61 |
To: "e/f", |
| 62 |
}, |
| 63 |
{ |
| 64 |
From: "static/a", |
| 65 |
To: "c/d", |
| 66 |
}, |
| 67 |
|
| 68 |
{ |
| 69 |
From: "static/c", |
| 70 |
To: "a/b", |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
rfs, err := NewRootMappingFs(fs, rm...) |
| 75 |
c.Assert(err, qt.IsNil) |
| 76 |
return NewBasePathFs(rfs, "static") |
| 77 |
} |
| 78 |
|
| 79 |
c.Run("Basic", func(c *qt.C) { |
| 80 |
bfs := prepare(c) |
| 81 |
|
| 82 |
names, err := collectPaths(bfs, "") |
| 83 |
|
| 84 |
c.Assert(err, qt.IsNil) |
| 85 |
c.Assert(names, qt.DeepEquals, []string{"/a/test.txt", "/b/test.txt", "/c/test.txt"}) |
| 86 |
}) |
| 87 |
|
| 88 |
c.Run("Para", func(c *qt.C) { |
| 89 |
bfs := prepare(c) |
| 90 |
|
| 91 |
p := para.New(4) |
| 92 |
r, _ := p.Start(context.Background()) |
| 93 |
|
| 94 |
for range 8 { |
| 95 |
r.Run(func() error { |
| 96 |
_, err := collectPaths(bfs, "") |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
fi, err := bfs.Stat("b/test.txt") |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
meta := fi.(FileMetaInfo).Meta() |
| 105 |
if meta.Filename == "" { |
| 106 |
return errors.New("fail") |
| 107 |
} |
| 108 |
return nil |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
c.Assert(r.Wait(), qt.IsNil) |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
func collectPaths(fs afero.Fs, root string) ([]string, error) { |
| 117 |
var names []string |
| 118 |
|
| 119 |
walkFn := func(ctx context.Context, path string, info FileMetaInfo) error { |
| 120 |
if info.IsDir() { |
| 121 |
return nil |
| 122 |
} |
| 123 |
names = append(names, info.Meta().PathInfo.Path()) |
| 124 |
|
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
w := NewWalkway(WalkwayConfig{Fs: fs, Root: root, WalkFn: walkFn, SortDirEntries: true, FailOnNotExist: true}) |
| 129 |
|
| 130 |
err := w.Walk() |
| 131 |
|
| 132 |
return names, err |
| 133 |
} |
| 134 |
|
| 135 |
func collectFileinfos(fs afero.Fs, root string) ([]FileMetaInfo, error) { |
| 136 |
var fis []FileMetaInfo |
| 137 |
|
| 138 |
walkFn := func(ctx context.Context, path string, info FileMetaInfo) error { |
| 139 |
fis = append(fis, info) |
| 140 |
|
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
w := NewWalkway(WalkwayConfig{Fs: fs, Root: root, WalkFn: walkFn, SortDirEntries: true, FailOnNotExist: true}) |
| 145 |
|
| 146 |
err := w.Walk() |
| 147 |
|
| 148 |
return fis, err |
| 149 |
} |
| 150 |
|
| 151 |
func BenchmarkWalk(b *testing.B) { |
| 152 |
c := qt.New(b) |
| 153 |
fs := NewBaseFileDecorator(afero.NewMemMapFs()) |
| 154 |
|
| 155 |
writeFiles := func(dir string, numfiles int) { |
| 156 |
for i := range numfiles { |
| 157 |
filename := filepath.Join(dir, fmt.Sprintf("file%d.txt", i)) |
| 158 |
c.Assert(afero.WriteFile(fs, filename, []byte("content"), 0o777), qt.IsNil) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
const numFilesPerDir = 20 |
| 163 |
|
| 164 |
writeFiles("root", numFilesPerDir) |
| 165 |
writeFiles("root/l1_1", numFilesPerDir) |
| 166 |
writeFiles("root/l1_1/l2_1", numFilesPerDir) |
| 167 |
writeFiles("root/l1_1/l2_2", numFilesPerDir) |
| 168 |
writeFiles("root/l1_2", numFilesPerDir) |
| 169 |
writeFiles("root/l1_2/l2_1", numFilesPerDir) |
| 170 |
writeFiles("root/l1_3", numFilesPerDir) |
| 171 |
|
| 172 |
walkFn := func(ctx context.Context, path string, info FileMetaInfo) error { |
| 173 |
if info.IsDir() { |
| 174 |
return nil |
| 175 |
} |
| 176 |
|
| 177 |
filename := info.Meta().Filename |
| 178 |
if !strings.HasPrefix(filename, "root") { |
| 179 |
return errors.New(filename) |
| 180 |
} |
| 181 |
|
| 182 |
return nil |
| 183 |
} |
| 184 |
|
| 185 |
for b.Loop() { |
| 186 |
w := NewWalkway(WalkwayConfig{Fs: fs, Root: "root", WalkFn: walkFn}) |
| 187 |
|
| 188 |
if err := w.Walk(); err != nil { |
| 189 |
b.Fatal(err) |
| 190 |
} |
| 191 |
} |
| 192 |
} |