uast_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package hercules
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gopkg.in/bblfsh/sdk.v1/uast"
  6. "gopkg.in/src-d/go-git.v4/plumbing"
  7. "gopkg.in/src-d/go-git.v4/plumbing/object"
  8. )
  9. func fixtureUASTExtractor() *UASTExtractor {
  10. exr := UASTExtractor{Endpoint: "0.0.0.0:9432"}
  11. exr.Initialize(testRepository)
  12. exr.Languages["Python"] = true
  13. return &exr
  14. }
  15. func TestUASTExtractorMeta(t *testing.T) {
  16. exr := fixtureUASTExtractor()
  17. assert.Equal(t, exr.Name(), "UAST")
  18. assert.Equal(t, len(exr.Provides()), 1)
  19. assert.Equal(t, exr.Provides()[0], "uasts")
  20. assert.Equal(t, len(exr.Requires()), 2)
  21. assert.Equal(t, exr.Requires()[0], "changes")
  22. assert.Equal(t, exr.Requires()[1], "blob_cache")
  23. opts := exr.ListConfigurationOptions()
  24. assert.Len(t, opts, 5)
  25. assert.Equal(t, opts[0].Name, ConfigUASTEndpoint)
  26. assert.Equal(t, opts[1].Name, ConfigUASTTimeout)
  27. assert.Equal(t, opts[2].Name, ConfigUASTPoolSize)
  28. assert.Equal(t, opts[3].Name, ConfigUASTFailOnErrors)
  29. assert.Equal(t, opts[4].Name, ConfigUASTLanguages)
  30. feats := exr.Features()
  31. assert.Len(t, feats, 1)
  32. assert.Equal(t, feats[0], "uast")
  33. }
  34. func TestUASTExtractorConfiguration(t *testing.T) {
  35. exr := fixtureUASTExtractor()
  36. facts := map[string]interface{}{}
  37. exr.Configure(facts)
  38. facts[ConfigUASTEndpoint] = "localhost:9432"
  39. facts[ConfigUASTTimeout] = 15
  40. facts[ConfigUASTPoolSize] = 7
  41. facts[ConfigUASTLanguages] = "C, Go"
  42. facts[ConfigUASTFailOnErrors] = true
  43. exr.Configure(facts)
  44. assert.Equal(t, exr.Endpoint, facts[ConfigUASTEndpoint])
  45. assert.NotNil(t, exr.Context)
  46. assert.Equal(t, exr.PoolSize, facts[ConfigUASTPoolSize])
  47. assert.True(t, exr.Languages["C"])
  48. assert.True(t, exr.Languages["Go"])
  49. assert.False(t, exr.Languages["Python"])
  50. assert.Equal(t, exr.FailOnErrors, true)
  51. }
  52. func TestUASTExtractorRegistration(t *testing.T) {
  53. tp, exists := Registry.registered[(&UASTExtractor{}).Name()]
  54. assert.True(t, exists)
  55. assert.Equal(t, tp.Elem().Name(), "UASTExtractor")
  56. tps, exists := Registry.provided[(&UASTExtractor{}).Provides()[0]]
  57. assert.True(t, exists)
  58. assert.Len(t, tps, 1)
  59. assert.Equal(t, tps[0].Elem().Name(), "UASTExtractor")
  60. }
  61. func TestUASTExtractorConsume(t *testing.T) {
  62. exr := fixtureUASTExtractor()
  63. changes := make(object.Changes, 2)
  64. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  65. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  66. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  67. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  68. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  69. changes[0] = &object.Change{From: object.ChangeEntry{
  70. Name: "analyser.go",
  71. Tree: treeFrom,
  72. TreeEntry: object.TreeEntry{
  73. Name: "analyser.go",
  74. Mode: 0100644,
  75. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  76. },
  77. }, To: object.ChangeEntry{},
  78. }
  79. changes[1] = &object.Change{From: object.ChangeEntry{
  80. Name: "cmd/hercules/main.go",
  81. Tree: treeFrom,
  82. TreeEntry: object.TreeEntry{
  83. Name: "cmd/hercules/main.go",
  84. Mode: 0100644,
  85. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  86. },
  87. }, To: object.ChangeEntry{
  88. Name: "cmd/hercules/main.go",
  89. Tree: treeTo,
  90. TreeEntry: object.TreeEntry{
  91. Name: "cmd/hercules/main.go",
  92. Mode: 0100644,
  93. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  94. },
  95. },
  96. }
  97. cache := map[plumbing.Hash]*object.Blob{}
  98. hash := plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  99. cache[hash], _ = testRepository.BlobObject(hash)
  100. hash = plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619")
  101. cache[hash], _ = testRepository.BlobObject(hash)
  102. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  103. cache[hash], _ = testRepository.BlobObject(hash)
  104. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  105. cache[hash], _ = testRepository.BlobObject(hash)
  106. deps := map[string]interface{}{}
  107. deps["blob_cache"] = cache
  108. deps["changes"] = changes
  109. res, err := exr.Consume(deps)
  110. // Language not enabled
  111. assert.Len(t, res["uasts"], 0)
  112. assert.Nil(t, err)
  113. exr.Languages["Go"] = true
  114. res, err = exr.Consume(deps)
  115. // No Go driver
  116. assert.Len(t, res["uasts"], 0)
  117. assert.Nil(t, err)
  118. hash = plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619")
  119. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  120. Name: "labours.py",
  121. Tree: treeTo,
  122. TreeEntry: object.TreeEntry{
  123. Name: "labours.py",
  124. Mode: 0100644,
  125. Hash: hash,
  126. },
  127. },
  128. }
  129. res, err = exr.Consume(deps)
  130. assert.Nil(t, err)
  131. uasts := res["uasts"].(map[plumbing.Hash]*uast.Node)
  132. assert.Equal(t, len(uasts), 1)
  133. assert.Equal(t, len(uasts[hash].Children), 24)
  134. }
  135. func fixtureUASTChanges() *UASTChanges {
  136. ch := UASTChanges{}
  137. ch.Configure(nil)
  138. ch.Initialize(testRepository)
  139. return &ch
  140. }
  141. func TestUASTChangesMeta(t *testing.T) {
  142. ch := fixtureUASTChanges()
  143. assert.Equal(t, ch.Name(), "UASTChanges")
  144. assert.Equal(t, len(ch.Provides()), 1)
  145. assert.Equal(t, ch.Provides()[0], "changed_uasts")
  146. assert.Equal(t, len(ch.Requires()), 2)
  147. assert.Equal(t, ch.Requires()[0], "uasts")
  148. assert.Equal(t, ch.Requires()[1], "changes")
  149. opts := ch.ListConfigurationOptions()
  150. assert.Len(t, opts, 0)
  151. feats := ch.Features()
  152. assert.Len(t, feats, 1)
  153. assert.Equal(t, feats[0], "uast")
  154. }
  155. func TestUASTChangesRegistration(t *testing.T) {
  156. tp, exists := Registry.registered[(&UASTChanges{}).Name()]
  157. assert.True(t, exists)
  158. assert.Equal(t, tp.Elem().Name(), "UASTChanges")
  159. tps, exists := Registry.provided[(&UASTChanges{}).Provides()[0]]
  160. assert.True(t, exists)
  161. assert.True(t, len(tps) >= 1)
  162. matched := false
  163. for _, tp := range tps {
  164. matched = matched || tp.Elem().Name() == "UASTChanges"
  165. }
  166. assert.True(t, matched)
  167. }
  168. func TestUASTChangesConsume(t *testing.T) {
  169. uastsArray := []*uast.Node{}
  170. uasts := map[plumbing.Hash]*uast.Node{}
  171. hash := plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  172. uasts[hash] = &uast.Node{}
  173. uasts[hash].InternalType = "uno"
  174. uastsArray = append(uastsArray, uasts[hash])
  175. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  176. uasts[hash] = &uast.Node{}
  177. uasts[hash].InternalType = "dos"
  178. uastsArray = append(uastsArray, uasts[hash])
  179. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  180. uasts[hash] = &uast.Node{}
  181. uasts[hash].InternalType = "tres"
  182. uastsArray = append(uastsArray, uasts[hash])
  183. hash = plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9")
  184. uasts[hash] = &uast.Node{}
  185. uasts[hash].InternalType = "quatro"
  186. uastsArray = append(uastsArray, uasts[hash])
  187. changes := make(object.Changes, 3)
  188. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  189. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  190. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  191. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  192. changes[0] = &object.Change{From: object.ChangeEntry{
  193. Name: "analyser.go",
  194. Tree: treeFrom,
  195. TreeEntry: object.TreeEntry{
  196. Name: "analyser.go",
  197. Mode: 0100644,
  198. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  199. },
  200. }, To: object.ChangeEntry{
  201. Name: "analyser.go",
  202. Tree: treeTo,
  203. TreeEntry: object.TreeEntry{
  204. Name: "analyser.go",
  205. Mode: 0100644,
  206. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  207. },
  208. }}
  209. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  210. Name: "cmd/hercules/main.go",
  211. Tree: treeTo,
  212. TreeEntry: object.TreeEntry{
  213. Name: "cmd/hercules/main.go",
  214. Mode: 0100644,
  215. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  216. },
  217. },
  218. }
  219. changes[2] = &object.Change{To: object.ChangeEntry{}, From: object.ChangeEntry{
  220. Name: ".travis.yml",
  221. Tree: treeTo,
  222. TreeEntry: object.TreeEntry{
  223. Name: ".travis.yml",
  224. Mode: 0100644,
  225. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  226. },
  227. },
  228. }
  229. deps := map[string]interface{}{}
  230. deps["uasts"] = uasts
  231. deps["changes"] = changes
  232. ch := fixtureUASTChanges()
  233. ch.cache[changes[0].From.TreeEntry.Hash] = uastsArray[3]
  234. ch.cache[changes[2].From.TreeEntry.Hash] = uastsArray[0]
  235. resultMap, err := ch.Consume(deps)
  236. assert.Nil(t, err)
  237. result := resultMap["changed_uasts"].([]UASTChange)
  238. assert.Len(t, result, 3)
  239. assert.Equal(t, result[0].Change, changes[0])
  240. assert.Equal(t, result[0].Before, uastsArray[3])
  241. assert.Equal(t, result[0].After, uastsArray[2])
  242. assert.Equal(t, result[1].Change, changes[1])
  243. assert.Nil(t, result[1].Before)
  244. assert.Equal(t, result[1].After, uastsArray[1])
  245. assert.Equal(t, result[2].Change, changes[2])
  246. assert.Equal(t, result[2].Before, uastsArray[0])
  247. assert.Nil(t, result[2].After)
  248. }
  249. func fixtureUASTChangesSaver() *UASTChangesSaver {
  250. ch := UASTChangesSaver{}
  251. ch.Initialize(testRepository)
  252. return &ch
  253. }
  254. func TestUASTChangesSaverMeta(t *testing.T) {
  255. ch := fixtureUASTChangesSaver()
  256. assert.Equal(t, ch.Name(), "UASTChangesSaver")
  257. assert.Equal(t, len(ch.Provides()), 0)
  258. assert.Equal(t, len(ch.Requires()), 1)
  259. assert.Equal(t, ch.Requires()[0], "changed_uasts")
  260. opts := ch.ListConfigurationOptions()
  261. assert.Len(t, opts, 1)
  262. assert.Equal(t, opts[0].Name, ConfigUASTChangesSaverOutputPath)
  263. feats := ch.Features()
  264. assert.Len(t, feats, 1)
  265. assert.Equal(t, feats[0], "uast")
  266. assert.Equal(t, ch.Flag(), "dump-uast-changes")
  267. }
  268. func TestUASTChangesSaverConfiguration(t *testing.T) {
  269. facts := map[string]interface{}{}
  270. ch := fixtureUASTChangesSaver()
  271. ch.Configure(facts)
  272. assert.Empty(t, ch.OutputPath)
  273. facts[ConfigUASTChangesSaverOutputPath] = "libre"
  274. ch.Configure(facts)
  275. assert.Equal(t, ch.OutputPath, "libre")
  276. }
  277. func TestUASTChangesSaverRegistration(t *testing.T) {
  278. tp, exists := Registry.registered[(&UASTChangesSaver{}).Name()]
  279. assert.True(t, exists)
  280. assert.Equal(t, tp.Elem().Name(), "UASTChangesSaver")
  281. tp, exists = Registry.flags[(&UASTChangesSaver{}).Flag()]
  282. assert.True(t, exists)
  283. assert.Equal(t, tp.Elem().Name(), "UASTChangesSaver")
  284. }