Repository: datreeio/datree
Pull request #660 (2022-06-02 09:23:11)

refactor: extract method GetResultsText in evaluation printer


Patch:
diff --git a/internal/fixtures/kube/combinations.yaml b/internal/fixtures/kube/combinations.yaml
index 5368501..1122036 100644
--- a/internal/fixtures/kube/combinations.yaml
+++ b/internal/fixtures/kube/combinations.yaml
@@ -51,7 +51,7 @@ metadata:
     environment: prod
     app: web
   annotations:
-    datree.skip/CONTAINERS_MISSING_MEMORY_LIMIT_KEY:  ''
+    datree.skip/CONTAINERS_MISSING_MEMORY_LIMIT_KEY: ''
 spec:
   replicas: 2
   selector:
diff --git a/pkg/evaluation/printer.go b/pkg/evaluation/printer.go
index c442ad2..1a31c61 100644
--- a/pkg/evaluation/printer.go
+++ b/pkg/evaluation/printer.go
@@ -10,8 +10,6 @@ import (
 	"sort"
 	"strings"
 
-	"github.com/fatih/color"
-
 	"github.com/datreeio/datree/bl/validation"
 	"github.com/datreeio/datree/pkg/extractor"
 
@@ -19,8 +17,6 @@ import (
 	"gopkg.in/yaml.v2"
 )
 
-var out = color.Output
-
 type Printer interface {
 	GetWarningsText(warnings []printer.Warning) string
 	GetSummaryTableText(summary printer.Summary) string
@@ -56,6 +52,16 @@ type textOutputData struct {
 }
 
 func PrintResults(resultsData *PrintResultsData) error {
+	resultsText, err := GetResultsText(resultsData)
+	if err != nil {
+		return err
+	}
+
+	fmt.Print(resultsText)
+	return nil
+}
+
+func GetResultsText(resultsData *PrintResultsData) (string, error) {
 	if IsFormattedOutputOption(resultsData.OutputFormat) {
 		nonInteractiveEvaluationResults := resultsData.Results.NonInteractiveEvaluationResults
 		if nonInteractiveEvaluationResults == nil {
@@ -103,59 +109,52 @@ func PrintResults(resultsData *PrintResultsData) error {
 	}
 }
 
-func jsonOutput(formattedOutput *FormattedOutput) error {
+func jsonOutput(formattedOutput *FormattedOutput) (string, error) {
 	jsonOutput, err := json.Marshal(formattedOutput)
 	if err != nil {
-		fmt.Println(err)
-		return err
+		return "", err
 	}
 
-	fmt.Println(string(jsonOutput))
-	return nil
+	return fmt.Sprintln(string(jsonOutput)), nil
 }
 
-func yamlOutput(formattedOutput *FormattedOutput) error {
+func yamlOutput(formattedOutput *FormattedOutput) (string, error) {
 	yamlOutput, err := yaml.Marshal(formattedOutput)
 	if err != nil {
-		fmt.Println(err)
-		return err
+		return "", err
 	}
 
-	fmt.Println(string(yamlOutput))
-	return nil
+	return fmt.Sprintln(string(yamlOutput)), nil
 }
 
-func xmlOutput(formattedOutput *FormattedOutput) error {
-	return printAsXml(formattedOutput)
+func xmlOutput(formattedOutput *FormattedOutput) (string, error) {
+	return getXmlOutput(formattedOutput)
 }
 
-func jUnitOutput(formattedOutput *FormattedOutput, additionalJUnitData AdditionalJUnitData) error {
-	return printAsXml(FormattedOutputToJUnitOutput(*formattedOutput, additionalJUnitData))
+func jUnitOutput(formattedOutput *FormattedOutput, additionalJUnitData AdditionalJUnitData) (string, error) {
+	return getXmlOutput(FormattedOutputToJUnitOutput(*formattedOutput, additionalJUnitData))
 }
 
-func printAsXml(output interface{}) error {
+func getXmlOutput(output interface{}) (string, error) {
 	xmlOutput, err := xml.MarshalIndent(output, "", "\t")
 	xmlOutput = []byte(xml.Header + string(xmlOutput))
 	if err != nil {
-		fmt.Println(err)
-		return err
+		return "", err
 	}
 
-	fmt.Println(string(xmlOutput))
-	return nil
+	return fmt.Sprintln(string(xmlOutput)), nil
 }
 
-func textOutput(outputData textOutputData) error {
+func textOutput(outputData textOutputData) (string, error) {
 	sb := strings.Builder{}
 	pwd, err := os.Getwd()
 	if err != nil {
-		return err
+		return "", err
 	}
 
 	warnings, err := parseToPrinterWarnings(outputData.results, outputData.invalidYamlFiles, outputData.invalidK8sFiles, pwd, outputData.k8sVersion, outputData.k8sValidationWarnings, outputData.Verbose)
 	if err != nil {
-		fmt.Println(err)
-		return err
+		return "", err
 	}
 
 	warningsText := outputData.printer.GetWarningsText(warnings)
@@ -169,12 +168,7 @@ func textOutput(outputData textOutputData) error {
 	summaryTableText := outputData.printer.GetSummaryTableText(summary)
 	sb.WriteString(summaryTableText)
 
-	_, err = out.Write([]byte(sb.String()))
-	if err != nil {
-		return err
-	}
-
-	return nil
+	return sb.String(), nil
 }
 
 func parseInvalidYamlFilesToWarnings(invalidYamlFiles []*extractor.InvalidFile) []printer.Warning {


Test patch:
diff --git a/pkg/evaluation/printer_test.go b/pkg/evaluation/printer_test.go
index 40b679c..a56033f 100644
--- a/pkg/evaluation/printer_test.go
+++ b/pkg/evaluation/printer_test.go
@@ -1,10 +1,7 @@
 package evaluation
 
 import (
-	"bytes"
 	"errors"
-	"io"
-	"log"
 	"os"
 	"testing"
 
@@ -114,16 +111,16 @@ func TestCustomOutputs(t *testing.T) {
 	additionalJUnitData := createAdditionalJUnitData()
 	expectedOutputs := getExpectedOutputs()
 
-	jsonStdout := readOutput("json", formattedOutput, additionalJUnitData)
+	jsonStdout, _ := jsonOutput(&formattedOutput)
 	assert.Equal(t, expectedOutputs.json, jsonStdout)
 
-	yamlStdout := readOutput("yaml", formattedOutput, additionalJUnitData)
+	yamlStdout, _ := yamlOutput(&formattedOutput)
 	assert.Equal(t, expectedOutputs.yaml, yamlStdout)
 
-	xmlStdout := readOutput("xml", formattedOutput, additionalJUnitData)
+	xmlStdout, _ := xmlOutput(&formattedOutput)
 	assert.Equal(t, expectedOutputs.xml, xmlStdout)
 
-	JUnitStdout := readOutput("JUnit", formattedOutput, additionalJUnitData)
+	JUnitStdout, _ := jUnitOutput(&formattedOutput, additionalJUnitData)
 	assert.Equal(t, expectedOutputs.JUnit, JUnitStdout)
 }
 
@@ -132,75 +129,10 @@ func TestInvalidK8sCustomOutputs(t *testing.T) {
 	additionalJUnitData := createAdditionalJUnitDataInvalidK8sFile()
 	expectedOutputs := getInvalidK8sFileExpectedOutputs()
 
-	JUnitStdout := readInvalidK8sFileOutput("JUnit", formattedOutput, additionalJUnitData)
+	JUnitStdout, _ := jUnitOutput(&formattedOutput, additionalJUnitData)
 	assert.Equal(t, expectedOutputs.JUnit, JUnitStdout)
 }
 
-func readOutput(outputFormat string, formattedOutput FormattedOutput, additionalJUnitData AdditionalJUnitData) string {
-	reader, writer, err := os.Pipe()
-	if err != nil {
-		panic(err)
-	}
-	os.Stdout = writer
-	os.Stderr = writer
-	log.SetOutput(writer)
-
-	out := make(chan string)
-	go func() {
-		var buf bytes.Buffer
-		io.Copy(&buf, reader)
-		out <- buf.String()
-	}()
-
-	switch {
-	case outputFormat == "json":
-		jsonOutput(&formattedOutput)
-	case outputFormat == "yaml":
-		yamlOutput(&formattedOutput)
-	case outputFormat == "xml":
-		xmlOutput(&formattedOutput)
-	case outputFormat == "JUnit":
-		err := jUnitOutput(&formattedOutput, additionalJUnitData)
-		if err != nil {
-			panic("unexpected error in printer_test: " + err.Error())
-		}
-	}
-
-	writer.Close()
-	return <-out
-}
-
-func readInvalidK8sFileOutput(outputFormat string, formattedOutput FormattedOutput, additionalJUnitData AdditionalJUnitData) string {
-	reader, writer, err := os.Pipe()
-	if err != nil {
-		panic(err)
-	}
-	os.Stdout = writer
-	os.Stderr = writer
-	log.SetOutput(writer)
-
-	out := make(chan string)
-	go func() {
-		var buf bytes.Buffer
-		_, err := io.Copy(&buf, reader)
-		if err != nil {
-			panic(err)
-		}
-		out <- buf.String()
-	}()
-
-	switch {
-	case outputFormat == "JUnit":
-		err := jUnitOutput(&formattedOutput, additionalJUnitData)
-		if err != nil {
-			panic("unexpected error in printer_test: " + err.Error())
-		}
-	}
-
-	writer.Close()
-	return <-out
-}
-
 func createAdditionalJUnitData() AdditionalJUnitData {
 	dr, err := defaultRules.GetDefaultRules()
 	if err != nil {