From 3f6dfbd2a7b21bf52b599d6287e4cc9ead01975a Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Wed, 3 May 2023 19:53:48 +0200 Subject: Fix temp file close error (#6068) Avoid Go 1.20 test error by not attempting to close the testing temp file unless there was an error in Read(). * Use a CreateTemp() to create unique test files. * Defer the deletion of the temp file. Woarkaround for: https://github.com/golang/go/issues/59938 Signed-off-by: SuperQ --- plugin/file/tree/print_test.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'plugin/file/tree/print_test.go') diff --git a/plugin/file/tree/print_test.go b/plugin/file/tree/print_test.go index 2ab552769..20ad37d9a 100644 --- a/plugin/file/tree/print_test.go +++ b/plugin/file/tree/print_test.go @@ -70,10 +70,11 @@ func TestPrint(t *testing.T) { */ - f, err := os.Create("tmp") + f, err := os.CreateTemp("", "print_test_tmp") if err != nil { t.Error(err) } + defer os.Remove(f.Name()) //Redirect the printed results to a tmp file for later comparison os.Stdout = f @@ -86,17 +87,14 @@ func TestPrint(t *testing.T) { buf := make([]byte, 256) f.Seek(0, 0) - _, er := f.Read(buf) - if er != nil { + _, err = f.Read(buf) + if err != nil { + f.Close() t.Error(err) } height := strings.Count(string(buf), ". \n") //Compare the height of the print with the actual height of the tree if height != 3 { - f.Close() - os.Remove("tmp") t.Fatal("The number of rows is inconsistent with the actual number of rows in the tree itself.") } - f.Close() - os.Remove("tmp") } -- cgit v1.2.3