package document import ( "io" "os" "path/filepath" "testing" "time" "gotest.tools/v3/assert" ) func TestExecuteAllUsesCachedImageMetadata(t *testing.T) { tmp := t.TempDir() cwd, err := os.Getwd() assert.NilError(t, err) assert.NilError(t, os.Chdir(tmp)) t.Cleanup(func() { _ = os.Chdir(cwd) }) srcImg := filepath.Join("src", "img", "2023", "trip", "IMG_0385.JPG") assert.NilError(t, os.MkdirAll(filepath.Dir(srcImg), 0o755)) sourceData, err := os.ReadFile(filepath.Join(cwd, "testdata", "IMG_0385.JPG")) assert.NilError(t, err) const wantAlt = "Cached embedded alt text" const wantPurchaseURL = "https://example.com/cached-purchase" packet := xmpPacket(` ` + wantAlt + ` ` + wantPurchaseURL + ``) assert.NilError(t, os.WriteFile(srcImg, addXMPToJPEG(sourceData, []byte(packet)), 0o644)) distImg := filepath.Join("dist", "img", "2023", "trip", "IMG_0385.webp") assert.NilError(t, os.MkdirAll(filepath.Dir(distImg), 0o755)) assert.NilError(t, copyFile(filepath.Join(cwd, "testdata", "IMG_0385.webp"), distImg)) oldTime := time.Unix(1, 0) assert.NilError(t, os.Chtimes(distImg, oldTime, oldTime)) cfg := &Config{ Description: "desc", Dist: "dist", Name: "Test Site", Since: 2024, } cfg.Author.Name = "Tester" cfg.Author.Email = "tester@example.com" cfg.Gear = []Gear{{ Link: "https://example.com/canon-eos-rebel-t7", Make: "Canon", Model: "EOS Rebel T7", }} cfg.Gear[0].EXIF.Make = "Canon" cfg.Gear[0].EXIF.Model = "Canon EOS Rebel T7" cfg.Development.URL = "http://localhost:8100" cfg.Known.URIs = filepath.Join("src", "uris.txt") cfg.Production.URL = "example.com" s, err := NewSubstructure(cfg) assert.NilError(t, err) gallery := s.galleries["trip"] assert.Assert(t, len(gallery) == 1) im := gallery[0] assert.NilError(t, im.loadMetadataFromSource()) assert.NilError(t, im.loadThumbnailMetadata()) for _, thumb := range im.Thumbnails { dest := filepath.Join(cfg.Dist, thumb.WebPath) assert.NilError(t, os.MkdirAll(filepath.Dir(dest), 0o755)) assert.NilError(t, copyFile(filepath.Join(cwd, "..", "testdata", filepath.Base(thumb.WebPath)), dest)) } hash, err := fileHash(srcImg) assert.NilError(t, err) manifest := newBuildManifest() manifest.record(srcImg, hash, imageGeneratorKey, "image", imageOutputPaths(im)) assert.NilError(t, manifest.save(cfg.Dist)) before, err := os.Stat(distImg) assert.NilError(t, err) assert.NilError(t, s.ExecuteAll(cfg.Dist)) after, err := os.Stat(distImg) assert.NilError(t, err) assert.Assert(t, after.ModTime().Equal(before.ModTime()), "cached build should not rewrite image") assert.Assert(t, len(gallery[0].Thumbnails) > 0) assert.Equal(t, gallery[0].Alt, wantAlt) assert.Equal(t, gallery[0].PurchaseURL, wantPurchaseURL) } func copyFile(src, dest string) error { in, err := os.Open(src) if err != nil { return err } defer in.Close() out, err := os.Create(dest) if err != nil { return err } defer out.Close() _, err = io.Copy(out, in) return err }