From 8f52e57d9cfe3b41ec9ff63ac89474b86f9d6ba9 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Wed, 17 Jul 2019 14:33:01 +0200 Subject: [PATCH] Add tests for file util --- internal/util/file_test.go | 45 ++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/internal/util/file_test.go b/internal/util/file_test.go index 1cdc75f55..c909ef1bc 100644 --- a/internal/util/file_test.go +++ b/internal/util/file_test.go @@ -1,6 +1,7 @@ package util import ( + "os" "testing" "github.com/stretchr/testify/assert" @@ -11,10 +12,46 @@ func TestExists(t *testing.T) { assert.False(t, Exists("./foo.jpg")) } -func TestExpandedFilename(t *testing.T) { - filename := ExpandedFilename("./testdata/test.jpg") +func TestOverwrite(t *testing.T) { + data := make([]byte, 3) + data[1] = 3 + data[2] = 8 + tmpPath := "./testdata/_tmp" + os.Mkdir(tmpPath, 0777) - assert.IsType(t, "", filename) + defer os.RemoveAll(tmpPath) + result := Overwrite("./testdata/_tmp/notyetexisting.jpg", data) + assert.FileExists(t, "./testdata/_tmp/notyetexisting.jpg") + t.Log(result) - t.Logf("ExpandedFilename: %s", filename) +} + +func TestExpandedFilename(t *testing.T) { + t.Run("test.jpg", func(t *testing.T) { + filename := ExpandedFilename("./testdata/test.jpg") + assert.Contains(t, filename, "/testdata/test.jpg") + assert.IsType(t, "", filename) + }) + t.Run("empty filename", func(t *testing.T) { + filename := ExpandedFilename("") + assert.Equal(t, "", filename) + assert.IsType(t, "", filename) + }) + t.Run("~ in filename", func(t *testing.T) { + filename := ExpandedFilename("~/test.jpg") + assert.Equal(t, "/root/test.jpg", filename) + assert.IsType(t, "", filename) + }) +} + +func TestDirectoryIsEmpty(t *testing.T) { + t.Run("not empty path", func(t *testing.T) { + assert.Equal(t, false, DirectoryIsEmpty("./testdata")) + }) + t.Run("not existing path", func(t *testing.T) { + assert.Equal(t, false, DirectoryIsEmpty("./xxx")) + }) + t.Run("photo-lover", func(t *testing.T) { + assert.Equal(t, true, DirectoryIsEmpty("./testdata/emptyDir")) + }) }