github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/testhelpers_test.go (about) 1 package resources 2 3 import ( 4 "image" 5 "io" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 13 "github.com/gohugoio/hugo/config" 14 "github.com/gohugoio/hugo/langs" 15 "github.com/gohugoio/hugo/modules" 16 17 qt "github.com/frankban/quicktest" 18 "github.com/gohugoio/hugo/cache/filecache" 19 "github.com/gohugoio/hugo/helpers" 20 "github.com/gohugoio/hugo/hugofs" 21 "github.com/gohugoio/hugo/media" 22 "github.com/gohugoio/hugo/output" 23 "github.com/gohugoio/hugo/resources/page" 24 "github.com/gohugoio/hugo/resources/resource" 25 "github.com/spf13/afero" 26 ) 27 28 type specDescriptor struct { 29 baseURL string 30 c *qt.C 31 fs afero.Fs 32 } 33 34 func createTestCfg() config.Provider { 35 cfg := config.New() 36 cfg.Set("resourceDir", "resources") 37 cfg.Set("contentDir", "content") 38 cfg.Set("dataDir", "data") 39 cfg.Set("i18nDir", "i18n") 40 cfg.Set("layoutDir", "layouts") 41 cfg.Set("assetDir", "assets") 42 cfg.Set("archetypeDir", "archetypes") 43 cfg.Set("publishDir", "public") 44 45 langs.LoadLanguageSettings(cfg, nil) 46 mod, err := modules.CreateProjectModule(cfg) 47 if err != nil { 48 panic(err) 49 } 50 cfg.Set("allModules", modules.Modules{mod}) 51 52 return cfg 53 } 54 55 func newTestResourceSpec(desc specDescriptor) *Spec { 56 baseURL := desc.baseURL 57 if baseURL == "" { 58 baseURL = "https://example.com/" 59 } 60 61 afs := desc.fs 62 if afs == nil { 63 afs = afero.NewMemMapFs() 64 } 65 66 afs = hugofs.NewBaseFileDecorator(afs) 67 68 c := desc.c 69 70 cfg := createTestCfg() 71 cfg.Set("baseURL", baseURL) 72 73 imagingCfg := map[string]interface{}{ 74 "resampleFilter": "linear", 75 "quality": 68, 76 "anchor": "left", 77 } 78 79 cfg.Set("imaging", imagingCfg) 80 81 fs := hugofs.NewFrom(afs, cfg) 82 fs.Destination = hugofs.NewCreateCountingFs(fs.Destination) 83 84 s, err := helpers.NewPathSpec(fs, cfg, nil) 85 c.Assert(err, qt.IsNil) 86 87 filecaches, err := filecache.NewCaches(s) 88 c.Assert(err, qt.IsNil) 89 90 spec, err := NewSpec(s, filecaches, nil, nil, nil, output.DefaultFormats, media.DefaultTypes) 91 c.Assert(err, qt.IsNil) 92 return spec 93 } 94 95 func newTargetPaths(link string) func() page.TargetPaths { 96 return func() page.TargetPaths { 97 return page.TargetPaths{ 98 SubResourceBaseTarget: filepath.FromSlash(link), 99 SubResourceBaseLink: link, 100 } 101 } 102 } 103 104 func newTestResourceOsFs(c *qt.C) (*Spec, string) { 105 cfg := createTestCfg() 106 cfg.Set("baseURL", "https://example.com") 107 108 workDir, err := ioutil.TempDir("", "hugores") 109 c.Assert(err, qt.IsNil) 110 c.Assert(workDir, qt.Not(qt.Equals), "") 111 112 if runtime.GOOS == "darwin" && !strings.HasPrefix(workDir, "/private") { 113 // To get the entry folder in line with the rest. This its a little bit 114 // mysterious, but so be it. 115 workDir = "/private" + workDir 116 } 117 118 cfg.Set("workingDir", workDir) 119 120 fs := hugofs.NewFrom(hugofs.NewBaseFileDecorator(hugofs.Os), cfg) 121 fs.Destination = &afero.MemMapFs{} 122 123 s, err := helpers.NewPathSpec(fs, cfg, nil) 124 c.Assert(err, qt.IsNil) 125 126 filecaches, err := filecache.NewCaches(s) 127 c.Assert(err, qt.IsNil) 128 129 spec, err := NewSpec(s, filecaches, nil, nil, nil, output.DefaultFormats, media.DefaultTypes) 130 c.Assert(err, qt.IsNil) 131 132 return spec, workDir 133 } 134 135 func fetchSunset(c *qt.C) resource.Image { 136 return fetchImage(c, "sunset.jpg") 137 } 138 139 func fetchImage(c *qt.C, name string) resource.Image { 140 spec := newTestResourceSpec(specDescriptor{c: c}) 141 return fetchImageForSpec(spec, c, name) 142 } 143 144 func fetchImageForSpec(spec *Spec, c *qt.C, name string) resource.Image { 145 r := fetchResourceForSpec(spec, c, name) 146 147 img := r.(resource.Image) 148 149 c.Assert(img, qt.Not(qt.IsNil)) 150 c.Assert(img.(specProvider).getSpec(), qt.Not(qt.IsNil)) 151 152 return img 153 } 154 155 func fetchResourceForSpec(spec *Spec, c *qt.C, name string, targetPathAddends ...string) resource.ContentResource { 156 src, err := os.Open(filepath.FromSlash("testdata/" + name)) 157 c.Assert(err, qt.IsNil) 158 workDir := spec.WorkingDir 159 if len(targetPathAddends) > 0 { 160 addends := strings.Join(targetPathAddends, "_") 161 name = addends + "_" + name 162 } 163 targetFilename := filepath.Join(workDir, name) 164 out, err := helpers.OpenFileForWriting(spec.Fs.Source, targetFilename) 165 c.Assert(err, qt.IsNil) 166 _, err = io.Copy(out, src) 167 out.Close() 168 src.Close() 169 c.Assert(err, qt.IsNil) 170 171 factory := newTargetPaths("/a") 172 173 r, err := spec.New(ResourceSourceDescriptor{Fs: spec.Fs.Source, TargetPaths: factory, LazyPublish: true, RelTargetFilename: name, SourceFilename: targetFilename}) 174 c.Assert(err, qt.IsNil) 175 c.Assert(r, qt.Not(qt.IsNil)) 176 177 return r.(resource.ContentResource) 178 } 179 180 func assertImageFile(c *qt.C, fs afero.Fs, filename string, width, height int) { 181 filename = filepath.Clean(filename) 182 f, err := fs.Open(filename) 183 c.Assert(err, qt.IsNil) 184 defer f.Close() 185 186 config, _, err := image.DecodeConfig(f) 187 c.Assert(err, qt.IsNil) 188 189 c.Assert(config.Width, qt.Equals, width) 190 c.Assert(config.Height, qt.Equals, height) 191 } 192 193 func assertFileCache(c *qt.C, fs afero.Fs, filename string, width, height int) { 194 assertImageFile(c, fs, filepath.Clean(filename), width, height) 195 } 196 197 func writeSource(t testing.TB, fs *hugofs.Fs, filename, content string) { 198 writeToFs(t, fs.Source, filename, content) 199 } 200 201 func writeToFs(t testing.TB, fs afero.Fs, filename, content string) { 202 if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0755); err != nil { 203 t.Fatalf("Failed to write file: %s", err) 204 } 205 }