uast_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  31. func TestUASTExtractorConfiguration(t *testing.T) {
  32. exr := fixtureUASTExtractor()
  33. facts := map[string]interface{}{}
  34. exr.Configure(facts)
  35. facts[ConfigUASTEndpoint] = "localhost:9432"
  36. facts[ConfigUASTTimeout] = 15
  37. facts[ConfigUASTPoolSize] = 7
  38. facts[ConfigUASTLanguages] = "C, Go"
  39. facts[ConfigUASTFailOnErrors] = true
  40. exr.Configure(facts)
  41. assert.Equal(t, exr.Endpoint, facts[ConfigUASTEndpoint])
  42. assert.NotNil(t, exr.Context)
  43. assert.Equal(t, exr.PoolSize, facts[ConfigUASTPoolSize])
  44. assert.True(t, exr.Languages["C"])
  45. assert.True(t, exr.Languages["Go"])
  46. assert.False(t, exr.Languages["Python"])
  47. assert.Equal(t, exr.FailOnErrors, true)
  48. }
  49. func TestUASTExtractorRegistration(t *testing.T) {
  50. tp, exists := Registry.registered[(&UASTExtractor{}).Name()]
  51. assert.True(t, exists)
  52. assert.Equal(t, tp.Elem().Name(), "UAST")
  53. tps, exists := Registry.provided[(&UASTExtractor{}).Provides()[0]]
  54. assert.True(t, exists)
  55. assert.Len(t, tps, 1)
  56. assert.Equal(t, tps[0].Elem().Name(), "UAST")
  57. }
  58. func TestUASTExtractorConsume(t *testing.T) {
  59. exr := fixtureUASTExtractor()
  60. changes := make(object.Changes, 2)
  61. // 2b1ed978194a94edeabbca6de7ff3b5771d4d665
  62. treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
  63. "96c6ece9b2f3c7c51b83516400d278dea5605100"))
  64. treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
  65. "251f2094d7b523d5bcc60e663b6cf38151bf8844"))
  66. changes[0] = &object.Change{From: object.ChangeEntry{
  67. Name: "analyser.go",
  68. Tree: treeFrom,
  69. TreeEntry: object.TreeEntry{
  70. Name: "analyser.go",
  71. Mode: 0100644,
  72. Hash: plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1"),
  73. },
  74. }, To: object.ChangeEntry{},
  75. }
  76. changes[1] = &object.Change{From: object.ChangeEntry{
  77. Name: "cmd/hercules/main.go",
  78. Tree: treeFrom,
  79. TreeEntry: object.TreeEntry{
  80. Name: "cmd/hercules/main.go",
  81. Mode: 0100644,
  82. Hash: plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9"),
  83. },
  84. }, To: object.ChangeEntry{
  85. Name: "cmd/hercules/main.go",
  86. Tree: treeTo,
  87. TreeEntry: object.TreeEntry{
  88. Name: "cmd/hercules/main.go",
  89. Mode: 0100644,
  90. Hash: plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72"),
  91. },
  92. },
  93. }
  94. cache := map[plumbing.Hash]*object.Blob{}
  95. hash := plumbing.NewHash("baa64828831d174f40140e4b3cfa77d1e917a2c1")
  96. cache[hash], _ = testRepository.BlobObject(hash)
  97. hash = plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619")
  98. cache[hash], _ = testRepository.BlobObject(hash)
  99. hash = plumbing.NewHash("c29112dbd697ad9b401333b80c18a63951bc18d9")
  100. cache[hash], _ = testRepository.BlobObject(hash)
  101. hash = plumbing.NewHash("f7d918ec500e2f925ecde79b51cc007bac27de72")
  102. cache[hash], _ = testRepository.BlobObject(hash)
  103. deps := map[string]interface{}{}
  104. deps["blob_cache"] = cache
  105. deps["changes"] = changes
  106. res, err := exr.Consume(deps)
  107. // No Go driver
  108. assert.Nil(t, res)
  109. assert.NotNil(t, err)
  110. changes[1] = &object.Change{From: object.ChangeEntry{}, To: object.ChangeEntry{
  111. Name: "labours.py",
  112. Tree: treeTo,
  113. TreeEntry: object.TreeEntry{
  114. Name: "labours.py",
  115. Mode: 0100644,
  116. Hash: plumbing.NewHash("5d78f57d732aed825764347ec6f3ab74d50d0619"),
  117. },
  118. },
  119. }
  120. res, err = exr.Consume(deps)
  121. assert.Nil(t, err)
  122. uasts := res["uasts"].(map[string]*uast.Node)
  123. assert.Equal(t, len(uasts), 1)
  124. assert.Equal(t, len(uasts["labours.py"].Children), 24)
  125. }