Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper function #236

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,39 @@ func calcChecksumDetails(filePath string) (checksum entities.Checksum, err error
checksum = entities.Checksum{Md5: checksumInfo[MD5], Sha1: checksumInfo[SHA1], Sha256: checksumInfo[SHA256]}
return
}

// TODO ERAN fix comment
// Checks for every file in filesList whether it exists. If so it copies the file to a temporary dir.
// Returns the path to the directory with all the copied files, or an empty path if none of the files existed
// The user is responsible to remove containerDir after finish using it
func KeepFilesInTempDirIfExist(filesList []string) (containerDir string, err error) {
if containerDir, err = CreateTempDir(); err != nil {
return
}
defer func() {
if err != nil {
err = errors.Join(RemoveTempDir(containerDir))
}
}()

anyFileOrDirExist := false
for _, filePath := range filesList {
var exists bool
if exists, err = IsFileExists(filePath, false); err != nil {
return
}

if exists {
anyFileOrDirExist = true
if err = CopyFile(containerDir, filePath); err != nil {
return
}
}
}

if !anyFileOrDirExist {
err = os.Remove(containerDir)
containerDir = ""
}
return
}
56 changes: 54 additions & 2 deletions utils/fileutils_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package utils

import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFindFileInDirAndParents(t *testing.T) {
Expand Down Expand Up @@ -54,3 +54,55 @@ func TestReadNLines(t *testing.T) {
assert.True(t, strings.HasPrefix(lines[1], "781"))
assert.True(t, strings.HasSuffix(lines[1], ":true}}}"))
}

func TestKeepFilesInTempDirIfExist(t *testing.T) {
var testCases = []struct {
amountFilesToCreate int
}{
{
amountFilesToCreate: 0,
},
{
amountFilesToCreate: 3,
},
}

for _, test := range testCases {
t.Run(fmt.Sprintf("with %d files created", test.amountFilesToCreate),
func(t *testing.T) {
tempDir, err := CreateTempDir()
assert.NoError(t, err)
defer func() {
assert.NoError(t, RemoveTempDir(tempDir), "Couldn't remove temp dir")
}()

var filesList []string
for i := 0; i < test.amountFilesToCreate; i++ {
var file *os.File
file, err = os.CreateTemp(tempDir, "tempfile")
assert.NoError(t, err)
filesList = append(filesList, file.Name())
}
containerDir, err := KeepFilesInTempDirIfExist(filesList)
defer func() {
assert.NoError(t, RemoveTempDir(containerDir), "Couldn't remove temp dir")
}()
assert.NoError(t, err)
if test.amountFilesToCreate > 0 {
assert.NotEqual(t, "", containerDir)

for i := 0; i < test.amountFilesToCreate; i++ {
fileName := filepath.Base(filesList[i])
expectedFilePath := filepath.Join(containerDir, fileName)
var exists bool
exists, err = IsFileExists(expectedFilePath, false)
assert.NoError(t, err)
assert.True(t, exists)
}
} else {
assert.Equal(t, "", containerDir)
}
})
}

}
Loading