2020-01-12 14:00:56 +01:00
|
|
|
package fs
|
2019-05-03 18:57:28 +02:00
|
|
|
|
|
|
|
import (
|
2020-12-26 18:06:54 +01:00
|
|
|
"fmt"
|
2019-07-17 14:33:01 +02:00
|
|
|
"os"
|
2019-12-27 05:18:52 +01:00
|
|
|
"os/user"
|
2019-05-03 18:57:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-12-26 18:06:54 +01:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if insensitive, err := CaseInsensitive(os.TempDir()); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else if insensitive {
|
|
|
|
IgnoreCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
func TestFileExists(t *testing.T) {
|
2020-01-12 14:00:56 +01:00
|
|
|
assert.True(t, FileExists("./testdata/test.jpg"))
|
2022-04-18 17:21:31 +02:00
|
|
|
assert.True(t, FileExists("./testdata/test.jpg"))
|
|
|
|
assert.True(t, FileExists("./testdata/empty.jpg"))
|
2020-01-12 14:00:56 +01:00
|
|
|
assert.False(t, FileExists("./foo.jpg"))
|
2020-07-07 15:13:42 +02:00
|
|
|
assert.False(t, FileExists(""))
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:21:31 +02:00
|
|
|
func TestFileExistsNotEmpty(t *testing.T) {
|
|
|
|
assert.True(t, FileExistsNotEmpty("./testdata/test.jpg"))
|
|
|
|
assert.True(t, FileExistsNotEmpty("./testdata/test.jpg"))
|
|
|
|
assert.False(t, FileExistsNotEmpty("./testdata/empty.jpg"))
|
|
|
|
assert.False(t, FileExistsNotEmpty("./foo.jpg"))
|
|
|
|
assert.False(t, FileExistsNotEmpty(""))
|
|
|
|
}
|
|
|
|
|
2020-01-31 15:29:06 +01:00
|
|
|
func TestPathExists(t *testing.T) {
|
|
|
|
assert.True(t, PathExists("./testdata"))
|
|
|
|
assert.False(t, PathExists("./testdata/test.jpg"))
|
|
|
|
assert.False(t, PathExists("./testdata3ggdtgdg"))
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:37:31 +01:00
|
|
|
func TestPathWritable(t *testing.T) {
|
|
|
|
assert.True(t, PathExists("./testdata"))
|
|
|
|
assert.False(t, PathExists("./testdata/test.jpg"))
|
|
|
|
assert.False(t, PathExists("./testdata3ggdtgdg"))
|
|
|
|
}
|
|
|
|
|
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")
|
2020-01-31 15:29:06 +01:00
|
|
|
assert.True(t, result)
|
2019-07-17 14:33:01 +02:00
|
|
|
}
|
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) {
|
2020-01-31 15:29:06 +01:00
|
|
|
filename := Abs("./testdata/test.jpg")
|
2019-07-17 14:33:01 +02:00
|
|
|
assert.Contains(t, filename, "/testdata/test.jpg")
|
|
|
|
assert.IsType(t, "", filename)
|
|
|
|
})
|
|
|
|
t.Run("empty filename", func(t *testing.T) {
|
2020-01-31 15:29:06 +01:00
|
|
|
filename := Abs("")
|
2019-07-17 14:33:01 +02:00
|
|
|
assert.Equal(t, "", filename)
|
|
|
|
assert.IsType(t, "", filename)
|
|
|
|
})
|
|
|
|
t.Run("~ in filename", func(t *testing.T) {
|
2019-12-27 05:18:52 +01:00
|
|
|
usr, _ := user.Current()
|
|
|
|
expected := usr.HomeDir + "/test.jpg"
|
2020-01-31 15:29:06 +01:00
|
|
|
filename := Abs("~/test.jpg")
|
2019-12-27 05:18:52 +01:00
|
|
|
assert.Equal(t, expected, filename)
|
2019-07-17 14:33:01 +02:00
|
|
|
assert.IsType(t, "", filename)
|
|
|
|
})
|
|
|
|
}
|
2019-05-03 18:57:28 +02:00
|
|
|
|
2022-08-01 17:08:46 +02:00
|
|
|
func TestDirIsEmpty(t *testing.T) {
|
|
|
|
t.Run("CurrentDir", func(t *testing.T) {
|
|
|
|
assert.Equal(t, false, DirIsEmpty("."))
|
2019-07-17 14:33:01 +02:00
|
|
|
})
|
2022-08-01 17:08:46 +02:00
|
|
|
t.Run("Testdata", func(t *testing.T) {
|
|
|
|
assert.Equal(t, false, DirIsEmpty("./testdata"))
|
2019-07-17 14:33:01 +02:00
|
|
|
})
|
2022-08-01 17:08:46 +02:00
|
|
|
t.Run("XXX", func(t *testing.T) {
|
|
|
|
assert.Equal(t, false, DirIsEmpty("./xxx"))
|
|
|
|
})
|
|
|
|
t.Run("EmptyDir", func(t *testing.T) {
|
|
|
|
if err := os.Mkdir("./testdata/emptyDir", 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-17 14:59:29 +02:00
|
|
|
defer os.RemoveAll("./testdata/emptyDir")
|
2022-08-01 17:08:46 +02:00
|
|
|
assert.Equal(t, true, DirIsEmpty("./testdata/emptyDir"))
|
2019-07-17 14:33:01 +02:00
|
|
|
})
|
2019-05-03 18:57:28 +02:00
|
|
|
}
|