Exemple #1
0
func main() {
	flag.Parse()

	// Get the file/directory to analyze.
	if len(flag.Args()) != 1 {
		shipshapeUsage()
		os.Exit(returnError)
	}

	thirdPartyAnalyzers := []string{}
	if *analyzerImages != "" {
		thirdPartyAnalyzers = strings.Split(*analyzerImages, ",")
	}
	cats := []string{}
	if *categories != "" {
		cats = strings.Split(*categories, ",")
	}

	options := cli.Options{
		File:                flag.Arg(0),
		ThirdPartyAnalyzers: thirdPartyAnalyzers,
		Build:               *build,
		TriggerCats:         cats,
		Dind:                *dind,
		Event:               *event,
		Repo:                *repo,
		StayUp:              *stayUp,
		Tag:                 *tag,
		LocalKythe:          *useLocalKythe,
	}
	if *jsonOutput == "" {
		options.HandleResponse = outputAsText
	} else {
		var allResponses rpcpb.ShipshapeResponse
		options.HandleResponse = func(msg *rpcpb.ShipshapeResponse, _ string) error {
			allResponses.AnalyzeResponse = append(allResponses.AnalyzeResponse, msg.AnalyzeResponse...)
			return nil
		}
		options.ResponsesDone = func() error {
			// TODO(ciera): these results aren't sorted. They should be sorted by path and start line
			b, err := json.Marshal(allResponses)
			if err != nil {
				return err
			}
			return ioutil.WriteFile(*jsonOutput, b, 0644)
		}
	}

	numResults, err := cli.New(options).Run()
	if err != nil {
		fmt.Printf("Error: %v", err.Error())
		os.Exit(returnError)
	}
	if numResults != 0 {
		os.Exit(returnFindings)
	}
	os.Exit(returnNoFindings)
}
func TestBuiltInAnalyzersPreBuild(t *testing.T) {
	options := Options{
		File:                "shipshape/cli/testdata/workspace1",
		ThirdPartyAnalyzers: []string{},
		Build:               "",
		TriggerCats:         []string{"PostMessage", "JSHint", "go vet", "PyLint"},
		Dind:                false,
		Event:               DefaultEvent,
		Repo:                DefaultRepo,
		StayUp:              true,
		Tag:                 *dockerTag,
		LocalKythe:          *localKythe,
	}
	var allResponses rpcpb.ShipshapeResponse
	options.HandleResponse = func(shipshapeResp *rpcpb.ShipshapeResponse, _ string) error {
		allResponses.AnalyzeResponse = append(allResponses.AnalyzeResponse, shipshapeResp.AnalyzeResponse...)
		return nil
	}
	returnedNotesCount, err := New(options).Run()
	if err != nil {
		t.Fatal(err)
	}
	testName := "TestBuiltInAnalyzerPreBuild"

	if got, want := countFailures(allResponses), 0; got != want {
		t.Errorf("%v: Wrong number of failures; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if countedNotes := countNotes(allResponses); returnedNotesCount != countedNotes {
		t.Errorf("%v: Inconsistent note count: returned %v, counted %v (proto data: %v", testName, returnedNotesCount, countedNotes, allResponses)
	}
	if got, want := returnedNotesCount, 39; got != want {
		t.Errorf("%v: Wrong number of notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PostMessage"), 2; got != want {
		t.Errorf("%v: Wrong number of PostMessage notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "JSHint"), 3; got != want {
		t.Errorf("%v: Wrong number of JSHint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "go vet"), 1; got != want {
		t.Errorf("%v: Wrong number of go vet notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PyLint"), 33; got != want {
		t.Errorf("%v: Wrong number of PyLint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
}
func main() {
	flag.Parse()

	// Get the file/directory to analyze.
	// If we are just showing category list, default to the current directory
	file := "."
	if len(flag.Args()) >= 1 {
		file = flag.Arg(0)
	} else if !*showCategories {
		shipshapeUsage()
		os.Exit(returnError)
	}

	thirdPartyAnalyzers := []string{}
	if *analyzerImages != "" {
		thirdPartyAnalyzers = strings.Split(*analyzerImages, ",")
	}
	cats := []string{}
	if *categories != "" {
		cats = strings.Split(*categories, ",")
	}

	options := cli.Options{
		File:                file,
		ThirdPartyAnalyzers: thirdPartyAnalyzers,
		Build:               *build,
		TriggerCats:         cats,
		Dind:                *dind,
		Event:               *event,
		Repo:                *repo,
		StayUp:              *stayUp,
		Tag:                 *tag,
		LocalKythe:          *useLocalKythe,
	}
	if *jsonOutput == "" {
		options.HandleResponse = outputAsText
	} else {
		// TODO(supertri): Does not work for showCategories
		var allResponses rpcpb.ShipshapeResponse
		options.HandleResponse = func(msg *rpcpb.ShipshapeResponse, _ string) error {
			allResponses.AnalyzeResponse = append(allResponses.AnalyzeResponse, msg.AnalyzeResponse...)
			return nil
		}
		options.ResponsesDone = func() error {
			// TODO(ciera): these results aren't sorted. They should be sorted by path and start line
			b, err := json.Marshal(allResponses)
			if err != nil {
				return err
			}
			return ioutil.WriteFile(*jsonOutput, b, 0644)
		}
	}
	invocation := cli.New(options)
	numResults := 0
	var err error = nil

	if *showCategories {
		err = invocation.ShowCategories()
	} else if *hotStart {
		err = invocation.StartService()
	} else {
		numResults, err = invocation.Run()
	}

	if err != nil {
		fmt.Printf("Error: %v\n", err.Error())
		os.Exit(returnError)
	}
	if numResults != 0 {
		os.Exit(returnFindings)
	}
	os.Exit(returnNoFindings)
}
func TestChangingDirs(t *testing.T) {
	tests := []struct {
		name           string
		file           string
		expectedJSHint int
		expectedGovet  int
		expectedPyLint int
		expectRestart  bool
	}{
		{
			name:           "ChildDir",
			file:           "shipshape/cli/testdata/workspace2/subworkspace1",
			expectedJSHint: 0,
			expectedGovet:  1,
			expectedPyLint: 0,
			expectRestart:  true,
		},
		{
			name:           "SiblingDir",
			file:           "shipshape/cli/testdata/workspace2/subworkspace2",
			expectedJSHint: 0,
			expectedGovet:  0,
			expectedPyLint: 22,
			expectRestart:  true,
		},
		{
			name:           "ParentDir",
			file:           "shipshape/cli/testdata/workspace2",
			expectedJSHint: 3,
			expectedGovet:  1,
			expectedPyLint: 22,
			expectRestart:  true,
		},
		{
			name:           "File",
			file:           "shipshape/cli/testdata/workspace2/test.js",
			expectedJSHint: 3,
			expectedGovet:  0,
			expectedPyLint: 0,
			expectRestart:  false,
		},
		{
			name:           "ParentToChild",
			file:           "shipshape/cli/testdata/workspace2/subworkspace2",
			expectedJSHint: 0,
			expectedGovet:  0,
			expectedPyLint: 22,
			expectRestart:  false,
		},
		{
			name:           "ParentToOtherChild",
			file:           "shipshape/cli/testdata/workspace2/subworkspace1",
			expectedJSHint: 0,
			expectedGovet:  1,
			expectedPyLint: 0,
			expectRestart:  false,
		},
	}

	// Clean up the docker state
	container := "shipping_container"
	cleanExistingContainer(t, container)
	oldId := ""

	for _, test := range tests {
		options := Options{
			File:                test.file,
			ThirdPartyAnalyzers: []string{},
			Build:               "",
			TriggerCats:         []string{"JSHint", "go vet", "PyLint"},
			Dind:                false,
			Event:               defaults.DefaultEvent,
			Repo:                defaults.DefaultRepo,
			StayUp:              true,
			Tag:                 *dockerTag,
			LocalKythe:          *localKythe,
		}
		var allResponses rpcpb.ShipshapeResponse
		options.HandleResponse = func(shipshapeResp *rpcpb.ShipshapeResponse, _ string) error {
			allResponses.AnalyzeResponse =
				append(allResponses.AnalyzeResponse, shipshapeResp.AnalyzeResponse...)
			return nil
		}
		testName := test.name
		if _, err := New(options).Run(); err != nil {
			t.Fatalf("%v: Failure on service call; err: %v", testName, err)
		}
		if got, want := countFailures(allResponses), 0; got != want {
			t.Errorf("%v: Wrong number of failures; got %v, want %v (proto data: %v)",
				testName, got, want, allResponses)
		}
		if got, want := countCategoryNotes(allResponses, "JSHint"), test.expectedJSHint; got != want {
			t.Errorf("%v: Wrong number of JSHint notes; got %v, want %v (proto data: %v)",
				testName, got, want, allResponses)
		}
		if got, want := countCategoryNotes(allResponses, "go vet"), test.expectedGovet; got != want {
			t.Errorf("%v: Wrong number of go vet notes; got %v, want %v (proto data: %v)",
				testName, got, want, allResponses)
		}
		if got, want := countCategoryNotes(allResponses, "PyLint"), test.expectedPyLint; got != want {
			t.Errorf("%v: Wrong number of PyLint notes; got %v, want %v (proto data: %v)",
				testName, got, want, allResponses)
		}
		newId, err := docker.ContainerId(container)
		if err != nil {
			t.Fatalf("%v: Could not get container id: %v", testName, err)
		}
		if got, want := newId != oldId, test.expectRestart; got != want {
			t.Errorf("%v: Incorrect restart status for container. Got %v, want %v", testName, got, want)
		}
		oldId = newId
	}
}