@@ -19,6 +19,7 @@ package jsre
1919import (
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+
4357func 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
6680func 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
90104func 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
107121func 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