burndown_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package hercules
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. "io"
  8. )
  9. func TestBurndownMeta(t *testing.T) {
  10. burndown := BurndownAnalysis{}
  11. assert.Equal(t, burndown.Name(), "Burndown")
  12. assert.Equal(t, len(burndown.Provides()), 0)
  13. required := [...]string{"file_diff", "renamed_changes", "blob_cache", "day", "author"}
  14. for _, name := range required {
  15. assert.Contains(t, burndown.Requires(), name)
  16. }
  17. }
  18. func TestBurndownConsumeFinalize(t *testing.T) {
  19. burndown := BurndownAnalysis{
  20. Granularity: 30,
  21. Sampling: 30,
  22. PeopleNumber: 2,
  23. TrackFiles: true,
  24. }
  25. burndown.Initialize(testRepository)
  26. deps := map[string]interface{}{}
  27. // stage 1
  28. deps["author"] = 0
  29. deps["day"] = 0
  30. cache := map[plumbing.Hash]*object.Blob{}
  31. hash := plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  32. cache[hash], _ = testRepository.BlobObject(hash)
  33. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  34. cache[hash], _ = testRepository.BlobObject(hash)
  35. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  36. cache[hash], _ = testRepository.BlobObject(hash)
  37. hash = plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9")
  38. cache[hash], _ = testRepository.BlobObject(hash)
  39. deps["blob_cache"] = cache
  40. changes := make(object.Changes, 3)
  41. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  42. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  43. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  44. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  45. changes[0] = &object.Change{From: object.ChangeEntry{
  46. Name: "analyser.go",
  47. Tree: treeFrom,
  48. TreeEntry: object.TreeEntry{
  49. Name: "analyser.go",
  50. Mode: 0100644,
  51. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  52. },
  53. }, To: object.ChangeEntry{
  54. Name: "analyser.go",
  55. Tree: treeTo,
  56. TreeEntry: object.TreeEntry{
  57. Name: "analyser.go",
  58. Mode: 0100644,
  59. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  60. },
  61. }}
  62. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  63. Name: "cmd/hercules/main.go",
  64. Tree: treeTo,
  65. TreeEntry: object.TreeEntry{
  66. Name: "cmd/hercules/main.go",
  67. Mode: 0100644,
  68. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  69. },
  70. },
  71. }
  72. changes[2] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  73. Name: ".travis.yml",
  74. Tree: treeTo,
  75. TreeEntry: object.TreeEntry{
  76. Name: ".travis.yml",
  77. Mode: 0100644,
  78. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  79. },
  80. },
  81. }
  82. deps["renamed_changes"] = changes
  83. fd := fixtureFileDiff()
  84. result, err := fd.Consume(deps)
  85. assert.Nil(t, err)
  86. deps["file_diff"] = result["file_diff"]
  87. result, err = burndown.Consume(deps)
  88. assert.Nil(t, result)
  89. assert.Nil(t, err)
  90. assert.Equal(t, burndown.previousDay, 0)
  91. assert.Equal(t, len(burndown.files), 3)
  92. assert.Equal(t, burndown.files["cmd/hercules/main.go"].Len(), 207)
  93. assert.Equal(t, burndown.files["analyser.go"].Len(), 926)
  94. assert.Equal(t, burndown.files[".travis.yml"].Len(), 12)
  95. assert.Equal(t, len(burndown.people), 2)
  96. assert.Equal(t, burndown.people[0][0], int64(12+207+926))
  97. assert.Equal(t, len(burndown.globalStatus), 1)
  98. assert.Equal(t, burndown.globalStatus[0], int64(12+207+926))
  99. assert.Equal(t, len(burndown.globalHistory), 0)
  100. assert.Equal(t, len(burndown.fileHistories), 0)
  101. burndown2 := BurndownAnalysis{
  102. Granularity: 30,
  103. Sampling: 0,
  104. }
  105. burndown2.Initialize(testRepository)
  106. _, err = burndown2.Consume(deps)
  107. assert.Nil(t, err)
  108. assert.Equal(t, len(burndown2.people), 0)
  109. assert.Equal(t, len(burndown2.peopleHistories), 0)
  110. assert.Equal(t, len(burndown2.fileHistories), 0)
  111. // stage 2
  112. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  113. deps["author"] = 1
  114. deps["day"] = 30
  115. cache = map[plumbing.Hash]*object.Blob{}
  116. hash = plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  117. cache[hash], _ = testRepository.BlobObject(hash)
  118. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  119. cache[hash], _ = testRepository.BlobObject(hash)
  120. hash = plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2")
  121. cache[hash], _ = testRepository.BlobObject(hash)
  122. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  123. cache[hash], _ = testRepository.BlobObject(hash)
  124. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  125. cache[hash], _ = testRepository.BlobObject(hash)
  126. deps["blob_cache"] = cache
  127. changes = make(object.Changes, 3)
  128. treeFrom, _ = testRepository.TreeObject(plumbing.NewHash(
  129. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  130. treeTo, _ = testRepository.TreeObject(plumbing.NewHash(
  131. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  132. changes[0] = &object.Change{From: object.ChangeEntry{
  133. Name: "analyser.go",
  134. Tree: treeFrom,
  135. TreeEntry: object.TreeEntry{
  136. Name: "analyser.go",
  137. Mode: 0100644,
  138. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  139. },
  140. }, To: object.ChangeEntry{
  141. Name: "burndown.go",
  142. Tree: treeTo,
  143. TreeEntry: object.TreeEntry{
  144. Name: "burndown.go",
  145. Mode: 0100644,
  146. Hash: plumbing.NewHash("29c9fafd6a2fae8cd20298c3f60115bc31a4c0f2"),
  147. },
  148. },
  149. }
  150. changes[1] = &object.Change{From: object.ChangeEntry{
  151. Name: "cmd/hercules/main.go",
  152. Tree: treeFrom,
  153. TreeEntry: object.TreeEntry{
  154. Name: "cmd/hercules/main.go",
  155. Mode: 0100644,
  156. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  157. },
  158. }, To: object.ChangeEntry{
  159. Name: "cmd/hercules/main.go",
  160. Tree: treeTo,
  161. TreeEntry: object.TreeEntry{
  162. Name: "cmd/hercules/main.go",
  163. Mode: 0100644,
  164. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  165. },
  166. },
  167. }
  168. changes[2] = &object.Change{From: object.ChangeEntry{
  169. Name: ".travis.yml",
  170. Tree: treeTo,
  171. TreeEntry: object.TreeEntry{
  172. Name: ".travis.yml",
  173. Mode: 0100644,
  174. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  175. },
  176. }, To: object.ChangeEntry{},
  177. }
  178. deps["renamed_changes"] = changes
  179. fd = fixtureFileDiff()
  180. result, err = fd.Consume(deps)
  181. assert.Nil(t, err)
  182. deps["file_diff"] = result["file_diff"]
  183. result, err = burndown.Consume(deps)
  184. assert.Nil(t, result)
  185. assert.Nil(t, err)
  186. assert.Equal(t, burndown.previousDay, 30)
  187. assert.Equal(t, len(burndown.files), 2)
  188. assert.Equal(t, burndown.files["cmd/hercules/main.go"].Len(), 290)
  189. assert.Equal(t, burndown.files["burndown.go"].Len(), 543)
  190. assert.Equal(t, len(burndown.people), 2)
  191. assert.Equal(t, len(burndown.globalStatus), 2)
  192. assert.Equal(t, burndown.globalStatus[0], int64(464))
  193. assert.Equal(t, burndown.globalStatus[1], int64(0))
  194. assert.Equal(t, len(burndown.globalHistory), 1)
  195. assert.Equal(t, len(burndown.globalHistory[0]), 2)
  196. assert.Equal(t, len(burndown.fileHistories), 3)
  197. out := burndown.Finalize().(BurndownResult)
  198. /*
  199. GlobalHistory [][]int64
  200. FileHistories map[string][][]int64
  201. PeopleHistories [][][]int64
  202. PeopleMatrix [][]int64
  203. */
  204. assert.Equal(t, len(out.GlobalHistory), 2)
  205. for i := 0; i < 2; i++ {
  206. assert.Equal(t, len(out.GlobalHistory[i]), 2)
  207. }
  208. assert.Equal(t, len(out.GlobalHistory), 2)
  209. assert.Equal(t, out.GlobalHistory[0][0], int64(1145))
  210. assert.Equal(t, out.GlobalHistory[0][1], int64(0))
  211. assert.Equal(t, out.GlobalHistory[1][0], int64(464))
  212. assert.Equal(t, out.GlobalHistory[1][1], int64(369))
  213. assert.Equal(t, len(out.FileHistories), 2)
  214. assert.Equal(t, len(out.FileHistories["cmd/hercules/main.go"]), 2)
  215. assert.Equal(t, len(out.FileHistories["burndown.go"]), 2)
  216. assert.Equal(t, len(out.FileHistories["cmd/hercules/main.go"][0]), 2)
  217. assert.Equal(t, len(out.FileHistories["burndown.go"][0]), 2)
  218. assert.Equal(t, len(out.PeopleMatrix), 2)
  219. assert.Equal(t, len(out.PeopleMatrix[0]), 4)
  220. assert.Equal(t, len(out.PeopleMatrix[1]), 4)
  221. assert.Equal(t, out.PeopleMatrix[0][0], int64(1145))
  222. assert.Equal(t, out.PeopleMatrix[0][1], int64(0))
  223. assert.Equal(t, out.PeopleMatrix[0][2], int64(0))
  224. assert.Equal(t, out.PeopleMatrix[0][3], int64(-681))
  225. assert.Equal(t, out.PeopleMatrix[1][0], int64(369))
  226. assert.Equal(t, out.PeopleMatrix[1][1], int64(0))
  227. assert.Equal(t, out.PeopleMatrix[1][2], int64(0))
  228. assert.Equal(t, out.PeopleMatrix[1][3], int64(0))
  229. assert.Equal(t, len(out.PeopleHistories), 2)
  230. for i := 0; i < 2; i++ {
  231. assert.Equal(t, len(out.PeopleHistories[i]), 2)
  232. assert.Equal(t, len(out.PeopleHistories[i][0]), 2)
  233. assert.Equal(t, len(out.PeopleHistories[i][1]), 2)
  234. }
  235. }
  236. type panickingCloser struct {
  237. }
  238. func (c panickingCloser) Close() error {
  239. return io.EOF
  240. }
  241. func TestCheckClose(t *testing.T) {
  242. closer := panickingCloser{}
  243. assert.Panics(t, func() { checkClose(closer) })
  244. }