Skip to content

Commit 3832019

Browse files
committed
common/compiler, common/docserver, jsre: fix tests on windows
1 parent eae1191 commit 3832019

4 files changed

Lines changed: 38 additions & 17 deletions

File tree

‎common/compiler/solidity_test.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"io/ioutil"
2222
"os"
23+
"path"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/common"
@@ -94,7 +95,7 @@ func TestSaveInfo(t *testing.T) {
9495
if err != nil {
9596
t.Errorf("%v", err)
9697
}
97-
filename := "/tmp/solctest.info.json"
98+
filename := path.Join(os.TempDir(), "solctest.info.json")
9899
os.Remove(filename)
99100
cinfohash, err := SaveInfo(&cinfo, filename)
100101
if err != nil {
@@ -110,4 +111,4 @@ func TestSaveInfo(t *testing.T) {
110111
if cinfohash != infohash {
111112
t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
112113
}
113-
}
114+
}

‎common/docserver/docserver.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func New(docRoot string) (self *DocServer) {
3838
DocRoot: docRoot,
3939
schemes: []string{"file"},
4040
}
41-
self.DocRoot = "/tmp/"
4241
self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
4342
return
4443
}

‎common/docserver/docserver_test.go‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ import (
2020
"io/ioutil"
2121
"net/http"
2222
"os"
23+
"path"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/common"
2627
"github.com/ethereum/go-ethereum/crypto"
2728
)
2829

2930
func TestGetAuthContent(t *testing.T) {
30-
text := "test"
31-
hash := common.Hash{}
32-
copy(hash[:], crypto.Sha3([]byte(text)))
33-
ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm)
31+
dir, err := ioutil.TempDir("", "docserver-test")
32+
if err != nil {
33+
t.Fatal("cannot create temporary directory:", err)
34+
}
35+
defer os.RemoveAll(dir)
36+
ds := New(dir)
3437

35-
ds := New("/tmp/")
38+
text := "test"
39+
hash := crypto.Sha3Hash([]byte(text))
40+
if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
41+
t.Fatal("could not write test file", err)
42+
}
3643
content, err := ds.GetAuthContent("file:///test.content", hash)
3744
if err != nil {
3845
t.Errorf("no error expected, got %v", err)
@@ -67,4 +74,4 @@ func TestRegisterScheme(t *testing.T) {
6774
if !ds.HasScheme("scheme") {
6875
t.Errorf("expected scheme to be registered")
6976
}
70-
}
77+
}

‎jsre/jsre_test.go‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package jsre
1919
import (
2020
"io/ioutil"
2121
"os"
22+
"path"
2223
"testing"
2324
"time"
2425

@@ -40,10 +41,23 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value
4041
return v
4142
}
4243

44+
func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
45+
dir, err := ioutil.TempDir("", "jsre-test")
46+
if err != nil {
47+
t.Fatal("cannot create temporary directory:", err)
48+
}
49+
if testjs != "" {
50+
if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
51+
t.Fatal("cannot create test.js:", err)
52+
}
53+
}
54+
return New(dir), dir
55+
}
56+
4357
func TestExec(t *testing.T) {
44-
jsre := New("/tmp")
58+
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
59+
defer os.RemoveAll(dir)
4560

46-
ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
4761
err := jsre.Exec("test.js")
4862
if err != nil {
4963
t.Errorf("expected no error, got %v", err)
@@ -64,9 +78,9 @@ func TestExec(t *testing.T) {
6478
}
6579

6680
func TestNatto(t *testing.T) {
67-
jsre := New("/tmp")
81+
jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
82+
defer os.RemoveAll(dir)
6883

69-
ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm)
7084
err := jsre.Exec("test.js")
7185
if err != nil {
7286
t.Errorf("expected no error, got %v", err)
@@ -88,7 +102,7 @@ func TestNatto(t *testing.T) {
88102
}
89103

90104
func TestBind(t *testing.T) {
91-
jsre := New("/tmp")
105+
jsre := New("")
92106

93107
jsre.Bind("no", &testNativeObjectBinding{})
94108

@@ -105,9 +119,9 @@ func TestBind(t *testing.T) {
105119
}
106120

107121
func TestLoadScript(t *testing.T) {
108-
jsre := New("/tmp")
122+
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
123+
defer os.RemoveAll(dir)
109124

110-
ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
111125
_, err := jsre.Run(`loadScript("test.js")`)
112126
if err != nil {
113127
t.Errorf("expected no error, got %v", err)
@@ -125,4 +139,4 @@ func TestLoadScript(t *testing.T) {
125139
t.Errorf("expected '%v', got '%v'", exp, got)
126140
}
127141
jsre.Stop(false)
128-
}
142+
}

0 commit comments

Comments
 (0)