github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/cache/filecache/integration_test.go (about)

     1  // Copyright 2023 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  
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/bep/logg"
    23  	qt "github.com/frankban/quicktest"
    24  	"github.com/gohugoio/hugo/htesting"
    25  	"github.com/gohugoio/hugo/hugolib"
    26  )
    27  
    28  // See issue #10781. That issue wouldn't have been triggered if we kept
    29  // the empty root directories (e.g. _resources/gen/images).
    30  // It's still an upstream Go issue that we also need to handle, but
    31  // this is a test for the first part.
    32  func TestPruneShouldPreserveEmptyCacheRoots(t *testing.T) {
    33  	files := `
    34  -- hugo.toml --
    35  baseURL = "https://example.com"
    36  -- content/_index.md --
    37  ---
    38  title: "Home"
    39  ---
    40  
    41  `
    42  
    43  	b := hugolib.NewIntegrationTestBuilder(
    44  		hugolib.IntegrationTestConfig{T: t, TxtarString: files, RunGC: true, NeedsOsFS: true},
    45  	).Build()
    46  
    47  	_, err := b.H.BaseFs.ResourcesCache.Stat(filepath.Join("_gen", "images"))
    48  
    49  	b.Assert(err, qt.IsNil)
    50  
    51  }
    52  
    53  func TestPruneImages(t *testing.T) {
    54  	if htesting.IsCI() {
    55  		// TODO(bep)
    56  		t.Skip("skip flaky test on CI server")
    57  	}
    58  	files := `
    59  -- hugo.toml --
    60  baseURL = "https://example.com"
    61  [caches]
    62  [caches.images]
    63  maxAge = "200ms"
    64  dir = ":resourceDir/_gen"
    65  -- content/_index.md --
    66  ---
    67  title: "Home"
    68  ---
    69  -- assets/a/pixel.png --
    70  iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
    71  -- layouts/index.html --
    72  {{ warnf "HOME!" }}
    73  {{ $img := resources.GetMatch "**.png" }}
    74  {{ $img = $img.Resize "3x3" }}
    75  {{ $img.RelPermalink }}
    76  
    77  
    78  
    79  `
    80  
    81  	b := hugolib.NewIntegrationTestBuilder(
    82  		hugolib.IntegrationTestConfig{T: t, TxtarString: files, Running: true, RunGC: true, NeedsOsFS: true, LogLevel: logg.LevelInfo},
    83  	).Build()
    84  
    85  	b.Assert(b.GCCount, qt.Equals, 0)
    86  	b.Assert(b.H, qt.IsNotNil)
    87  
    88  	imagesCacheDir := filepath.Join("_gen", "images")
    89  	_, err := b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
    90  
    91  	b.Assert(err, qt.IsNil)
    92  
    93  	// TODO(bep) we need a way to test full rebuilds.
    94  	// For now, just sleep a little so the cache elements expires.
    95  	time.Sleep(300 * time.Millisecond)
    96  
    97  	b.RenameFile("assets/a/pixel.png", "assets/b/pixel2.png").Build()
    98  
    99  	b.Assert(b.GCCount, qt.Equals, 1)
   100  	// Build it again to GC the empty a dir.
   101  	b.Build()
   102  
   103  	_, err = b.H.BaseFs.ResourcesCache.Stat(filepath.Join(imagesCacheDir, "a"))
   104  	b.Assert(err, qt.Not(qt.IsNil))
   105  	_, err = b.H.BaseFs.ResourcesCache.Stat(imagesCacheDir)
   106  	b.Assert(err, qt.IsNil)
   107  
   108  }