moth/cmd/mothd/theme_test.go

45 lines
931 B
Go
Raw Normal View History

2019-12-05 22:25:03 -07:00
package main
import (
2020-02-29 18:36:59 -07:00
"io/ioutil"
2020-02-29 22:37:22 -07:00
"testing"
2020-08-14 20:26:04 -06:00
"github.com/spf13/afero"
2019-12-05 22:25:03 -07:00
)
2020-08-17 17:43:57 -06:00
func NewTestTheme() *Theme {
return NewTheme(new(afero.MemMapFs))
}
2019-12-05 22:25:03 -07:00
func TestTheme(t *testing.T) {
2020-08-17 17:43:57 -06:00
s := NewTestTheme()
2020-08-14 20:26:04 -06:00
filename := "/index.html"
2020-02-29 18:36:59 -07:00
index := "this is the index"
2020-08-17 17:43:57 -06:00
afero.WriteFile(s.Fs, filename, []byte(index), 0644)
fileInfo, err := s.Fs.Stat(filename)
2020-08-14 20:26:04 -06:00
if err != nil {
t.Error(err)
}
2019-12-05 22:25:03 -07:00
2020-08-14 20:26:04 -06:00
if f, timestamp, err := s.Open("/index.html"); err != nil {
2020-02-29 18:36:59 -07:00
t.Error(err)
} else if buf, err := ioutil.ReadAll(f); err != nil {
t.Error(err)
} else if string(buf) != index {
t.Error("Read wrong value from index")
2020-08-14 20:26:04 -06:00
} else if !timestamp.Equal(fileInfo.ModTime()) {
t.Error("Timestamp compared wrong")
2019-12-05 22:25:03 -07:00
}
2020-08-21 17:02:38 -06:00
2021-10-13 16:43:51 -06:00
if f, _, err := s.Open("/foo/bar/index.html"); err == nil {
f.Close()
t.Error("Path is ignored")
}
2020-08-21 17:02:38 -06:00
if f, _, err := s.Open("nofile"); err == nil {
f.Close()
t.Error("Opening non-existent file didn't return an error")
}
2019-12-05 22:25:03 -07:00
}