1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Fix LoadOutputCodeCache

This commit is contained in:
Ryan Dahl 2018-05-22 12:43:20 -04:00
parent cd9c361ee1
commit 9ea397861f
2 changed files with 52 additions and 4 deletions

View file

@ -61,15 +61,17 @@ func FetchRemoteSource(remoteUrl string, localFilename string) ([]byte, error) {
return ioutil.ReadAll(sourceReader) 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) cacheFn := CacheFileName(filename, sourceCodeBuf)
outputCodeBuf, err := ioutil.ReadFile(cacheFn) outputCodeBuf, err := ioutil.ReadFile(cacheFn)
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = nil // Ignore error if we can't load the cache. // Ignore error if we can't find the cache file.
} else if err != nil { err = nil
} else if err == nil {
outputCode = string(outputCodeBuf) outputCode = string(outputCodeBuf)
} }
return return outputCode, err
} }
func UserHomeDir() string { func UserHomeDir() string {

46
deno_dir_test.go Normal file
View file

@ -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)
}
}