pages_capture_test.go (2216B)
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 hugolib
15
16 import (
17 "context"
18 "fmt"
19 "path/filepath"
20 "testing"
21
22 "github.com/gohugoio/hugo/helpers"
23 "github.com/gohugoio/hugo/source"
24
25 "github.com/gohugoio/hugo/common/loggers"
26
27 qt "github.com/frankban/quicktest"
28 "github.com/gohugoio/hugo/hugofs"
29 "github.com/spf13/afero"
30 )
31
32 func TestPagesCapture(t *testing.T) {
33 cfg, hfs := newTestCfg()
34 fs := hfs.Source
35
36 c := qt.New(t)
37
38 writeFile := func(filename string) {
39 c.Assert(afero.WriteFile(fs, filepath.FromSlash(filename), []byte(fmt.Sprintf("content-%s", filename)), 0755), qt.IsNil)
40 }
41
42 writeFile("_index.md")
43 writeFile("logo.png")
44 writeFile("root.md")
45 writeFile("blog/index.md")
46 writeFile("blog/hello.md")
47 writeFile("blog/images/sunset.png")
48 writeFile("pages/page1.md")
49 writeFile("pages/page2.md")
50 writeFile("pages/page.png")
51
52 ps, err := helpers.NewPathSpec(hugofs.NewFrom(fs, cfg), cfg, loggers.NewErrorLogger())
53 c.Assert(err, qt.IsNil)
54 sourceSpec := source.NewSourceSpec(ps, nil, fs)
55
56 t.Run("Collect", func(t *testing.T) {
57 c := qt.New(t)
58 proc := &testPagesCollectorProcessor{}
59 coll := newPagesCollector(sourceSpec, nil, loggers.NewErrorLogger(), nil, proc)
60 c.Assert(coll.Collect(), qt.IsNil)
61 c.Assert(len(proc.items), qt.Equals, 4)
62 })
63 }
64
65 type testPagesCollectorProcessor struct {
66 items []any
67 waitErr error
68 }
69
70 func (proc *testPagesCollectorProcessor) Process(item any) error {
71 proc.items = append(proc.items, item)
72 return nil
73 }
74
75 func (proc *testPagesCollectorProcessor) Start(ctx context.Context) context.Context {
76 return ctx
77 }
78
79 func (proc *testPagesCollectorProcessor) Wait() error { return proc.waitErr }