Browse Source

Add some pipeline and uast tests

Vadim Markovtsev 7 years ago
parent
commit
973693ed1d
4 changed files with 98 additions and 12 deletions
  1. 6 0
      pipeline.go
  2. 46 5
      pipeline_test.go
  3. 10 7
      uast.go
  4. 36 0
      uast_test.go

+ 6 - 0
pipeline.go

@@ -146,6 +146,12 @@ func (acf *arrayFeatureFlags) Set(value string) error {
 
 var featureFlags = arrayFeatureFlags{Flags: []string{}, Choices: map[string]bool{}}
 
+// AddFlags inserts the cmdline options from PipelineItem.ListConfigurationOptions(),
+// FeaturedPipelineItem().Features() and LeafPipelineItem.Flag() into the global "flag" parser
+// built into the Go runtime.
+// Returns the "facts" which can be fed into PipelineItem.Configure() and the dictionary of
+// runnable analysis (LeafPipelineItem) choices. E.g. if "BurndownAnalysis" was activated
+// through "-burndown" cmdline argument, this mapping would contain ["BurndownAnalysis"] = *true.
 func (registry *PipelineItemRegistry) AddFlags() (map[string]interface{}, map[string]*bool) {
 	flags := map[string]interface{}{}
 	deployed := map[string]*bool{}

+ 46 - 5
pipeline_test.go

@@ -5,6 +5,8 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"path"
+	"reflect"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -12,7 +14,7 @@ import (
 	"gopkg.in/src-d/go-git.v4/plumbing"
 	"gopkg.in/src-d/go-git.v4/plumbing/object"
 	"gopkg.in/src-d/go-git.v4/storage/memory"
-	"path"
+	"flag"
 )
 
 type testPipelineItem struct {
@@ -50,6 +52,15 @@ func (item *testPipelineItem) ListConfigurationOptions() []ConfigurationOption {
 	return options[:]
 }
 
+func (item *testPipelineItem) Flag() string {
+	return "mytest"
+}
+
+func (item *testPipelineItem) Features() []string {
+	f := [...]string{"power"}
+	return f[:]
+}
+
 func (item *testPipelineItem) Initialize(repository *git.Repository) {
 	item.Initialized = repository != nil
 }
@@ -76,6 +87,40 @@ func (item *testPipelineItem) Finalize() interface{} {
 	return item
 }
 
+func (item *testPipelineItem) Serialize(result interface{}, binary bool, writer io.Writer) error {
+	return nil
+}
+
+func getRegistry() *PipelineItemRegistry {
+	return &PipelineItemRegistry{
+		provided:   map[string][]reflect.Type{},
+		registered: map[string]reflect.Type{},
+		flags:      map[string]reflect.Type{},
+	}
+}
+
+func TestPipelineItemRegistrySummon(t *testing.T) {
+	reg := getRegistry()
+	reg.Register(&testPipelineItem{})
+	summoned := reg.Summon((&testPipelineItem{}).Provides()[0])
+	assert.Len(t, summoned, 1)
+	assert.Equal(t, summoned[0].Name(), (&testPipelineItem{}).Name())
+	summoned = reg.Summon((&testPipelineItem{}).Name())
+	assert.Len(t, summoned, 1)
+	assert.Equal(t, summoned[0].Name(), (&testPipelineItem{}).Name())
+}
+
+func TestPipelineItemRegistryAddFlags(t *testing.T) {
+	reg := getRegistry()
+	reg.Register(&testPipelineItem{})
+	facts, deployed := reg.AddFlags()
+	assert.Len(t, facts, 1)
+	assert.IsType(t, 0, facts[(&testPipelineItem{}).ListConfigurationOptions()[0].Name])
+	assert.Len(t, deployed, 1)
+	assert.Contains(t, deployed, (&testPipelineItem{}).Name())
+	assert.NotNil(t, flag.Lookup((&testPipelineItem{}).Flag()))
+}
+
 type dependingTestPipelineItem struct {
 	DependencySatisfied  bool
 	TestNilConsumeReturn bool
@@ -122,10 +167,6 @@ func (item *dependingTestPipelineItem) Consume(deps map[string]interface{}) (map
 	}
 }
 
-func (item *dependingTestPipelineItem) Finalize() interface{} {
-	return item.DependencySatisfied
-}
-
 func TestPipelineRun(t *testing.T) {
 	pipeline := NewPipeline(testRepository)
 	item := &testPipelineItem{}

+ 10 - 7
uast.go

@@ -42,11 +42,11 @@ type UASTExtractor struct {
 const (
 	UAST_EXTRACTION_SKIPPED = -(1 << 31)
 
-	ConfigUASTEndpoint     = "UAST.Endpoint"
-	ConfigUASTTimeout      = "UAST.Timeout"
-	ConfigUASTPoolSize     = "UAST.PoolSize"
-	ConfigUASTFailOnErrors = "UAST.FailOnErrors"
-	ConfigUASTLanguages    = "UAST.Languages"
+	ConfigUASTEndpoint     = "ConfigUASTEndpoint"
+	ConfigUASTTimeout      = "ConfigUASTTimeout"
+	ConfigUASTPoolSize     = "ConfigUASTPoolSize"
+	ConfigUASTFailOnErrors = "ConfigUASTFailOnErrors"
+	ConfigUASTLanguages    = "ConfigUASTLanguages"
 )
 
 type uastTask struct {
@@ -127,7 +127,7 @@ func (exr *UASTExtractor) Configure(facts map[string]interface{}) {
 	if val, exists := facts[ConfigUASTEndpoint].(string); exists {
 		exr.Endpoint = val
 	}
-	if val, exists := facts["UAST.Timeout"].(int); exists {
+	if val, exists := facts[ConfigUASTTimeout].(int); exists {
 		exr.Context = func() context.Context {
 			ctx, _ := context.WithTimeout(context.Background(),
 				time.Duration(val)*time.Second)
@@ -140,7 +140,7 @@ func (exr *UASTExtractor) Configure(facts map[string]interface{}) {
 	if val, exists := facts[ConfigUASTLanguages].(string); exists {
 		exr.Languages = map[string]bool{}
 		for _, lang := range strings.Split(val, ",") {
-			exr.Languages[lang] = true
+			exr.Languages[strings.TrimSpace(lang)] = true
 		}
 	}
 	if val, exists := facts[ConfigUASTFailOnErrors].(bool); exists {
@@ -177,6 +177,9 @@ func (exr *UASTExtractor) Initialize(repository *git.Repository) {
 		panic(err)
 	}
 	exr.ProcessedFiles = map[string]int{}
+	if exr.Languages == nil {
+		exr.Languages = map[string]bool{}
+	}
 }
 
 func (exr *UASTExtractor) Consume(deps map[string]interface{}) (map[string]interface{}, error) {

+ 36 - 0
uast_test.go

@@ -24,6 +24,42 @@ func TestUASTExtractorMeta(t *testing.T) {
 	assert.Equal(t, len(exr.Requires()), 2)
 	assert.Equal(t, exr.Requires()[0], "changes")
 	assert.Equal(t, exr.Requires()[1], "blob_cache")
+	opts := exr.ListConfigurationOptions()
+	assert.Len(t, opts, 5)
+	assert.Equal(t, opts[0].Name, ConfigUASTEndpoint)
+	assert.Equal(t, opts[1].Name, ConfigUASTTimeout)
+	assert.Equal(t, opts[2].Name, ConfigUASTPoolSize)
+	assert.Equal(t, opts[3].Name, ConfigUASTFailOnErrors)
+	assert.Equal(t, opts[4].Name, ConfigUASTLanguages)
+}
+
+func TestUASTExtractorConfiguration(t *testing.T) {
+	exr := fixtureUASTExtractor()
+	facts := map[string]interface{}{}
+	exr.Configure(facts)
+	facts[ConfigUASTEndpoint] = "localhost:9432"
+	facts[ConfigUASTTimeout] = 15
+	facts[ConfigUASTPoolSize] = 7
+	facts[ConfigUASTLanguages] = "C, Go"
+	facts[ConfigUASTFailOnErrors] = true
+	exr.Configure(facts)
+	assert.Equal(t, exr.Endpoint, facts[ConfigUASTEndpoint])
+	assert.NotNil(t, exr.Context)
+	assert.Equal(t, exr.PoolSize, facts[ConfigUASTPoolSize])
+	assert.True(t, exr.Languages["C"])
+	assert.True(t, exr.Languages["Go"])
+	assert.False(t, exr.Languages["Python"])
+	assert.Equal(t, exr.FailOnErrors, true)
+}
+
+func TestUASTExtractorRegistration(t *testing.T) {
+	tp, exists := Registry.registered[(&UASTExtractor{}).Name()]
+	assert.True(t, exists)
+	assert.Equal(t, tp.Elem().Name(), "UAST")
+	tps, exists := Registry.provided[(&UASTExtractor{}).Provides()[0]]
+	assert.True(t, exists)
+	assert.Len(t, tps, 1)
+	assert.Equal(t, tps[0].Elem().Name(), "UAST")
 }
 
 func TestUASTExtractorConsume(t *testing.T) {