summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-22 12:43:20 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-22 13:12:05 -0400
commit9ea397861feeefcce9f18947e10aa6cc155ec459 (patch)
treec41647a0528d1872135dab9147f8eb0c96d77447
parentcd9c361ee1b09b42e731d79ce55b95a3e9bc4fbc (diff)
Fix LoadOutputCodeCache
-rw-r--r--deno_dir.go10
-rw-r--r--deno_dir_test.go46
2 files changed, 52 insertions, 4 deletions
diff --git a/deno_dir.go b/deno_dir.go
index 185f7abe0..d53bf85d8 100644
--- a/deno_dir.go
+++ b/deno_dir.go
@@ -61,15 +61,17 @@ func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
return ioutil.ReadAll(sourceReader)
}
-func LoadOutputCodeCache(filename string, sourceCodeBuf []byte) (outputCode string, err error) {
+func LoadOutputCodeCache(filename string, sourceCodeBuf []byte) (
+ outputCode string, err error) {
cacheFn := CacheFileName(filename, sourceCodeBuf)
outputCodeBuf, err := ioutil.ReadFile(cacheFn)
if os.IsNotExist(err) {
- err = nil // Ignore error if we can't load the cache.
- } else if err != nil {
+ // Ignore error if we can't find the cache file.
+ err = nil
+ } else if err == nil {
outputCode = string(outputCodeBuf)
}
- return
+ return outputCode, err
}
func UserHomeDir() string {
diff --git a/deno_dir_test.go b/deno_dir_test.go
new file mode 100644
index 000000000..fa3d68aed
--- /dev/null
+++ b/deno_dir_test.go
@@ -0,0 +1,46 @@
+package main
+
+import (
+ "io/ioutil"
+ "testing"
+)
+
+func SetCompileDirForTest(prefix string) {
+ dir, err := ioutil.TempDir("", prefix)
+ if err != nil {
+ panic(err)
+ }
+ CompileDir = dir
+}
+
+func TestLoadOutputCodeCache(t *testing.T) {
+ SetCompileDirForTest("TestLoadOutputCodeCache")
+
+ filename := "Hello.ts"
+ sourceCodeBuf := []byte("1+2")
+
+ cacheFn := CacheFileName(filename, sourceCodeBuf)
+
+ outputCode, err := LoadOutputCodeCache(filename, sourceCodeBuf)
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
+ if outputCode != "" {
+ t.Fatalf("Expected empty outputCode but got <<%s>>", outputCode)
+ }
+
+ // Now let's write to the cache file
+ err = ioutil.WriteFile(cacheFn, []byte("blah"), 0700)
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
+
+ // Try it again.
+ outputCode, err = LoadOutputCodeCache(filename, sourceCodeBuf)
+ if err != nil {
+ t.Fatalf(err.Error())
+ }
+ if outputCode != "blah" {
+ t.Fatalf("Bad outputCode but got <<%s>>", outputCode)
+ }
+}