refactor(test): new apis for creating temporary files (#3605)

Replace MakeTempFile usage with MakeTempFilePath and MakeTempFileWithContent
helpers that automatically handle file lifecycle. This prevents resource
leaks by ensuring temporary files are properly closed.

Shoudld also make the tests easier to read.

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
This commit is contained in:
Andrei Aaron
2025-12-05 09:54:38 +02:00
committed by GitHub
parent 92aee8ebce
commit 9dfa7c3ae6
46 changed files with 1321 additions and 2612 deletions
+36 -2
View File
@@ -12,6 +12,7 @@ import (
"path"
"path/filepath"
"strings"
"testing"
"time"
"github.com/GehirnInc/crypt"
@@ -308,8 +309,41 @@ func GetSHA512CredString(username, password string) string {
return usernameAndHash
}
func MakeHtpasswdFileFromString(fileContent string) string {
htpasswdFile, err := os.CreateTemp("", "htpasswd-")
func MakeTempFile(tb testing.TB, filename string) *os.File {
tb.Helper()
tempDir := tb.TempDir()
file, err := os.Create(filepath.Join(tempDir, filename))
if err != nil {
panic(err)
}
return file
}
// MakeTempFilePath creates an empty temporary file and returns its path.
func MakeTempFilePath(tb testing.TB, filename string) string {
tb.Helper()
return filepath.Join(tb.TempDir(), filename)
}
// MakeTempFileWithContent creates a temporary file with the given filename and content, and returns its path.
func MakeTempFileWithContent(tb testing.TB, filename, content string) string {
tb.Helper()
tmpfile := MakeTempFile(tb, filename)
path := tmpfile.Name()
tmpfile.Close() // Close immediately, we'll write using os.WriteFile
if err := os.WriteFile(path, []byte(content), 0o0600); err != nil {
panic(err)
}
return path
}
func MakeHtpasswdFileFromString(tb testing.TB, fileContent string) string {
tb.Helper()
tempDir := tb.TempDir()
htpasswdFile, err := os.Create(filepath.Join(tempDir, "htpasswd"))
if err != nil {
panic(err)
}
+21 -21
View File
@@ -20,7 +20,7 @@ var ErrTestError = errors.New("ErrTestError")
func TestCopyFiles(t *testing.T) {
Convey("sourceDir does not exist", t, func() {
err := tcommon.CopyFiles("/path/to/some/unexisting/directory", os.TempDir())
err := tcommon.CopyFiles("/path/to/some/unexisting/directory", t.TempDir())
So(err, ShouldNotBeNil)
})
Convey("destDir is a file", t, func() {
@@ -38,7 +38,7 @@ func TestCopyFiles(t *testing.T) {
err := os.Chmod(dir, 0o300)
So(err, ShouldBeNil)
err = tcommon.CopyFiles(dir, os.TempDir())
err = tcommon.CopyFiles(dir, t.TempDir())
So(err, ShouldNotBeNil)
})
Convey("sourceDir has a subfolder that does not have read permissions", t, func() {
@@ -48,7 +48,7 @@ func TestCopyFiles(t *testing.T) {
err := os.Mkdir(path.Join(dir, sdir), 0o300)
So(err, ShouldBeNil)
err = tcommon.CopyFiles(dir, os.TempDir())
err = tcommon.CopyFiles(dir, t.TempDir())
So(err, ShouldNotBeNil)
})
Convey("sourceDir has a file that does not have read permissions", t, func() {
@@ -64,7 +64,7 @@ func TestCopyFiles(t *testing.T) {
err = os.Chmod(filePath, 0o300)
So(err, ShouldBeNil)
err = tcommon.CopyFiles(dir, os.TempDir())
err = tcommon.CopyFiles(dir, t.TempDir())
So(err, ShouldNotBeNil)
})
Convey("sourceDir contains a folder starting with invalid characters", t, func() {
@@ -125,21 +125,22 @@ func TestCopyFile(t *testing.T) {
}
func TestReadLogFileAndSearchString(t *testing.T) {
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
if err != nil {
panic(err)
}
logPath := logFile.Name()
defer os.Remove(logPath)
logPath := tcommon.MakeTempFilePath(t, "zot-log.txt")
Convey("Invalid path", t, func() {
_, err = tcommon.ReadLogFileAndSearchString("invalidPath",
_, err := tcommon.ReadLogFileAndSearchString("invalidPath",
"cve-db update completed, next update scheduled after interval", 1*time.Second)
So(err, ShouldNotBeNil)
})
Convey("Time too short", t, func() {
// Create an empty file so ReadLogFileAndSearchString can read it
file, err := os.Create(logPath)
if err != nil {
panic(err)
}
file.Close()
ok, err := tcommon.ReadLogFileAndSearchString(logPath, "invalid string", time.Microsecond)
So(err, ShouldBeNil)
So(ok, ShouldBeFalse)
@@ -147,18 +148,15 @@ func TestReadLogFileAndSearchString(t *testing.T) {
}
func TestReadLogFileAndCountStringOccurence(t *testing.T) {
logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
if err != nil {
panic(err)
}
logFile := tcommon.MakeTempFile(t, "zot-log.txt")
defer logFile.Close()
_, err = logFile.WriteString("line1\n line2\n line3 line1 line2\n line1")
_, err := logFile.WriteString("line1\n line2\n line3 line1 line2\n line1")
if err != nil {
panic(err)
}
logPath := logFile.Name()
defer os.Remove(logPath)
Convey("Invalid path", t, func() {
_, err = tcommon.ReadLogFileAndCountStringOccurence("invalidPath",
@@ -239,9 +237,10 @@ func TestCopyTestKeysAndCerts(t *testing.T) {
So(err.Error(), ShouldContainSubstring, "CopyFiles os.Stat failed")
// --- GetProjectRootDir call fails -----
err = os.Chdir(os.TempDir())
tempDir := t.TempDir()
err = os.Chdir(tempDir)
So(err, ShouldBeNil)
err = tcommon.CopyTestKeysAndCerts(os.TempDir())
err = tcommon.CopyTestKeysAndCerts(tempDir)
So(err, ShouldNotBeNil)
So(err, ShouldEqual, tcommon.ErrNoGoModFileFound)
})
@@ -259,7 +258,8 @@ func TestGetProjectRootDir(t *testing.T) {
defer func() { _ = os.Chdir(workDir) }()
err = os.Chdir(os.TempDir())
tempDir := t.TempDir()
err = os.Chdir(tempDir)
So(err, ShouldBeNil)
path, err := tcommon.GetProjectRootDir()
So(err, ShouldNotBeNil)
+2 -4
View File
@@ -180,8 +180,7 @@ func TestUploadImage(t *testing.T) {
password1 := "test"
testString1 := tcommon.GetBcryptCredString(user1, password1)
htpasswdPath := tcommon.MakeHtpasswdFileFromString(testString1)
defer os.Remove(htpasswdPath)
htpasswdPath := tcommon.MakeHtpasswdFileFromString(t, testString1)
conf.HTTP.Auth = &config.AuthConfig{
HTPasswd: config.AuthHTPasswd{
Path: htpasswdPath,
@@ -497,8 +496,7 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
password := "password"
testString := tcommon.GetBcryptCredString(user, password)
htpasswdPath := tcommon.MakeHtpasswdFileFromString(testString)
defer os.Remove(htpasswdPath)
htpasswdPath := tcommon.MakeHtpasswdFileFromString(t, testString)
conf.HTTP.Auth = &config.AuthConfig{
HTPasswd: config.AuthHTPasswd{
Path: htpasswdPath,