uast_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // +build !disable_babelfish
  2. package uast
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "fmt"
  9. "path"
  10. "github.com/gogo/protobuf/proto"
  11. "github.com/stretchr/testify/assert"
  12. "gopkg.in/bblfsh/sdk.v1/uast"
  13. "gopkg.in/src-d/go-git.v4/plumbing"
  14. "gopkg.in/src-d/go-git.v4/plumbing/object"
  15. "gopkg.in/src-d/hercules.v4/internal/core"
  16. "gopkg.in/src-d/hercules.v4/internal/pb"
  17. items "gopkg.in/src-d/hercules.v4/internal/plumbing"
  18. "gopkg.in/src-d/hercules.v4/internal/test"
  19. )
  20. func fixtureUASTExtractor() *Extractor {
  21. exr := Extractor{Endpoint: "0.0.0.0:9432"}
  22. exr.Initialize(test.Repository)
  23. exr.Languages["Python"] = true
  24. return &exr
  25. }
  26. func TestUASTExtractorMeta(t *testing.T) {
  27. exr := fixtureUASTExtractor()
  28. assert.Equal(t, exr.Name(), "UAST")
  29. assert.Equal(t, len(exr.Provides()), 1)
  30. assert.Equal(t, exr.Provides()[0], DependencyUasts)
  31. assert.Equal(t, len(exr.Requires()), 2)
  32. assert.Equal(t, exr.Requires()[0], items.DependencyTreeChanges)
  33. assert.Equal(t, exr.Requires()[1], items.DependencyBlobCache)
  34. opts := exr.ListConfigurationOptions()
  35. assert.Len(t, opts, 5)
  36. assert.Equal(t, opts[0].Name, ConfigUASTEndpoint)
  37. assert.Equal(t, opts[1].Name, ConfigUASTTimeout)
  38. assert.Equal(t, opts[2].Name, ConfigUASTPoolSize)
  39. assert.Equal(t, opts[3].Name, ConfigUASTFailOnErrors)
  40. assert.Equal(t, opts[4].Name, ConfigUASTLanguages)
  41. feats := exr.Features()
  42. assert.Len(t, feats, 1)
  43. assert.Equal(t, feats[0], FeatureUast)
  44. }
  45. func TestUASTExtractorConfiguration(t *testing.T) {
  46. exr := fixtureUASTExtractor()
  47. facts := map[string]interface{}{}
  48. exr.Configure(facts)
  49. facts[ConfigUASTEndpoint] = "localhost:9432"
  50. facts[ConfigUASTTimeout] = 15
  51. facts[ConfigUASTPoolSize] = 7
  52. facts[ConfigUASTLanguages] = "C, Go"
  53. facts[ConfigUASTFailOnErrors] = true
  54. exr.Configure(facts)
  55. assert.Equal(t, exr.Endpoint, facts[ConfigUASTEndpoint])
  56. assert.NotNil(t, exr.Context)
  57. assert.Equal(t, exr.PoolSize, facts[ConfigUASTPoolSize])
  58. assert.True(t, exr.Languages["C"])
  59. assert.True(t, exr.Languages["Go"])
  60. assert.False(t, exr.Languages["Python"])
  61. assert.Equal(t, exr.FailOnErrors, true)
  62. }
  63. func TestUASTExtractorRegistration(t *testing.T) {
  64. summoned := core.Registry.Summon((&Extractor{}).Name())
  65. assert.Len(t, summoned, 1)
  66. assert.Equal(t, summoned[0].Name(), "UAST")
  67. summoned = core.Registry.Summon((&Extractor{}).Provides()[0])
  68. assert.Len(t, summoned, 1)
  69. assert.Equal(t, summoned[0].Name(), "UAST")
  70. }
  71. func TestUASTExtractorConsume(t *testing.T) {
  72. exr := fixtureUASTExtractor()
  73. changes := make(object.Changes, 2)
  74. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  75. treeFrom, _ := test.Repository.TreeObject(plumbing.NewHash(
  76. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  77. treeTo, _ := test.Repository.TreeObject(plumbing.NewHash(
  78. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  79. changes[0] = &object.Change{From: object.ChangeEntry{
  80. Name: "analyser.go",
  81. Tree: treeFrom,
  82. TreeEntry: object.TreeEntry{
  83. Name: "analyser.go",
  84. Mode: 0100644,
  85. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  86. },
  87. }, To: object.ChangeEntry{},
  88. }
  89. changes[1] = &object.Change{From: object.ChangeEntry{
  90. Name: "cmd/hercules/main.go",
  91. Tree: treeFrom,
  92. TreeEntry: object.TreeEntry{
  93. Name: "cmd/hercules/main.go",
  94. Mode: 0100644,
  95. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  96. },
  97. }, To: object.ChangeEntry{
  98. Name: "cmd/hercules/main.go",
  99. Tree: treeTo,
  100. TreeEntry: object.TreeEntry{
  101. Name: "cmd/hercules/main.go",
  102. Mode: 0100644,
  103. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  104. },
  105. },
  106. }
  107. cache := map[plumbing.Hash]*object.Blob{}
  108. hash := plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  109. cache[hash], _ = test.Repository.BlobObject(hash)
  110. hash = plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619")
  111. cache[hash], _ = test.Repository.BlobObject(hash)
  112. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  113. cache[hash], _ = test.Repository.BlobObject(hash)
  114. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  115. cache[hash], _ = test.Repository.BlobObject(hash)
  116. deps := map[string]interface{}{}
  117. deps[items.DependencyBlobCache] = cache
  118. deps[items.DependencyTreeChanges] = changes
  119. res, err := exr.Consume(deps)
  120. // Language not enabled
  121. assert.Len(t, res[DependencyUasts], 0)
  122. assert.Nil(t, err)
  123. exr.Languages["Go3000"] = true
  124. res, err = exr.Consume(deps)
  125. // No Go driver
  126. assert.Len(t, res[DependencyUasts], 0)
  127. assert.Nil(t, err)
  128. hash = plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619")
  129. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  130. Name: "labours.py",
  131. Tree: treeTo,
  132. TreeEntry: object.TreeEntry{
  133. Name: "labours.py",
  134. Mode: 0100644,
  135. Hash: hash,
  136. },
  137. },
  138. }
  139. res, err = exr.Consume(deps)
  140. assert.Nil(t, err)
  141. uasts := res[DependencyUasts].(map[plumbing.Hash]*uast.Node)
  142. assert.Equal(t, len(uasts), 1)
  143. assert.Equal(t, len(uasts[hash].Children), 24)
  144. }
  145. func fixtureUASTChanges() *Changes {
  146. ch := Changes{}
  147. ch.Configure(nil)
  148. ch.Initialize(test.Repository)
  149. return &ch
  150. }
  151. func TestUASTChangesMeta(t *testing.T) {
  152. ch := fixtureUASTChanges()
  153. assert.Equal(t, ch.Name(), "UASTChanges")
  154. assert.Equal(t, len(ch.Provides()), 1)
  155. assert.Equal(t, ch.Provides()[0], DependencyUastChanges)
  156. assert.Equal(t, len(ch.Requires()), 2)
  157. assert.Equal(t, ch.Requires()[0], DependencyUasts)
  158. assert.Equal(t, ch.Requires()[1], items.DependencyTreeChanges)
  159. opts := ch.ListConfigurationOptions()
  160. assert.Len(t, opts, 0)
  161. feats := ch.Features()
  162. assert.Len(t, feats, 1)
  163. assert.Equal(t, feats[0], FeatureUast)
  164. }
  165. func TestUASTChangesRegistration(t *testing.T) {
  166. summoned := core.Registry.Summon((&Changes{}).Name())
  167. assert.Len(t, summoned, 1)
  168. assert.Equal(t, summoned[0].Name(), "UASTChanges")
  169. summoned = core.Registry.Summon((&Changes{}).Provides()[0])
  170. assert.True(t, len(summoned) >= 1)
  171. matched := false
  172. for _, tp := range summoned {
  173. matched = matched || tp.Name() == "UASTChanges"
  174. }
  175. assert.True(t, matched)
  176. }
  177. func TestUASTChangesConsume(t *testing.T) {
  178. uastsArray := []*uast.Node{}
  179. uasts := map[plumbing.Hash]*uast.Node{}
  180. hash := plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe")
  181. uasts[hash] = &uast.Node{}
  182. uasts[hash].InternalType = "uno"
  183. uastsArray = append(uastsArray, uasts[hash])
  184. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  185. uasts[hash] = &uast.Node{}
  186. uasts[hash].InternalType = "dos"
  187. uastsArray = append(uastsArray, uasts[hash])
  188. hash = plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  189. uasts[hash] = &uast.Node{}
  190. uasts[hash].InternalType = "tres"
  191. uastsArray = append(uastsArray, uasts[hash])
  192. hash = plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9")
  193. uasts[hash] = &uast.Node{}
  194. uasts[hash].InternalType = "quatro"
  195. uastsArray = append(uastsArray, uasts[hash])
  196. changes := make(object.Changes, 3)
  197. treeFrom, _ := test.Repository.TreeObject(plumbing.NewHash(
  198. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  199. treeTo, _ := test.Repository.TreeObject(plumbing.NewHash(
  200. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  201. changes[0] = &object.Change{From: object.ChangeEntry{
  202. Name: "analyser.go",
  203. Tree: treeFrom,
  204. TreeEntry: object.TreeEntry{
  205. Name: "analyser.go",
  206. Mode: 0100644,
  207. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  208. },
  209. }, To: object.ChangeEntry{
  210. Name: "analyser.go",
  211. Tree: treeTo,
  212. TreeEntry: object.TreeEntry{
  213. Name: "analyser.go",
  214. Mode: 0100644,
  215. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  216. },
  217. }}
  218. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  219. Name: "cmd/hercules/main.go",
  220. Tree: treeTo,
  221. TreeEntry: object.TreeEntry{
  222. Name: "cmd/hercules/main.go",
  223. Mode: 0100644,
  224. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  225. },
  226. },
  227. }
  228. changes[2] = &object.Change{To: object.ChangeEntry{}, From: object.ChangeEntry{
  229. Name: ".travis.yml",
  230. Tree: treeTo,
  231. TreeEntry: object.TreeEntry{
  232. Name: ".travis.yml",
  233. Mode: 0100644,
  234. Hash: plumbing.NewHash("291286b4ac41952cbd1389fda66420ec03c1a9fe"),
  235. },
  236. },
  237. }
  238. deps := map[string]interface{}{}
  239. deps[DependencyUasts] = uasts
  240. deps[items.DependencyTreeChanges] = changes
  241. ch := fixtureUASTChanges()
  242. ch.cache[changes[0].From.TreeEntry.Hash] = uastsArray[3]
  243. ch.cache[changes[2].From.TreeEntry.Hash] = uastsArray[0]
  244. resultMap, err := ch.Consume(deps)
  245. assert.Nil(t, err)
  246. result := resultMap[DependencyUastChanges].([]Change)
  247. assert.Len(t, result, 3)
  248. assert.Equal(t, result[0].Change, changes[0])
  249. assert.Equal(t, result[0].Before, uastsArray[3])
  250. assert.Equal(t, result[0].After, uastsArray[2])
  251. assert.Equal(t, result[1].Change, changes[1])
  252. assert.Nil(t, result[1].Before)
  253. assert.Equal(t, result[1].After, uastsArray[1])
  254. assert.Equal(t, result[2].Change, changes[2])
  255. assert.Equal(t, result[2].Before, uastsArray[0])
  256. assert.Nil(t, result[2].After)
  257. }
  258. func fixtureUASTChangesSaver() *ChangesSaver {
  259. ch := ChangesSaver{}
  260. ch.Initialize(test.Repository)
  261. return &ch
  262. }
  263. func TestUASTChangesSaverMeta(t *testing.T) {
  264. chs := fixtureUASTChangesSaver()
  265. assert.Equal(t, chs.Name(), "UASTChangesSaver")
  266. assert.Equal(t, len(chs.Provides()), 0)
  267. assert.Equal(t, len(chs.Requires()), 1)
  268. assert.Equal(t, chs.Requires()[0], DependencyUastChanges)
  269. opts := chs.ListConfigurationOptions()
  270. assert.Len(t, opts, 1)
  271. assert.Equal(t, opts[0].Name, ConfigUASTChangesSaverOutputPath)
  272. feats := chs.Features()
  273. assert.Len(t, feats, 1)
  274. assert.Equal(t, feats[0], FeatureUast)
  275. assert.Equal(t, chs.Flag(), "dump-uast-changes")
  276. }
  277. func TestUASTChangesSaverConfiguration(t *testing.T) {
  278. facts := map[string]interface{}{}
  279. chs := fixtureUASTChangesSaver()
  280. chs.Configure(facts)
  281. assert.Empty(t, chs.OutputPath)
  282. facts[ConfigUASTChangesSaverOutputPath] = "libre"
  283. chs.Configure(facts)
  284. assert.Equal(t, chs.OutputPath, "libre")
  285. }
  286. func TestUASTChangesSaverRegistration(t *testing.T) {
  287. summoned := core.Registry.Summon((&ChangesSaver{}).Name())
  288. assert.Len(t, summoned, 1)
  289. assert.Equal(t, summoned[0].Name(), "UASTChangesSaver")
  290. leaves := core.Registry.GetLeaves()
  291. matched := false
  292. for _, tp := range leaves {
  293. if tp.Flag() == (&ChangesSaver{}).Flag() {
  294. matched = true
  295. break
  296. }
  297. }
  298. assert.True(t, matched)
  299. }
  300. func TestUASTChangesSaverPayload(t *testing.T) {
  301. chs := fixtureUASTChangesSaver()
  302. deps := map[string]interface{}{}
  303. changes := make([]Change, 1)
  304. deps[DependencyUastChanges] = changes
  305. treeFrom, _ := test.Repository.TreeObject(plumbing.NewHash(
  306. "a1eb2ea76eb7f9bfbde9b243861474421000eb96"))
  307. treeTo, _ := test.Repository.TreeObject(plumbing.NewHash(
  308. "994eac1cd07235bb9815e547a75c84265dea00f5"))
  309. changes[0] = Change{Before: &uast.Node{}, After: &uast.Node{},
  310. Change: &object.Change{From: object.ChangeEntry{
  311. Name: "analyser.go",
  312. Tree: treeFrom,
  313. TreeEntry: object.TreeEntry{
  314. Name: "analyser.go",
  315. Mode: 0100644,
  316. Hash: plumbing.NewHash("dc248ba2b22048cc730c571a748e8ffcf7085ab9"),
  317. },
  318. }, To: object.ChangeEntry{
  319. Name: "analyser.go",
  320. Tree: treeTo,
  321. TreeEntry: object.TreeEntry{
  322. Name: "analyser.go",
  323. Mode: 0100644,
  324. Hash: plumbing.NewHash("334cde09da4afcb74f8d2b3e6fd6cce61228b485"),
  325. },
  326. }}}
  327. chs.Consume(deps)
  328. res := chs.Finalize()
  329. tmpdir, err := ioutil.TempDir("", "hercules-test-")
  330. assert.Nil(t, err)
  331. defer os.RemoveAll(tmpdir)
  332. chs.OutputPath = tmpdir
  333. buffer := &bytes.Buffer{}
  334. chs.Serialize(res, true, buffer)
  335. pbResults := &pb.UASTChangesSaverResults{}
  336. proto.Unmarshal(buffer.Bytes(), pbResults)
  337. assert.Len(t, pbResults.Changes, 1)
  338. assert.Equal(t, pbResults.Changes[0].FileName, "analyser.go")
  339. assert.Equal(t, pbResults.Changes[0].SrcAfter,
  340. path.Join(tmpdir, "0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.src"))
  341. assert.Equal(t, pbResults.Changes[0].SrcBefore,
  342. path.Join(tmpdir, "0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.src"))
  343. assert.Equal(t, pbResults.Changes[0].UastAfter,
  344. path.Join(tmpdir, "0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.pb"))
  345. assert.Equal(t, pbResults.Changes[0].UastBefore,
  346. path.Join(tmpdir, "0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.pb"))
  347. checkFiles := func() {
  348. files, err := ioutil.ReadDir(tmpdir)
  349. assert.Nil(t, err)
  350. assert.Len(t, files, 4)
  351. names := map[string]int{
  352. "0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.src": 1,
  353. "0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.src": 1,
  354. "0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.pb": 1,
  355. "0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.pb": 1,
  356. }
  357. matches := 0
  358. for _, fi := range files {
  359. matches += names[fi.Name()]
  360. os.Remove(fi.Name())
  361. }
  362. assert.Equal(t, matches, len(names))
  363. }
  364. checkFiles()
  365. buffer.Truncate(0)
  366. chs.Serialize(res, false, buffer)
  367. assert.Equal(t, buffer.String(), fmt.Sprintf(` - {file: analyser.go, src0: %s/0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.src, src1: %s/0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.src, uast0: %s/0_0_before_dc248ba2b22048cc730c571a748e8ffcf7085ab9.pb, uast1: %s/0_0_after_334cde09da4afcb74f8d2b3e6fd6cce61228b485.pb}
  368. `, tmpdir, tmpdir, tmpdir, tmpdir))
  369. checkFiles()
  370. }