Skip to content

Commit

Permalink
added helper func and test
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Feb 25, 2024
1 parent 154fcd1 commit c0213bb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
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)
}
})
}

}

0 comments on commit c0213bb

Please sign in to comment.