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 <superq@gmail.com>
This commit is contained in:
Ben Kochie
2023-05-03 19:53:48 +02:00
committed by GitHub
parent 604a902e2c
commit 3f6dfbd2a7

View File

@@ -70,10 +70,11 @@ func TestPrint(t *testing.T) {
*/ */
f, err := os.Create("tmp") f, err := os.CreateTemp("", "print_test_tmp")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
defer os.Remove(f.Name())
//Redirect the printed results to a tmp file for later comparison //Redirect the printed results to a tmp file for later comparison
os.Stdout = f os.Stdout = f
@@ -86,17 +87,14 @@ func TestPrint(t *testing.T) {
buf := make([]byte, 256) buf := make([]byte, 256)
f.Seek(0, 0) f.Seek(0, 0)
_, er := f.Read(buf) _, err = f.Read(buf)
if er != nil { if err != nil {
f.Close()
t.Error(err) t.Error(err)
} }
height := strings.Count(string(buf), ". \n") height := strings.Count(string(buf), ". \n")
//Compare the height of the print with the actual height of the tree //Compare the height of the print with the actual height of the tree
if height != 3 { 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.") t.Fatal("The number of rows is inconsistent with the actual number of rows in the tree itself.")
} }
f.Close()
os.Remove("tmp")
} }