Debian Codegraph @

cache/filecache/filecache_integration_test.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package filecache_test
15
16 import (
17 "path/filepath"
18 "testing"
19 "time"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/htesting"
23 "github.com/gohugoio/hugo/hugolib"
24 )
25
26 // See issue #10781. That issue wouldn't have been triggered if we kept
27 // the empty root directories (e.g. _resources/gen/images).
28 // It's still an upstream Go issue that we also need to handle, but
29 // this is a test for the first part.
30 func TestPruneShouldPreserveEmptyCacheRoots(t *testing.T) {
31 files := `
32 -- hugo.toml --
33 baseURL = "https://example.com"
34 -- content/_index.md --
35 ---
36 title: "Home"
37 ---
38
39 `
40
41 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
42 c.RunGC = true
43 }))
44
45 _, err := b.H.BaseFs.ResourcesCache.Stat(filepath.Join("_gen", "images"))
46
47 b.Assert(err, qt.IsNil)
48 }
49
50 func TestPruneImages(t *testing.T) {
51 if htesting.IsCI() {
52 // TODO(bep)
53 t.Skip("skip flaky test on CI server")
54 }
55 t.Skip("skip flaky test")
56 files := `
57 -- hugo.toml --
58 baseURL = "https://example.com"
59 [caches]
60 [caches.images]
61 maxAge = "200ms"
62 dir = ":resourceDir/_gen"
63 -- content/_index.md --
64 ---
65 title: "Home"
66 ---
67 -- assets/a/pixel.png --
68 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
69 -- layouts/home.html --
70 {{ warnf "HOME!" }}
71 {{ $img := resources.GetMatch "**.png" }}
72 {{ $img = $img.Resize "3x3" }}
73 {{ $img.RelPermalink }}
74
75
76
77 `
78
79 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs(), hugolib.TestOptInfo(), hugolib.TestOptWithConfig(func(c *hugolib.IntegrationTestConfig) {
80 c.RunGC = true
81 }))
82
83 b.Assert(b.GCCount, qt.Equals, 0)
84 b.Assert(b.H, qt.IsNotNil)
85
86 imagesCacheDir := filepath.Join("_gen", "images")
87 _, err := b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
88
89 b.Assert(err, qt.IsNil)
90
91 // TODO(bep) we need a way to test full rebuilds.
92 // For now, just sleep a little so the cache elements expires.
93 time.Sleep(500 * time.Millisecond)
94
95 b.RenameFile("assets/a/pixel.png", "assets/b/pixel2.png").Build()
96
97 b.Assert(b.GCCount, qt.Equals, 1)
98 // Build it again to GC the empty a dir.
99 b.Build()
100
101 _, err = b.H.BaseFs.ResourcesCache.Stat(filepath.Join(imagesCacheDir, "a"))
102 b.Assert(err, qt.Not(qt.IsNil))
103 _, err = b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
104 b.Assert(err, qt.IsNil)
105 }