mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-26 19:34:52 +01:00
Tests enable, disable cache
This commit is contained in:
parent
957c56c86e
commit
b53e16f472
25
cache/cache.go
vendored
25
cache/cache.go
vendored
@ -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()
|
||||
|
3
cache/cache_test.go
vendored
3
cache/cache_test.go
vendored
@ -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) {
|
||||
|
@ -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 })
|
||||
|
Loading…
Reference in New Issue
Block a user