1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 05:58:59 +01:00

Tests enable, disable cache

This commit is contained in:
Oliver Steele 2017-07-21 16:28:42 -04:00
parent 957c56c86e
commit b53e16f472
3 changed files with 24 additions and 6 deletions

25
cache/cache.go vendored
View File

@ -10,13 +10,13 @@ import (
"sync" "sync"
) )
var disableCache = false var enabled = true
var cacheMx sync.Mutex var cacheMx sync.Mutex
func init() { func init() {
s := os.Getenv("GOJEKYLL_DISABLE_CACHE") s := os.Getenv("GOJEKYLL_DISABLE_CACHE")
if s != "" && s != "0" && s != "false" { 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")) 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()) 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. // 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 // If found, it writes the file contents. Else it calls fn to write to
// both the writer and the file system. // 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:])) 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 // 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. // WriteFile truncates the file before writing it, so ignore empty files.
// If the writer actually wrote an empty file, we'll end up gratuitously // If the writer actually wrote an empty file, we'll end up gratuitously
// re-running it, which is okay. // 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 return string(b), err
} }
s, err := fn() s, err := fn()

3
cache/cache_test.go vendored
View File

@ -8,7 +8,8 @@ import (
) )
func TestWithFile(t *testing.T) { func TestWithFile(t *testing.T) {
require.NoError(t, resetCache()) Enable()
require.NoError(t, Clear())
callCount := 0 callCount := 0
stringMaker := func() (string, error) { stringMaker := func() (string, error) {

View File

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"testing" "testing"
"github.com/osteele/gojekyll/cache"
"github.com/osteele/gojekyll/config" "github.com/osteele/gojekyll/config"
"github.com/osteele/liquid" "github.com/osteele/liquid"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -24,6 +25,7 @@ var highlightTagTests = []struct{ in, out string }{
} }
func TestHighlightTag(t *testing.T) { func TestHighlightTag(t *testing.T) {
cache.Disable()
engine := liquid.NewEngine() engine := liquid.NewEngine()
cfg := config.Default() cfg := config.Default()
AddJekyllTags(engine, &cfg, func(string) (string, bool) { return "", false }) AddJekyllTags(engine, &cfg, func(string) (string, bool) { return "", false })