github.com/pietrocarrara/hugo@v0.47.1/resource/testhelpers_test.go (about) 1 package resource 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "fmt" 8 "image" 9 "io" 10 "io/ioutil" 11 "os" 12 "path" 13 "runtime" 14 "strings" 15 16 "github.com/gohugoio/hugo/helpers" 17 "github.com/gohugoio/hugo/hugofs" 18 "github.com/gohugoio/hugo/media" 19 "github.com/gohugoio/hugo/output" 20 "github.com/spf13/afero" 21 "github.com/spf13/viper" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func newTestResourceSpec(assert *require.Assertions) *Spec { 26 return newTestResourceSpecForBaseURL(assert, "https://example.com/") 27 } 28 29 func newTestResourceSpecForBaseURL(assert *require.Assertions, baseURL string) *Spec { 30 cfg := viper.New() 31 cfg.Set("baseURL", baseURL) 32 cfg.Set("resourceDir", "resources") 33 cfg.Set("contentDir", "content") 34 cfg.Set("dataDir", "data") 35 cfg.Set("i18nDir", "i18n") 36 cfg.Set("layoutDir", "layouts") 37 cfg.Set("assetDir", "assets") 38 cfg.Set("archetypeDir", "archetypes") 39 cfg.Set("publishDir", "public") 40 41 imagingCfg := map[string]interface{}{ 42 "resampleFilter": "linear", 43 "quality": 68, 44 "anchor": "left", 45 } 46 47 cfg.Set("imaging", imagingCfg) 48 49 fs := hugofs.NewMem(cfg) 50 51 s, err := helpers.NewPathSpec(fs, cfg) 52 53 assert.NoError(err) 54 55 spec, err := NewSpec(s, nil, output.DefaultFormats, media.DefaultTypes) 56 assert.NoError(err) 57 return spec 58 } 59 60 func newTestResourceOsFs(assert *require.Assertions) *Spec { 61 cfg := viper.New() 62 cfg.Set("baseURL", "https://example.com") 63 64 workDir, err := ioutil.TempDir("", "hugores") 65 66 if runtime.GOOS == "darwin" && !strings.HasPrefix(workDir, "/private") { 67 // To get the entry folder in line with the rest. This its a little bit 68 // mysterious, but so be it. 69 workDir = "/private" + workDir 70 } 71 72 cfg.Set("workingDir", workDir) 73 cfg.Set("resourceDir", filepath.Join(workDir, "res")) 74 cfg.Set("contentDir", "content") 75 cfg.Set("dataDir", "data") 76 cfg.Set("i18nDir", "i18n") 77 cfg.Set("layoutDir", "layouts") 78 cfg.Set("assetDir", "assets") 79 cfg.Set("archetypeDir", "archetypes") 80 cfg.Set("publishDir", "public") 81 82 fs := hugofs.NewFrom(hugofs.Os, cfg) 83 fs.Destination = &afero.MemMapFs{} 84 85 s, err := helpers.NewPathSpec(fs, cfg) 86 87 assert.NoError(err) 88 89 spec, err := NewSpec(s, nil, output.DefaultFormats, media.DefaultTypes) 90 assert.NoError(err) 91 return spec 92 93 } 94 95 func fetchSunset(assert *require.Assertions) *Image { 96 return fetchImage(assert, "sunset.jpg") 97 } 98 99 func fetchImage(assert *require.Assertions, name string) *Image { 100 spec := newTestResourceSpec(assert) 101 return fetchImageForSpec(spec, assert, name) 102 } 103 104 func fetchImageForSpec(spec *Spec, assert *require.Assertions, name string) *Image { 105 r := fetchResourceForSpec(spec, assert, name) 106 assert.IsType(&Image{}, r) 107 return r.(*Image) 108 } 109 110 func fetchResourceForSpec(spec *Spec, assert *require.Assertions, name string) ContentResource { 111 src, err := os.Open(filepath.FromSlash("testdata/" + name)) 112 assert.NoError(err) 113 114 out, err := helpers.OpenFileForWriting(spec.BaseFs.Content.Fs, name) 115 assert.NoError(err) 116 _, err = io.Copy(out, src) 117 out.Close() 118 src.Close() 119 assert.NoError(err) 120 121 factory := func(s string) string { 122 return path.Join("/a", s) 123 } 124 125 r, err := spec.New(ResourceSourceDescriptor{TargetPathBuilder: factory, SourceFilename: name}) 126 assert.NoError(err) 127 128 return r.(ContentResource) 129 } 130 131 func assertImageFile(assert *require.Assertions, fs afero.Fs, filename string, width, height int) { 132 f, err := fs.Open(filename) 133 if err != nil { 134 printFs(fs, "", os.Stdout) 135 } 136 assert.NoError(err) 137 defer f.Close() 138 139 config, _, err := image.DecodeConfig(f) 140 assert.NoError(err) 141 142 assert.Equal(width, config.Width) 143 assert.Equal(height, config.Height) 144 } 145 146 func assertFileCache(assert *require.Assertions, fs afero.Fs, filename string, width, height int) { 147 assertImageFile(assert, fs, filepath.Join("_gen/images", filename), width, height) 148 } 149 150 func writeSource(t testing.TB, fs *hugofs.Fs, filename, content string) { 151 writeToFs(t, fs.Source, filename, content) 152 } 153 154 func writeToFs(t testing.TB, fs afero.Fs, filename, content string) { 155 if err := afero.WriteFile(fs, filepath.FromSlash(filename), []byte(content), 0755); err != nil { 156 t.Fatalf("Failed to write file: %s", err) 157 } 158 } 159 160 func printFs(fs afero.Fs, path string, w io.Writer) { 161 if fs == nil { 162 return 163 } 164 afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error { 165 if info != nil && !info.IsDir() { 166 s := path 167 if lang, ok := info.(hugofs.LanguageAnnouncer); ok { 168 s = s + "\t" + lang.Lang() 169 } 170 if fp, ok := info.(hugofs.FilePather); ok { 171 s += "\tFilename: " + fp.Filename() + "\tBase: " + fp.BaseDir() 172 } 173 fmt.Fprintln(w, " ", s) 174 } 175 return nil 176 }) 177 }