minor cleanups for 'os' module tests

This commit is contained in:
Daniel Kang 2019-02-07 15:18:49 -08:00
parent e81380bb38
commit a9f9ba4592

View file

@ -5,25 +5,23 @@ import (
"os" "os"
"testing" "testing"
"github.com/d5/tengo/assert"
"github.com/d5/tengo/objects" "github.com/d5/tengo/objects"
) )
func TestReadFile(t *testing.T) { func TestReadFile(t *testing.T) {
content := []byte("the quick brown fox jumps over the lazy dog") content := []byte("the quick brown fox jumps over the lazy dog")
tf, err := ioutil.TempFile("", "test") tf, err := ioutil.TempFile("", "test")
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not open tempfile: %s", err)
return return
} }
defer os.Remove(tf.Name()) defer func() { _ = os.Remove(tf.Name()) }()
_, err = tf.Write(content) _, err = tf.Write(content)
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not write temp content: %s", err)
return return
} }
_ = tf.Close()
tf.Close()
module(t, "os").call("read_file", tf.Name()).expect(&objects.Bytes{Value: content}) module(t, "os").call("read_file", tf.Name()).expect(&objects.Bytes{Value: content})
} }
@ -38,19 +36,16 @@ func TestFileStatArgs(t *testing.T) {
func TestFileStatFile(t *testing.T) { func TestFileStatFile(t *testing.T) {
content := []byte("the quick brown fox jumps over the lazy dog") content := []byte("the quick brown fox jumps over the lazy dog")
tf, err := ioutil.TempFile("", "test") tf, err := ioutil.TempFile("", "test")
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not open tempfile: %s", err)
return return
} }
defer os.Remove(tf.Name()) defer func() { _ = os.Remove(tf.Name()) }()
_, err = tf.Write(content) _, err = tf.Write(content)
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not write temp content: %s", err)
return return
} }
_ = tf.Close()
tf.Close()
stat, err := os.Stat(tf.Name()) stat, err := os.Stat(tf.Name())
if err != nil { if err != nil {
@ -71,15 +66,13 @@ func TestFileStatFile(t *testing.T) {
func TestFileStatDir(t *testing.T) { func TestFileStatDir(t *testing.T) {
td, err := ioutil.TempDir("", "test") td, err := ioutil.TempDir("", "test")
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not open tempdir: %s", err)
return return
} }
defer os.RemoveAll(td) defer func() { _ = os.RemoveAll(td) }()
stat, err := os.Stat(td) stat, err := os.Stat(td)
if err != nil { if !assert.NoError(t, err) {
t.Logf("could not get tmp dir stat: %s", err)
return return
} }