all: use T.TempDir to create temporary test directories (#24633)
This commit replaces ioutil.TempDir with t.TempDir in tests. The directory created by t.TempDir is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using ioutil.TempDir had to be removed manually by calling os.RemoveAll, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but t.TempDir handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
@ -40,23 +40,19 @@ func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value
|
||||
return no.vm.ToValue(&msg{m})
|
||||
}
|
||||
|
||||
func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
|
||||
dir, err := ioutil.TempDir("", "jsre-test")
|
||||
if err != nil {
|
||||
t.Fatal("cannot create temporary directory:", err)
|
||||
}
|
||||
func newWithTestJS(t *testing.T, testjs string) *JSRE {
|
||||
dir := t.TempDir()
|
||||
if testjs != "" {
|
||||
if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
|
||||
t.Fatal("cannot create test.js:", err)
|
||||
}
|
||||
}
|
||||
jsre := New(dir, os.Stdout)
|
||||
return jsre, dir
|
||||
return jsre
|
||||
}
|
||||
|
||||
func TestExec(t *testing.T) {
|
||||
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
||||
defer os.RemoveAll(dir)
|
||||
jsre := newWithTestJS(t, `msg = "testMsg"`)
|
||||
|
||||
err := jsre.Exec("test.js")
|
||||
if err != nil {
|
||||
@ -78,8 +74,7 @@ func TestExec(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNatto(t *testing.T) {
|
||||
jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
|
||||
defer os.RemoveAll(dir)
|
||||
jsre := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
|
||||
|
||||
err := jsre.Exec("test.js")
|
||||
if err != nil {
|
||||
@ -114,8 +109,7 @@ func TestBind(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoadScript(t *testing.T) {
|
||||
jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
|
||||
defer os.RemoveAll(dir)
|
||||
jsre := newWithTestJS(t, `msg = "testMsg"`)
|
||||
|
||||
_, err := jsre.Run(`loadScript("test.js")`)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user