Debian Codegraph @

resources/testhelpers_test.go

raw
1 package resources_test
2
3 import (
4 "image"
5 "os"
6 "path/filepath"
7 "runtime"
8 "strings"
9
10 "github.com/gohugoio/hugo/common/hugio"
11 "github.com/gohugoio/hugo/config"
12 "github.com/gohugoio/hugo/config/testconfig"
13 "github.com/gohugoio/hugo/deps"
14 "github.com/gohugoio/hugo/identity"
15 "github.com/gohugoio/hugo/resources"
16
17 qt "github.com/frankban/quicktest"
18 "github.com/gohugoio/hugo/hugofs"
19 "github.com/gohugoio/hugo/resources/images"
20 "github.com/gohugoio/hugo/resources/resource"
21 "github.com/spf13/afero"
22 )
23
24 type specDescriptor struct {
25 baseURL string
26 c *qt.C
27 fs afero.Fs
28 imagingMeta map[string]any
29 }
30
31 func newTestResourceSpec(desc specDescriptor) *resources.Spec {
32 baseURL := desc.baseURL
33 if baseURL == "" {
34 baseURL = "https://example.com/"
35 }
36
37 afs := desc.fs
38 if afs == nil {
39 afs = afero.NewMemMapFs()
40 }
41
42 if hugofs.IsOsFs(afs) {
43 panic("osFs not supported for this test")
44 }
45
46 if err := afs.MkdirAll("assets", 0o755); err != nil {
47 panic(err)
48 }
49
50 cfg := config.New()
51 cfg.Set("baseURL", baseURL)
52 cfg.Set("publishDir", "public")
53
54 imagingCfg := map[string]any{
55 "resampleFilter": "linear",
56 "quality": 68,
57 "anchor": "left",
58 }
59
60 if desc.imagingMeta != nil {
61 imagingCfg["meta"] = desc.imagingMeta
62 }
63
64 cfg.Set("imaging", imagingCfg)
65 d := testconfig.GetTestDeps(
66 afs, cfg,
67 func(d *deps.Deps) { d.Fs.PublishDir = hugofs.NewCreateCountingFs(d.Fs.PublishDir) },
68 )
69
70 desc.c.Cleanup(func() {
71 if err := d.Close(); err != nil {
72 panic(err)
73 }
74 })
75
76 return d.ResourceSpec
77 }
78
79 func newTestResourceOsFs(c *qt.C) (*resources.Spec, string) {
80 cfg := config.New()
81 cfg.Set("baseURL", "https://example.com")
82
83 workDir, err := os.MkdirTemp("", "hugores")
84 c.Assert(err, qt.IsNil)
85 c.Assert(workDir, qt.Not(qt.Equals), "")
86
87 if runtime.GOOS == "darwin" && !strings.HasPrefix(workDir, "/private") {
88 // To get the entry folder in line with the rest. This its a little bit
89 // mysterious, but so be it.
90 workDir = "/private" + workDir
91 }
92
93 cfg.Set("workingDir", workDir)
94
95 os.MkdirAll(filepath.Join(workDir, "assets"), 0o755)
96
97 d := testconfig.GetTestDeps(hugofs.Os, cfg)
98
99 return d.ResourceSpec, workDir
100 }
101
102 func fetchSunset(c *qt.C) (*resources.Spec, images.ImageResource) {
103 return fetchImage(c, "sunset.jpg")
104 }
105
106 func fetchImage(c *qt.C, name string) (*resources.Spec, images.ImageResource) {
107 spec := newTestResourceSpec(specDescriptor{c: c})
108 return spec, fetchImageForSpec(spec, c, name)
109 }
110
111 func fetchImageForSpec(spec *resources.Spec, c *qt.C, name string) images.ImageResource {
112 r := fetchResourceForSpec(spec, c, name)
113 img := r.(images.ImageResource)
114 c.Assert(img, qt.Not(qt.IsNil))
115 return img
116 }
117
118 func fetchResourceForSpec(spec *resources.Spec, c *qt.C, name string, targetPathAddends ...string) resource.ContentResource {
119 b, err := os.ReadFile(filepath.FromSlash("testdata/" + name))
120 c.Assert(err, qt.IsNil)
121 // Create a new reader each time to avoid race conditions when multiple
122 // goroutines access the same resource concurrently.
123 open := func() (hugio.ReadSeekCloser, error) {
124 return hugio.NewReadSeekerNoOpCloserFromBytes(b), nil
125 }
126 targetPath := name
127 base := "/a/"
128 r, err := spec.NewResource(resources.ResourceSourceDescriptor{
129 LazyPublish: true,
130 NameNormalized: name, TargetPath: targetPath, BasePathRelPermalink: base, BasePathTargetPath: base, OpenReadSeekCloser: open,
131 GroupIdentity: identity.Anonymous,
132 })
133 c.Assert(err, qt.IsNil)
134 c.Assert(r, qt.Not(qt.IsNil))
135
136 return r.(resource.ContentResource)
137 }
138
139 func assertImageFile(c *qt.C, fs afero.Fs, filename string, width, height int) {
140 filename = filepath.Clean(filename)
141 f, err := fs.Open(filename)
142 c.Assert(err, qt.IsNil)
143 defer f.Close()
144
145 config, _, err := image.DecodeConfig(f)
146 c.Assert(err, qt.IsNil)
147
148 c.Assert(config.Width, qt.Equals, width)
149 c.Assert(config.Height, qt.Equals, height)
150 }