2019-05-06 23:18:10 +02:00
|
|
|
package util
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-17 14:33:01 +02:00
|
|
|
"os"
|
2019-05-03 18:57:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
2019-05-04 05:25:00 +02:00
|
|
|
assert.True(t, Exists("./testdata/test.jpg"))
|
2019-05-03 18:57:28 +02:00
|
|
|
assert.False(t, Exists("./foo.jpg"))
|
|
|
|
}
|
|
|
|
|
2019-07-17 14:33:01 +02:00
|
|
|
func TestOverwrite(t *testing.T) {
|
|
|
|
data := make([]byte, 3)
|
|
|
|
data[1] = 3
|
|
|
|
data[2] = 8
|
|
|
|
tmpPath := "./testdata/_tmp"
|
|
|
|
os.Mkdir(tmpPath, 0777)
|
|
|
|
|
|
|
|
defer os.RemoveAll(tmpPath)
|
|
|
|
result := Overwrite("./testdata/_tmp/notyetexisting.jpg", data)
|
|
|
|
assert.FileExists(t, "./testdata/_tmp/notyetexisting.jpg")
|
|
|
|
t.Log(result)
|
|
|
|
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-07-17 14:33:01 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2019-07-17 14:33:01 +02:00
|
|
|
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"))
|
|
|
|
})
|
2019-07-17 14:59:29 +02:00
|
|
|
t.Run("empty path", func(t *testing.T) {
|
|
|
|
os.Mkdir("./testdata/emptyDir", 0777)
|
|
|
|
defer os.RemoveAll("./testdata/emptyDir")
|
2019-07-17 14:33:01 +02:00
|
|
|
assert.Equal(t, true, DirectoryIsEmpty("./testdata/emptyDir"))
|
|
|
|
})
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|