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