-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary
os.Stat
in eval cache (#624)
* Remove unnecessary `os.Stat` in eval cache Any time that stat returns a file stat, we're going to read the file anyways. Might as well jump straight to the read `os.ReadFile` returns an error that matches `os.IsNotExist` just the same as `os.Stat` * Add caching test
- Loading branch information
1 parent
ab2c791
commit 4271d75
Showing
2 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,104 @@ | ||
package jsonnet | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const importTreeResult = `[ | ||
{ | ||
"breed": "apple", | ||
"color": "red", | ||
"creates": "o2", | ||
"eats": "co2", | ||
"keeps": "the world healthy", | ||
"kind": "tree", | ||
"needs": "water", | ||
"size": "m" | ||
}, | ||
{ | ||
"breed": "cherry", | ||
"color": "red", | ||
"creates": "o2", | ||
"eats": "co2", | ||
"keeps": "the world healthy", | ||
"kind": "tree", | ||
"needs": "water", | ||
"size": "xs" | ||
}, | ||
{ | ||
"breed": "peach", | ||
"color": "orange", | ||
"creates": "o2", | ||
"eats": "co2", | ||
"keeps": "the world healthy", | ||
"kind": "tree", | ||
"needs": "water", | ||
"size": "s" | ||
} | ||
] | ||
` | ||
|
||
const thisFileResult = `{ | ||
"test": "testdata/thisFile/main.jsonnet" | ||
} | ||
` | ||
|
||
// To be consistent with the jsonnet executable, | ||
// when evaluating a file, `std.thisFile` should point to the given path | ||
func TestEvaluateFile(t *testing.T) { | ||
result, err := EvaluateFile("testdata/thisFile/main.jsonnet", Opts{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, `{ | ||
"test": "testdata/thisFile/main.jsonnet" | ||
} | ||
`, result) | ||
assert.Equal(t, thisFileResult, result) | ||
} | ||
|
||
func TestEvaluateFileDoesntExist(t *testing.T) { | ||
result, err := EvaluateFile("testdata/doesnt-exist/main.jsonnet", Opts{}) | ||
assert.EqualError(t, err, "open testdata/doesnt-exist/main.jsonnet: no such file or directory") | ||
assert.Equal(t, "", result) | ||
} | ||
|
||
func TestEvaluateFileWithCaching(t *testing.T) { | ||
tmp, err := os.MkdirTemp("", "test-tanka-caching") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(tmp) | ||
cachePath := filepath.Join(tmp, "cache") // Should be created during caching | ||
|
||
// Evaluate two files | ||
result, err := EvaluateFile("testdata/thisFile/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, thisFileResult, result) | ||
result, err = EvaluateFile("testdata/importTree/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, importTreeResult, result) | ||
|
||
// Check that we have two entries in the cache | ||
readCache, err := os.ReadDir(cachePath) | ||
require.NoError(t, err) | ||
assert.Len(t, readCache, 2) | ||
|
||
// Evaluate two files again, same result | ||
result, err = EvaluateFile("testdata/thisFile/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, thisFileResult, result) | ||
result, err = EvaluateFile("testdata/importTree/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, importTreeResult, result) | ||
|
||
// Modify the cache items | ||
for _, entry := range readCache { | ||
require.NoError(t, os.WriteFile(filepath.Join(cachePath, entry.Name()), []byte(entry.Name()), 0666)) | ||
} | ||
|
||
// Evaluate two files again, modified cache is returned instead of the actual result | ||
result, err = EvaluateFile("testdata/thisFile/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "BYfdlr1ZOVwiOfbd89JYTcK-eRQh05bi8ky3k1vVW5o=.json", result) | ||
result, err = EvaluateFile("testdata/importTree/main.jsonnet", Opts{CachePath: cachePath}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "R_3hy-dRfOwXN-fezQ50ZF4dnrFcBcbQ9LztR_XWzJA=.json", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters