Simplify tests with T.TempDir (#929)

This commit is contained in:
Oleksandr Redko
2025-01-31 20:55:41 +02:00
committed by GitHub
parent 2a0ff5ac63
commit 7a2915a37d
8 changed files with 24 additions and 70 deletions

View File

@@ -1,7 +1,6 @@
package openai //nolint:testpackage // testing private field
import (
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
"bytes"
@@ -20,15 +19,11 @@ func (*failingWriter) Write([]byte) (int, error) {
}
func TestFormBuilderWithFailingWriter(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
file, err := os.CreateTemp(dir, "")
file, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
t.Fatalf("Error creating tmp file: %v", err)
}
defer file.Close()
defer os.Remove(file.Name())
builder := NewFormBuilder(&failingWriter{})
err = builder.CreateFormFile("file", file)
@@ -36,15 +31,11 @@ func TestFormBuilderWithFailingWriter(t *testing.T) {
}
func TestFormBuilderWithClosedFile(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
file, err := os.CreateTemp(dir, "")
file, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
t.Fatalf("Error creating tmp file: %v", err)
}
file.Close()
defer os.Remove(file.Name())
body := &bytes.Buffer{}
builder := NewFormBuilder(body)

View File

@@ -19,16 +19,6 @@ func CreateTestFile(t *testing.T, path string) {
file.Close()
}
// CreateTestDirectory creates a temporary folder which will be deleted when cleanup is called.
func CreateTestDirectory(t *testing.T) (path string, cleanup func()) {
t.Helper()
path, err := os.MkdirTemp(os.TempDir(), "")
checks.NoError(t, err)
return path, func() { os.RemoveAll(path) }
}
// TokenRoundTripper is a struct that implements the RoundTripper
// interface, specifically to handle the authentication token by adding a token
// to the request header. We need this because the API requires that each