generator_test.go 4.5 KB

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