markdown_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package static
  2. import (
  3. "html/template"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. )
  10. var parseTemplate *template.Template
  11. var readfileError, templateError, createError, mkdirallError, parseError, walkError error
  12. func init() {
  13. readfile = func(_ string) ([]byte, error) { return nil, readfileError }
  14. create = func(_ string) (*os.File, error) { return nil, createError }
  15. mkdirall = func(_ string, _ os.FileMode) error { return mkdirallError }
  16. parseFiles = func(...string) (*template.Template, error) { return parseTemplate, parseError }
  17. walk = func(_ string, _ filepath.WalkFunc) error { return walkError }
  18. }
  19. type mockLogger struct{}
  20. func (self *mockLogger) Error(_ string, _ ...interface{}) {}
  21. func (self *mockLogger) Debug(_ string, _ ...interface{}) {}
  22. func (self *mockLogger) Info(_ string, _ ...interface{}) {}
  23. type mockTemplate struct{}
  24. func (self *mockTemplate) Execute(_ io.Writer, _ interface{}) error { return templateError }
  25. type mockFileInfo struct {
  26. N string
  27. S int64
  28. Fm uint32
  29. T time.Time
  30. D bool
  31. So interface{}
  32. }
  33. func (self *mockFileInfo) Name() string { return self.N }
  34. func (self *mockFileInfo) Size() int64 { return self.S }
  35. func (self *mockFileInfo) Mode() os.FileMode { return os.FileMode(self.Fm) }
  36. func (self *mockFileInfo) ModTime() time.Time { return self.T }
  37. func (self *mockFileInfo) IsDir() bool { return self.D }
  38. func (self *mockFileInfo) Sys() interface{} { return self.So }
  39. func TestIor(t *testing.T) {
  40. t.Parallel()
  41. g := Markdown{}
  42. if s := g.ior("some.md"); len(s) == 0 {
  43. t.FailNow()
  44. }
  45. }
  46. func TestDepth(t *testing.T) {
  47. t.Parallel()
  48. absp := "/abs/path/"
  49. g := Markdown{Output: absp}
  50. // test abs depth
  51. if d := g.depth("somefile"); len(d) > 0 {
  52. t.FailNow()
  53. }
  54. // test relative depth
  55. g.Relative = true
  56. if d := g.depth(absp + "somefile"); len(d) == 0 {
  57. t.Logf("Path: %s\n", d)
  58. t.FailNow()
  59. }
  60. }
  61. func TestWalk(t *testing.T) {
  62. t.Parallel()
  63. g := Markdown{}
  64. p := "valid.md"
  65. var f os.FileInfo = &mockFileInfo{S: 1}
  66. var e error
  67. // test with valid file
  68. if err := g.walk(p, f, e); err != nil {
  69. t.FailNow()
  70. }
  71. }
  72. func TestMulti(t *testing.T) {
  73. g := Markdown{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
  74. // set expected defaults
  75. notExist = false
  76. statError = nil
  77. // no pages
  78. if e := g.multi(); e != nil {
  79. t.FailNow()
  80. }
  81. // test full pass
  82. if e := g.multi(); e != nil {
  83. t.FailNow()
  84. }
  85. // test full pass relative
  86. g.Relative = true
  87. if e := g.multi(); e != nil {
  88. t.FailNow()
  89. }
  90. // test failing execute
  91. templateError = mockError
  92. if e := g.multi(); e == nil {
  93. t.FailNow()
  94. }
  95. // test failing file creation
  96. createError = mockError
  97. if e := g.multi(); e == nil {
  98. t.FailNow()
  99. }
  100. // test failing to read the file
  101. readfileError = mockError
  102. if e := g.multi(); e == nil {
  103. t.FailNow()
  104. }
  105. // test dir creation failure
  106. mkdirallError = mockError
  107. statError = mockError
  108. if e := g.multi(); e == nil {
  109. t.FailNow()
  110. }
  111. }
  112. func TestSingle(t *testing.T) {
  113. g := Markdown{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
  114. // reset expected defaults
  115. statError = nil
  116. readfileError = nil
  117. createError = nil
  118. templateError = nil
  119. // test full pass
  120. if e := g.single(); e != nil {
  121. t.FailNow()
  122. }
  123. // test failing execute
  124. templateError = mockError
  125. if e := g.single(); e == nil {
  126. t.FailNow()
  127. }
  128. // test create error
  129. createError = mockError
  130. if e := g.single(); e == nil {
  131. t.FailNow()
  132. }
  133. // test fail mkdirall
  134. mkdirallError = mockError
  135. statError = mockError
  136. if e := g.single(); e == nil {
  137. t.FailNow()
  138. }
  139. // test fail readfile
  140. readfileError = mockError
  141. if e := g.single(); e == nil {
  142. t.FailNow()
  143. }
  144. }
  145. func TestGenerate(t *testing.T) {
  146. g := Markdown{L: &mockLogger{}}
  147. // set template for stand-alone execution
  148. parseTemplate = template.New("test")
  149. // test full pass
  150. if e := g.Generate(); e != nil {
  151. t.FailNow()
  152. }
  153. // test book mode full pass
  154. g.Book = true
  155. if e := g.Generate(); e == nil {
  156. t.FailNow()
  157. }
  158. // test walk error
  159. walkError = mockError
  160. if e := g.Generate(); e == nil {
  161. t.FailNow()
  162. }
  163. // test template error
  164. parseError = mockError
  165. if e := g.Generate(); e == nil {
  166. t.FailNow()
  167. }
  168. }