0ac7e342 by astaxie

Merge pull request #118 from miraclesu/test

Refactor template visit & Add template test
2 parents 5ccdaeb0 2a9852fa
...@@ -51,34 +51,35 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { ...@@ -51,34 +51,35 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
51 if f == nil { 51 if f == nil {
52 return err 52 return err
53 } 53 }
54 if f.IsDir() { 54 if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 {
55 return nil 55 return nil
56 } else if (f.Mode() & os.ModeSymlink) > 0 { 56 }
57 if !HasTemplateEXt(paths) {
57 return nil 58 return nil
59 }
60
61 replace := strings.NewReplacer("\\", "/")
62 a := []byte(paths)
63 a = a[len([]byte(self.root)):]
64 subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/"))
65 if _, ok := self.files[subdir]; ok {
66 self.files[subdir] = append(self.files[subdir], paths)
58 } else { 67 } else {
59 hasExt := false 68 m := make([]string, 1)
60 for _, v := range BeeTemplateExt { 69 m[0] = paths
61 if strings.HasSuffix(paths, v) { 70 self.files[subdir] = m
62 hasExt = true 71 }
63 break
64 }
65 }
66 if hasExt {
67 replace := strings.NewReplacer("\\", "/")
68 a := []byte(paths)
69 a = a[len([]byte(self.root)):]
70 subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/"))
71 if _, ok := self.files[subdir]; ok {
72 self.files[subdir] = append(self.files[subdir], paths)
73 } else {
74 m := make([]string, 1)
75 m[0] = paths
76 self.files[subdir] = m
77 }
78 72
73 return nil
74 }
75
76 func HasTemplateEXt(paths string) bool {
77 for _, v := range BeeTemplateExt {
78 if strings.HasSuffix(paths, "."+v) {
79 return true
79 } 80 }
80 } 81 }
81 return nil 82 return false
82 } 83 }
83 84
84 func AddTemplateExt(ext string) { 85 func AddTemplateExt(ext string) {
......
1 package beego
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7 )
8
9 func TestBuildTemplate(t *testing.T) {
10 dir := "_beeTmp"
11 files := []string{
12 "1.tpl",
13 "2.html",
14 "3.htmltpl",
15 "4.mystyle",
16 }
17 if err := os.MkdirAll(dir, 0777); err != nil {
18 t.Fatal(err)
19 }
20 for _, name := range files {
21 if _, err := os.Create(filepath.Join(dir, name)); err != nil {
22 t.Fatal(err)
23 }
24 }
25 if err := BuildTemplate(dir); err != nil {
26 t.Fatal(err)
27 }
28 if len(BeeTemplates) != 1 {
29 t.Fatalf("should be 1 but got %v", len(BeeTemplates))
30 }
31 for _, v := range BeeTemplates {
32 if len(v.Templates()) != 3 {
33 t.Errorf("should be 3 but got %v", len(v.Templates()))
34 }
35 }
36
37 AddTemplateExt("mystyle")
38 if err := BuildTemplate(dir); err != nil {
39 t.Fatal(err)
40 }
41 if len(BeeTemplates) != 1 {
42 t.Fatalf("should be 1 but got %v", len(BeeTemplates))
43 }
44 for _, v := range BeeTemplates {
45 if len(v.Templates()) != 4 {
46 t.Errorf("should be 4 but got %v", len(v.Templates()))
47 }
48 }
49 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!