Преглед изворни кода

Merge pull request #194 from vmarkovtsev/master

Nicer Babelfish error reporting
Vadim Markovtsev пре 6 година
родитељ
комит
146e39f211

+ 7 - 2
internal/core/forks.go

@@ -192,10 +192,15 @@ func printAction(p runAction) {
 // Yes, it *is* possible to have several identical parents, and Hercules used to crash because of that.
 func getCommitParents(commit *object.Commit) []plumbing.Hash {
 	result := make([]plumbing.Hash, 0, len(commit.ParentHashes))
-	parents := map[plumbing.Hash]bool{}
+	var parents map[plumbing.Hash]bool
+	if len(commit.ParentHashes) > 1 {
+		parents = map[plumbing.Hash]bool{}
+	}
 	for _, parent := range commit.ParentHashes {
 		if _, exists := parents[parent]; !exists {
-			parents[parent] = true
+			if parents != nil {
+				parents[parent] = true
+			}
 			result = append(result, parent)
 		}
 	}

+ 13 - 0
internal/core/pipeline.go

@@ -626,6 +626,15 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 // resolves the execution DAG, Configure()-s and Initialize()-s the items in it in the
 // topological dependency order. `facts` are passed inside Configure(). They are mutable.
 func (pipeline *Pipeline) Initialize(facts map[string]interface{}) error {
+	cleanReturn := false
+	defer func() {
+		if !cleanReturn {
+			remotes, _ := pipeline.repository.Remotes()
+			if len(remotes) > 0 {
+				log.Printf("Failed to initialize the pipeline on %s", remotes[0].Config().URLs)
+			}
+		}
+	}()
 	if facts == nil {
 		facts = map[string]interface{}{}
 	}
@@ -651,18 +660,21 @@ func (pipeline *Pipeline) Initialize(facts map[string]interface{}) error {
 	if dryRun, exists := facts[ConfigPipelineDryRun].(bool); exists {
 		pipeline.DryRun = dryRun
 		if dryRun {
+			cleanReturn = true
 			return nil
 		}
 	}
 	for _, item := range pipeline.items {
 		err := item.Configure(facts)
 		if err != nil {
+			cleanReturn = true
 			return errors.Wrapf(err, "%s failed to configure", item.Name())
 		}
 	}
 	for _, item := range pipeline.items {
 		err := item.Initialize(pipeline.repository)
 		if err != nil {
+			cleanReturn = true
 			return errors.Wrapf(err, "%s failed to initialize", item.Name())
 		}
 	}
@@ -670,6 +682,7 @@ func (pipeline *Pipeline) Initialize(facts map[string]interface{}) error {
 		// if we want hibernation, then we want to minimize RSS
 		debug.SetGCPercent(20) // the default is 100
 	}
+	cleanReturn = true
 	return nil
 }
 

+ 11 - 0
internal/core/pipeline_test.go

@@ -29,6 +29,8 @@ type testPipelineItem struct {
 	TestError        bool
 	ConfigureRaises  bool
 	InitializeRaises bool
+	InitializePanics bool
+	ConsumePanics    bool
 }
 
 func (item *testPipelineItem) Name() string {
@@ -76,6 +78,9 @@ func (item *testPipelineItem) Features() []string {
 }
 
 func (item *testPipelineItem) Initialize(repository *git.Repository) error {
+	if item.InitializePanics {
+		panic("!")
+	}
 	item.Initialized = repository != nil
 	item.Merged = new(bool)
 	item.MergeState = new(int)
@@ -89,6 +94,9 @@ func (item *testPipelineItem) Consume(deps map[string]interface{}) (map[string]i
 	if item.TestError {
 		return nil, errors.New("error")
 	}
+	if item.ConsumePanics {
+		panic("!")
+	}
 	obj, exists := deps[DependencyCommit]
 	item.DepsConsumed = exists
 	if item.DepsConsumed {
@@ -268,6 +276,9 @@ func TestPipelineErrors(t *testing.T) {
 	assert.NotNil(t, err)
 	assert.Contains(t, err.Error(), "initialize")
 	assert.Contains(t, err.Error(), "test2")
+	item.InitializeRaises = false
+	item.InitializePanics = true
+	assert.Panics(t, func() { pipeline.Initialize(map[string]interface{}{}) })
 }
 
 func TestPipelineRun(t *testing.T) {

+ 6 - 1
internal/plumbing/uast/uast.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"os"
 	"path"
 	"runtime"
@@ -172,7 +173,11 @@ func (exr *Extractor) Initialize(repository *git.Repository) error {
 	for i := 0; i < poolSize; i++ {
 		client, err := bblfsh.NewClient(exr.Endpoint)
 		if err != nil {
-			panic(err)
+			if err.Error() == "context deadline exceeded" {
+				log.Println("Looks like the Babelfish server is not running. Please refer " +
+					"to https://docs.sourced.tech/babelfish/using-babelfish/getting-started#running-with-docker-recommended")
+			}
+			return err
 		}
 		exr.clients[i] = client
 	}

+ 10 - 1
internal/plumbing/uast/uast_test.go

@@ -23,7 +23,10 @@ import (
 
 func fixtureUASTExtractor() *Extractor {
 	exr := Extractor{Endpoint: "0.0.0.0:9432"}
-	exr.Initialize(test.Repository)
+	err := exr.Initialize(test.Repository)
+	if err != nil {
+		panic(err)
+	}
 	return &exr
 }
 
@@ -80,6 +83,12 @@ func TestUASTExtractorRegistration(t *testing.T) {
 	assert.Equal(t, summoned[0].Name(), "UAST")
 }
 
+func TestUASTExtractorNoBabelfish(t *testing.T) {
+	exr := Extractor{Endpoint: "0.0.0.0:56934"}
+	err := exr.Initialize(test.Repository)
+	assert.NotNil(t, err)
+}
+
 func TestUASTExtractorConsume(t *testing.T) {
 	exr := fixtureUASTExtractor()
 	changes := make(object.Changes, 3)