1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:20:55 +00:00

Merge pull request #118 from miraclesu/test

Refactor template visit & Add template test
This commit is contained in:
astaxie 2013-07-31 22:29:04 -07:00
commit 0ac7e342f0
2 changed files with 73 additions and 23 deletions

View File

@ -51,34 +51,35 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 {
return nil
} else if (f.Mode() & os.ModeSymlink) > 0 {
}
if !HasTemplateEXt(paths) {
return nil
} else {
hasExt := false
for _, v := range BeeTemplateExt {
if strings.HasSuffix(paths, v) {
hasExt = true
break
}
}
if hasExt {
replace := strings.NewReplacer("\\", "/")
a := []byte(paths)
a = a[len([]byte(self.root)):]
subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/"))
if _, ok := self.files[subdir]; ok {
self.files[subdir] = append(self.files[subdir], paths)
} else {
m := make([]string, 1)
m[0] = paths
self.files[subdir] = m
}
}
replace := strings.NewReplacer("\\", "/")
a := []byte(paths)
a = a[len([]byte(self.root)):]
subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/"))
if _, ok := self.files[subdir]; ok {
self.files[subdir] = append(self.files[subdir], paths)
} else {
m := make([]string, 1)
m[0] = paths
self.files[subdir] = m
}
return nil
}
func HasTemplateEXt(paths string) bool {
for _, v := range BeeTemplateExt {
if strings.HasSuffix(paths, "."+v) {
return true
}
}
return nil
return false
}
func AddTemplateExt(ext string) {

49
template_test.go Normal file
View File

@ -0,0 +1,49 @@
package beego
import (
"os"
"path/filepath"
"testing"
)
func TestBuildTemplate(t *testing.T) {
dir := "_beeTmp"
files := []string{
"1.tpl",
"2.html",
"3.htmltpl",
"4.mystyle",
}
if err := os.MkdirAll(dir, 0777); err != nil {
t.Fatal(err)
}
for _, name := range files {
if _, err := os.Create(filepath.Join(dir, name)); err != nil {
t.Fatal(err)
}
}
if err := BuildTemplate(dir); err != nil {
t.Fatal(err)
}
if len(BeeTemplates) != 1 {
t.Fatalf("should be 1 but got %v", len(BeeTemplates))
}
for _, v := range BeeTemplates {
if len(v.Templates()) != 3 {
t.Errorf("should be 3 but got %v", len(v.Templates()))
}
}
AddTemplateExt("mystyle")
if err := BuildTemplate(dir); err != nil {
t.Fatal(err)
}
if len(BeeTemplates) != 1 {
t.Fatalf("should be 1 but got %v", len(BeeTemplates))
}
for _, v := range BeeTemplates {
if len(v.Templates()) != 4 {
t.Errorf("should be 4 but got %v", len(v.Templates()))
}
}
}