summaryrefslogtreecommitdiff
path: root/deno_dir_test.go
blob: fa3d68aedda9cf4316e4e12cf3533fb8f5f9b016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
	}
}