Repository: datreeio/datree
Pull request #63 (2021-06-09 11:00:52)

fix: rules count in summary


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index d253012..7766f03 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -28,7 +28,6 @@ func New(c CLIClient) *Evaluator {
 type EvaluationResults struct {
 	FileNameRuleMapper map[string]map[int]*Rule
 	Summary            struct {
-		RulesCount       int
 		TotalFailedRules int
 		FilesCount       int
 		TotalPassedCount int
@@ -97,7 +96,6 @@ func (e *Evaluator) Evaluate(filesConfigurations []*extractor.FileConfigurations
 func (e *Evaluator) formatEvaluationResults(evaluationResults []*cliClient.EvaluationResult, filesCount int) *EvaluationResults {
 	mapper := make(map[string]map[int]*Rule)
 
-	totalRulesCount := len(evaluationResults)
 	totalFailedCount := 0
 	totalPassedCount := filesCount
 
@@ -122,12 +120,10 @@ func (e *Evaluator) formatEvaluationResults(evaluationResults []*cliClient.Evalu
 	results := &EvaluationResults{
 		FileNameRuleMapper: mapper,
 		Summary: struct {
-			RulesCount       int
 			TotalFailedRules int
 			FilesCount       int
 			TotalPassedCount int
 		}{
-			RulesCount:       totalRulesCount,
 			TotalFailedRules: totalFailedCount,
 			FilesCount:       filesCount,
 			TotalPassedCount: totalPassedCount,
diff --git a/bl/evaluation/printer.go b/bl/evaluation/printer.go
index 91729df..0fb3942 100644
--- a/bl/evaluation/printer.go
+++ b/bl/evaluation/printer.go
@@ -154,27 +154,18 @@ func (t OutputTitle) String() string {
 }
 func parseEvaluationResultsToSummary(results *EvaluationResults, evaluationSummary printer.EvaluationSummary, loginURL string) printer.Summary {
 	configsCount := evaluationSummary.ConfigsCount
-	rulesCount := 0
 	totalRulesEvaluated := 0
 	totalFailedRules := 0
 	totalPassedRules := 0
 
 	if results != nil {
-		rulesCount = results.Summary.RulesCount
-		totalRulesEvaluated = results.Summary.RulesCount * results.Summary.FilesCount
+		totalRulesEvaluated = evaluationSummary.RulesCount * results.Summary.FilesCount
 		totalFailedRules = results.Summary.TotalFailedRules
 		totalPassedRules = totalRulesEvaluated - totalFailedRules
 	}
 
-	var rulesCountRightCol string
-	if rulesCount == 0 {
-		rulesCountRightCol = fmt.Sprint("N/A")
-	} else {
-		rulesCountRightCol = fmt.Sprint(rulesCount)
-	}
-
 	plainRows := []printer.SummaryItem{
-		{LeftCol: EnabledRules.String(), RightCol: rulesCountRightCol, RowIndex: 0},
+		{LeftCol: EnabledRules.String(), RightCol: fmt.Sprint(evaluationSummary.RulesCount), RowIndex: 0},
 		{LeftCol: EvaluatedConfigurations.String(), RightCol: fmt.Sprint(configsCount), RowIndex: 1},
 		{LeftCol: TotalRulesEvaluated.String(), RightCol: fmt.Sprint(totalRulesEvaluated), RowIndex: 2},
 		{LeftCol: SeeAll.String(), RightCol: loginURL, RowIndex: 5},
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index 3e55344..9121cc2 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -23,6 +23,7 @@ type CreateEvaluationRequest struct {
 type CreateEvaluationResponse struct {
 	EvaluationId int    `json:"evaluationId"`
 	K8sVersion   string `json:"k8sVersion"`
+	RulesCount   int    `json:"rulesCount"`
 }
 
 func (c *CliClient) CreateEvaluation(request *CreateEvaluationRequest) (*CreateEvaluationResponse, error) {
diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go
index ccdf9eb..8f1a49c 100644
--- a/pkg/printer/printer.go
+++ b/pkg/printer/printer.go
@@ -107,6 +107,7 @@ type Summary struct {
 
 type EvaluationSummary struct {
 	ConfigsCount              int
+	RulesCount                int
 	FilesCount                int
 	PassedYamlValidationCount int
 	PassedK8sValidationCount  int


Test patch:
diff --git a/bl/evaluation/evaluator_test.go b/bl/evaluation/evaluator_test.go
index e70efa5..378395f 100644
--- a/bl/evaluation/evaluator_test.go
+++ b/bl/evaluation/evaluator_test.go
@@ -123,6 +123,7 @@ type evaluateArgs struct {
 	validFilesConfigurations []*extractor.FileConfigurations
 	evaluationId             int
 	osInfo                   *OSInfo
+	rulesCount               int
 }
 
 type evaluateExpected struct {
@@ -189,12 +190,10 @@ func request_evaluation_all_valid() *evaluateTestCase {
 			response: &EvaluationResults{
 				FileNameRuleMapper: make(map[string]map[int]*Rule),
 				Summary: struct {
-					RulesCount       int
 					TotalFailedRules int
 					FilesCount       int
 					TotalPassedCount int
 				}{
-					RulesCount:       0,
 					TotalFailedRules: 0,
 					FilesCount:       1,
 					TotalPassedCount: 1,
@@ -253,12 +252,10 @@ func request_evaluation_all_invalid() *evaluateTestCase {
 			response: &EvaluationResults{
 				FileNameRuleMapper: make(map[string]map[int]*Rule),
 				Summary: struct {
-					RulesCount       int
 					TotalFailedRules int
 					FilesCount       int
 					TotalPassedCount int
 				}{
-					RulesCount:       0,
 					TotalFailedRules: 0,
 					FilesCount:       1,
 					TotalPassedCount: 0,
diff --git a/bl/evaluation/printer_test.go b/bl/evaluation/printer_test.go
index eb664c0..d6ac9c5 100644
--- a/bl/evaluation/printer_test.go
+++ b/bl/evaluation/printer_test.go
@@ -78,12 +78,10 @@ func print_resultst(outputFormat string) *printResultsTestCase {
 			results: &EvaluationResults{
 				FileNameRuleMapper: map[string]map[int]*Rule{},
 				Summary: struct {
-					RulesCount       int
 					TotalFailedRules int
 					FilesCount       int
 					TotalPassedCount int
 				}{
-					RulesCount:       0,
 					TotalFailedRules: 0,
 					FilesCount:       0,
 					TotalPassedCount: 0,
diff --git a/cmd/test/main.go b/cmd/test/main.go
index bc6b9c8..db76ab2 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -180,6 +180,7 @@ func test(ctx *TestCommandContext, paths []string, flags TestCommandFlags) error
 
 	evaluationSummary := printer.EvaluationSummary{
 		FilesCount:                filesPathsLen,
+		RulesCount:                createEvaluationResponse.RulesCount,
 		PassedYamlValidationCount: passedYamlValidationCount,
 		PassedK8sValidationCount:  passedK8sValidationCount,
 		ConfigsCount:              configsCount,
diff --git a/cmd/test/main_test.go b/cmd/test/main_test.go
index a71cb22..1a7dbbc 100644
--- a/cmd/test/main_test.go
+++ b/cmd/test/main_test.go
@@ -104,16 +104,15 @@ func TestTestCommand(t *testing.T) {
 
 	evaluationResults := &evaluation.EvaluationResults{
 		FileNameRuleMapper: map[string]map[int]*evaluation.Rule{}, Summary: struct {
-			RulesCount       int
 			TotalFailedRules int
 			FilesCount       int
 			TotalPassedCount int
-		}{RulesCount: 1, TotalFailedRules: 0, FilesCount: 0, TotalPassedCount: 1},
+		}{TotalFailedRules: 0, FilesCount: 0, TotalPassedCount: 1},
 	}
 
 	mockedEvaluator := &mockEvaluator{}
 	mockedEvaluator.On("Evaluate", mock.Anything, mock.Anything, mock.Anything).Return(evaluationResults, nil)
-	mockedEvaluator.On("CreateEvaluation", mock.Anything, mock.Anything, mock.Anything).Return(&cliClient.CreateEvaluationResponse{EvaluationId: evaluationId, K8sVersion: "1.18.0"}, nil)
+	mockedEvaluator.On("CreateEvaluation", mock.Anything, mock.Anything, mock.Anything).Return(&cliClient.CreateEvaluationResponse{EvaluationId: evaluationId, K8sVersion: "1.18.0", RulesCount: 21}, nil)
 	mockedEvaluator.On("UpdateFailedYamlValidation", mock.Anything, mock.Anything, mock.Anything).Return(nil)
 	mockedEvaluator.On("UpdateFailedK8sValidation", mock.Anything, mock.Anything, mock.Anything).Return(nil)
 	messager := &mockMessager{}