diff --git a/omniparser/samples/sampleutil/util_test.go b/omniparser/samples/sampleutil/util_test.go index 2d014c4..ba0456e 100644 --- a/omniparser/samples/sampleutil/util_test.go +++ b/omniparser/samples/sampleutil/util_test.go @@ -4,16 +4,13 @@ import ( "testing" "github.com/bradleyjkemp/cupaloy" - "github.com/stretchr/testify/assert" "github.com/jf-tech/omniparser/jsons" "github.com/jf-tech/omniparser/testlib" ) func createTempFile(t *testing.T, content string) string { - f, err := testlib.CreateTempFileWithContent("", "", content) - assert.NoError(t, err) - return f.Name() + return testlib.CreateTempFileWithContent(t, "", "", content).Name() } func TestSampleTestCommon(t *testing.T) { diff --git a/testlib/tempfile.go b/testlib/tempfile.go index 9450d40..63baeaa 100644 --- a/testlib/tempfile.go +++ b/testlib/tempfile.go @@ -3,33 +3,26 @@ package testlib import ( "io/ioutil" "os" + "testing" + + "github.com/stretchr/testify/assert" "github.com/jf-tech/omniparser/strs" ) // For dir/pattern params check https://golang.org/pkg/io/ioutil/#TempFile // Caller is responsible for calling os.Remove on the returned file. -func CreateTempFileWithContent(dir, pattern, content string) (*os.File, error) { +func CreateTempFileWithContent(t *testing.T, dir, pattern, content string) *os.File { f, err := ioutil.TempFile(dir, pattern) - if err != nil { - return nil, err - } - + assert.NoError(t, err) // If content is empty, no need to write if !strs.IsStrNonBlank(content) { _ = f.Close() - return f, nil - } - - if _, err := f.Write([]byte(content)); err != nil { - _ = os.Remove(f.Name()) - return nil, err - } - - if err := f.Close(); err != nil { - _ = os.Remove(f.Name()) - return nil, err + return f } - - return f, nil + _, err = f.Write([]byte(content)) + assert.NoError(t, err) + err = f.Close() + assert.NoError(t, err) + return f } diff --git a/testlib/tempfile_test.go b/testlib/tempfile_test.go index be6cd40..03f8829 100644 --- a/testlib/tempfile_test.go +++ b/testlib/tempfile_test.go @@ -8,25 +8,16 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCreateTempFileWithContent_BadDir(t *testing.T) { - _, err := CreateTempFileWithContent("some_dir_does_not_exist", t.Name(), "success") - assert.Error(t, err) -} - func TestCreateTempFileWithContent_Success(t *testing.T) { - tmp, err := CreateTempFileWithContent("", t.Name(), "success") - assert.NoError(t, err) - defer func() { assert.NoError(t, os.Remove(tmp.Name())) }() - actual, err := ioutil.ReadFile(tmp.Name()) + tmpEmpty := CreateTempFileWithContent(t, "", t.Name(), "") + defer func() { assert.NoError(t, os.Remove(tmpEmpty.Name())) }() + actual, err := ioutil.ReadFile(tmpEmpty.Name()) assert.NoError(t, err) - assert.Equal(t, "success", string(actual)) -} + assert.Equal(t, "", string(actual)) -func TestCreateTempFileWithContent_EmptyContent(t *testing.T) { - tmp, err := CreateTempFileWithContent("", t.Name(), "") - assert.NoError(t, err) - defer func() { assert.NoError(t, os.Remove(tmp.Name())) }() - actual, err := ioutil.ReadFile(tmp.Name()) + tmpSuccess := CreateTempFileWithContent(t, "", t.Name(), "success") + defer func() { assert.NoError(t, os.Remove(tmpSuccess.Name())) }() + actual, err = ioutil.ReadFile(tmpSuccess.Name()) assert.NoError(t, err) - assert.Equal(t, "", string(actual)) + assert.Equal(t, "success", string(actual)) }