Add tests for filesystem driver

This commit is contained in:
ErickSkrauch 2020-04-21 16:17:52 +03:00
parent 6c59ecbe2e
commit 803f3f406b
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 57 additions and 4 deletions

1
Gopkg.lock generated
View File

@ -350,6 +350,7 @@
"github.com/spf13/viper", "github.com/spf13/viper",
"github.com/stretchr/testify/assert", "github.com/stretchr/testify/assert",
"github.com/stretchr/testify/mock", "github.com/stretchr/testify/mock",
"github.com/stretchr/testify/require",
"github.com/stretchr/testify/suite", "github.com/stretchr/testify/suite",
"github.com/tevino/abool", "github.com/tevino/abool",
"github.com/thedevsaddam/govalidator", "github.com/thedevsaddam/govalidator",

View File

@ -17,10 +17,6 @@ type Filesystem struct {
} }
func (f *Filesystem) FindCapeByUsername(username string) (*model.Cape, error) { func (f *Filesystem) FindCapeByUsername(username string) (*model.Cape, error) {
if username == "" {
return nil, nil
}
capePath := path.Join(f.path, strings.ToLower(username)+".png") capePath := path.Join(f.path, strings.ToLower(username)+".png")
file, err := os.Open(capePath) file, err := os.Open(capePath)
if err != nil { if err != nil {

View File

@ -0,0 +1,56 @@
package fs
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
fs, err := New("base/path")
require.Nil(t, err)
require.Equal(t, "base/path", fs.path)
}
func TestFilesystem(t *testing.T) {
t.Run("FindCapeByUsername", func(t *testing.T) {
dir, err := ioutil.TempDir("", "capes")
if err != nil {
panic(fmt.Errorf("cannot crete temp directory for tests: %w", err))
}
defer os.RemoveAll(dir)
t.Run("exists cape", func(t *testing.T) {
file, err := os.Create(path.Join(dir, "username.png"))
if err != nil {
panic(fmt.Errorf("cannot create temp skin for tests: %w", err))
}
defer os.Remove(file.Name())
fs, _ := New(dir)
cape, err := fs.FindCapeByUsername("username")
require.Nil(t, err)
require.NotNil(t, cape)
capeFile, _ := cape.File.(*os.File)
require.Equal(t, file.Name(), capeFile.Name())
})
t.Run("not exists cape", func(t *testing.T) {
fs, _ := New(dir)
cape, err := fs.FindCapeByUsername("username")
require.Nil(t, err)
require.Nil(t, cape)
})
t.Run("empty username", func(t *testing.T) {
fs, _ := New(dir)
cape, err := fs.FindCapeByUsername("")
require.Nil(t, err)
require.Nil(t, cape)
})
})
}