testhelpers_test.go - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
testhelpers_test.go (3581B)
---
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 }
29
30 func newTestResourceSpec(desc specDescriptor) *resources.Spec {
31 baseURL := desc.baseURL
32 if baseURL == "" {
33 baseURL = "https://example.com/"
34 }
35
36 afs := desc.fs
37 if afs == nil {
38 afs = afero.NewMemMapFs()
39 }
40
41 if hugofs.IsOsFs(afs) {
42 panic("osFs not supported for this test")
43 }
44
45 if err := afs.MkdirAll("assets", 0o755); err != nil {
46 panic(err)
47 }
48
49 cfg := config.New()
50 cfg.Set("baseURL", baseURL)
51 cfg.Set("publishDir", "public")
52
53 imagingCfg := map[string]any{
54 "resampleFilter": "linear",
55 "quality": 68,
56 "anchor": "left",
57 }
58
59 cfg.Set("imaging", imagingCfg)
60 d := testconfig.GetTestDeps(
61 afs, cfg,
62 func(d *deps.Deps) { d.Fs.PublishDir = hugofs.NewCreateCountingFs(d.Fs.PublishDir) },
63 )
64
65 desc.c.Cleanup(func() {
66 if err := d.Close(); err != nil {
67 panic(err)
68 }
69 })
70
71 return d.ResourceSpec
72 }
73
74 func newTestResourceOsFs(c *qt.C) (*resources.Spec, string) {
75 cfg := config.New()
76 cfg.Set("baseURL", "https://example.com")
77
78 workDir, err := os.MkdirTemp("", "hugores")
79 c.Assert(err, qt.IsNil)
80 c.Assert(workDir, qt.Not(qt.Equals), "")
81
82 if runtime.GOOS == "darwin" && !strings.HasPrefix(workDir, "/private") {
83 // To get the entry folder in line with the rest. This its a little bit
84 // mysterious, but so be it.
85 workDir = "/private" + workDir
86 }
87
88 cfg.Set("workingDir", workDir)
89
90 os.MkdirAll(filepath.Join(workDir, "assets"), 0o755)
91
92 d := testconfig.GetTestDeps(hugofs.Os, cfg)
93
94 return d.ResourceSpec, workDir
95 }
96
97 func fetchSunset(c *qt.C) (*resources.Spec, images.ImageResource) {
98 return fetchImage(c, "sunset.jpg")
99 }
100
101 func fetchImage(c *qt.C, name string) (*resources.Spec, images.ImageResource) {
102 spec := newTestResourceSpec(specDescriptor{c: c})
103 return spec, fetchImageForSpec(spec, c, name)
104 }
105
106 func fetchImageForSpec(spec *resources.Spec, c *qt.C, name string) images.ImageResource {
107 r := fetchResourceForSpec(spec, c, name)
108 img := r.(images.ImageResource)
109 c.Assert(img, qt.Not(qt.IsNil))
110 return img
111 }
112
113 func fetchResourceForSpec(spec *resources.Spec, c *qt.C, name string, targetPathAddends ...string) resource.ContentResource {
114 b, err := os.ReadFile(filepath.FromSlash("testdata/" + name))
115 c.Assert(err, qt.IsNil)
116 open := hugio.NewOpenReadSeekCloser(hugio.NewReadSeekerNoOpCloserFromBytes(b))
117 targetPath := name
118 base := "/a/"
119 r, err := spec.NewResource(resources.ResourceSourceDescriptor{
120 LazyPublish: true,
121 NameNormalized: name, TargetPath: targetPath, BasePathRelPermalink: base, BasePathTargetPath: base, OpenReadSeekCloser: open,
122 GroupIdentity: identity.Anonymous,
123 })
124 c.Assert(err, qt.IsNil)
125 c.Assert(r, qt.Not(qt.IsNil))
126
127 return r.(resource.ContentResource)
128 }
129
130 func assertImageFile(c *qt.C, fs afero.Fs, filename string, width, height int) {
131 filename = filepath.Clean(filename)
132 f, err := fs.Open(filename)
133 c.Assert(err, qt.IsNil)
134 defer f.Close()
135
136 config, _, err := image.DecodeConfig(f)
137 c.Assert(err, qt.IsNil)
138
139 c.Assert(config.Width, qt.Equals, width)
140 c.Assert(config.Height, qt.Equals, height)
141 }