aboutsummaryrefslogtreecommitdiff
path: root/plugin/file/tree
diff options
context:
space:
mode:
authorGravatar Ben Kochie <superq@gmail.com> 2023-05-03 19:53:48 +0200
committerGravatar GitHub <noreply@github.com> 2023-05-03 13:53:48 -0400
commit3f6dfbd2a7b21bf52b599d6287e4cc9ead01975a (patch)
tree7bfcb0c68c8aa1f8daa07792af547feef06a26cc /plugin/file/tree
parent604a902e2c7e0317aecaa3666124079c75a31573 (diff)
downloadcoredns-3f6dfbd2a7b21bf52b599d6287e4cc9ead01975a.tar.gz
coredns-3f6dfbd2a7b21bf52b599d6287e4cc9ead01975a.tar.zst
coredns-3f6dfbd2a7b21bf52b599d6287e4cc9ead01975a.zip
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>
Diffstat (limited to 'plugin/file/tree')
-rw-r--r--plugin/file/tree/print_test.go12
1 files changed, 5 insertions, 7 deletions
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")
}