From b53e16f472f150bc76fa721705cd9347423652b8 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Fri, 21 Jul 2017 16:28:42 -0400 Subject: [PATCH] Tests enable, disable cache --- cache/cache.go | 25 ++++++++++++++++++++----- cache/cache_test.go | 3 ++- tags/highlight_test.go | 2 ++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 74182fd..6aa5236 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -10,13 +10,13 @@ import ( "sync" ) -var disableCache = false +var enabled = true var cacheMx sync.Mutex func init() { s := os.Getenv("GOJEKYLL_DISABLE_CACHE") if s != "" && s != "0" && s != "false" { - disableCache = true + enabled = false } } @@ -24,10 +24,21 @@ func cacheDir() string { return filepath.Join(os.TempDir(), os.ExpandEnv("gojekyll-$USER")) } -func resetCache() error { +// Clear clears the cache. It's used for testing. +func Clear() error { return os.RemoveAll(cacheDir()) } +// Enable enables the cache; for testing. +func Enable() { + enabled = true +} + +// Disable disables the cache; for testing. +func Disable() { + enabled = false +} + // WithFile looks (header, content) up in a user-specific file cache. // If found, it writes the file contents. Else it calls fn to write to // both the writer and the file system. @@ -46,11 +57,15 @@ func WithFile(header string, content string, fn func() (string, error)) (string, cachefile := filepath.Join(cachedir, fmt.Sprintf("%x%c%x", sum[:1], filepath.Separator, sum[1:])) // ignore errors; if there's a missing file we don't care, and if it's - // another error we'll pick it up during write + // another error we'll pick it up during write. + // // WriteFile truncates the file before writing it, so ignore empty files. // If the writer actually wrote an empty file, we'll end up gratuitously // re-running it, which is okay. - if b, err := ioutil.ReadFile(cachefile); err == nil && len(b) > 0 && !disableCache { + // + // Do as much work as possible before checking if the cache is enabled, to + // minimize code paths and timing differences. + if b, err := ioutil.ReadFile(cachefile); err == nil && len(b) > 0 && enabled { return string(b), err } s, err := fn() diff --git a/cache/cache_test.go b/cache/cache_test.go index 8e9a107..2a1138a 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -8,7 +8,8 @@ import ( ) func TestWithFile(t *testing.T) { - require.NoError(t, resetCache()) + Enable() + require.NoError(t, Clear()) callCount := 0 stringMaker := func() (string, error) { diff --git a/tags/highlight_test.go b/tags/highlight_test.go index 68b0051..268c895 100644 --- a/tags/highlight_test.go +++ b/tags/highlight_test.go @@ -5,6 +5,7 @@ import ( "regexp" "testing" + "github.com/osteele/gojekyll/cache" "github.com/osteele/gojekyll/config" "github.com/osteele/liquid" "github.com/stretchr/testify/require" @@ -24,6 +25,7 @@ var highlightTagTests = []struct{ in, out string }{ } func TestHighlightTag(t *testing.T) { + cache.Disable() engine := liquid.NewEngine() cfg := config.Default() AddJekyllTags(engine, &cfg, func(string) (string, bool) { return "", false })