From c701ee91714371daa1c63f476e5d74f06391a7b4 Mon Sep 17 00:00:00 2001 From: "lucas.bruno" Date: Thu, 11 Nov 2021 10:48:35 -0300 Subject: [PATCH] e2e: test for the output format and json outuput file flags Signed-off-by: lucas.bruno --- e2e/commands/start/start_test.go | 78 +++++++++++++++---- .../printresults/print_results_test.go | 3 +- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/e2e/commands/start/start_test.go b/e2e/commands/start/start_test.go index 06139e762..1abcc6f4f 100644 --- a/e2e/commands/start/start_test.go +++ b/e2e/commands/start/start_test.go @@ -16,15 +16,16 @@ package start_test import ( "fmt" + "github.com/ZupIT/horusec-devkit/pkg/utils/logger/enums" "os" + "github.com/ZupIT/horusec/internal/enums/outputtype" + "github.com/google/uuid" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/gexec" - "github.com/ZupIT/horusec-devkit/pkg/utils/logger/enums" - "path/filepath" "github.com/ZupIT/horusec/internal/utils/testutil" @@ -32,22 +33,18 @@ import ( var _ = Describe("running binary Horusec with start parameter", func() { var ( - session *gexec.Session - err error - flags map[string]string - repoAuthorization string - configFilePath = testutil.GoExample1 + session *gexec.Session + flags map[string]string + configFilePath = testutil.GoExample1 ) JustBeforeEach(func() { + var err error cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.CmdStart, flags) session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter) + Expect(err).NotTo(HaveOccurred()) session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder) - - By("runs the command without errors", func() { - Expect(err).NotTo(HaveOccurred()) - Expect(session).Should(gexec.Exit(0)) - }) + Expect(session).Should(gexec.Exit(0)) }) When("global flag --log-level is passed", func() { @@ -120,8 +117,9 @@ var _ = Describe("running binary Horusec with start parameter", func() { }) When("--authorization is passed", func() { + repoAuthorization := uuid.New().String() + BeforeEach(func() { - repoAuthorization = uuid.New().String() flags = map[string]string{ testutil.StartFlagProjectPath: configFilePath, testutil.StartFlagAuthorization: repoAuthorization, @@ -132,4 +130,58 @@ var _ = Describe("running binary Horusec with start parameter", func() { Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"repository_authorization\": \"%s\"`, testutil.NormalizePathToAssertInJSON(repoAuthorization)))) }) }) + + When("--output-format and --json-output-file is passed as JSON", func() { + jsonOutputPath := filepath.Join(os.TempDir(), fmt.Sprintf("%s-e2e-output.json", uuid.New())) + + BeforeEach(func() { + flags = map[string]string{ + testutil.StartFlagProjectPath: configFilePath, + testutil.StartFlagOutputFormat: outputtype.JSON, + testutil.StartFlagJSONOutputFilePath: jsonOutputPath, + } + }) + + It("Checks if format was set as JSON and the file is created", func() { + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"print_output_type\": \"%s\"`, outputtype.JSON))) + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"json_output_file_path\": \"%s\"`, testutil.NormalizePathToAssertInJSON(jsonOutputPath)))) + Expect(jsonOutputPath).Should(BeAnExistingFile()) + }) + }) + + When("--output-format and --json-output-file is passed as Text", func() { + textOutputPath := filepath.Join(os.TempDir(), fmt.Sprintf("%s-e2e-output.txt", uuid.New())) + + BeforeEach(func() { + flags = map[string]string{ + testutil.StartFlagProjectPath: configFilePath, + testutil.StartFlagOutputFormat: outputtype.Text, + testutil.StartFlagJSONOutputFilePath: textOutputPath, + } + }) + + It("Checks if format was set as text and the file is created", func() { + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"print_output_type\": \"%s\"`, outputtype.Text))) + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"json_output_file_path\": \"%s\"`, testutil.NormalizePathToAssertInJSON(textOutputPath)))) + Expect(textOutputPath).Should(BeAnExistingFile()) + }) + }) + + When("--output-format is passed as sonarqube with --json-output-file as JSON", func() { + sonarqubeOutputPath := filepath.Join(os.TempDir(), fmt.Sprintf("%s-e2e-output-sonarqube.json", uuid.New())) + + BeforeEach(func() { + flags = map[string]string{ + testutil.StartFlagProjectPath: configFilePath, + testutil.StartFlagOutputFormat: outputtype.SonarQube, + testutil.StartFlagJSONOutputFilePath: sonarqubeOutputPath, + } + }) + + It("Checks if format was set as sonarqube and the JSON file is created", func() { + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"print_output_type\": \"%s\"`, outputtype.SonarQube))) + Expect(session.Out.Contents()).To(ContainSubstring(fmt.Sprintf(`\"json_output_file_path\": \"%s\"`, testutil.NormalizePathToAssertInJSON(sonarqubeOutputPath)))) + Expect(sonarqubeOutputPath).Should(BeAnExistingFile()) + }) + }) }) diff --git a/internal/controllers/printresults/print_results_test.go b/internal/controllers/printresults/print_results_test.go index 20b9510aa..3d538a971 100644 --- a/internal/controllers/printresults/print_results_test.go +++ b/internal/controllers/printresults/print_results_test.go @@ -21,6 +21,8 @@ import ( "strings" "testing" + "github.com/google/uuid" + "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" entitiesAnalysis "github.com/ZupIT/horusec-devkit/pkg/entities/analysis" "github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability" @@ -33,7 +35,6 @@ import ( "github.com/ZupIT/horusec/internal/enums/outputtype" "github.com/ZupIT/horusec/internal/helpers/messages" "github.com/ZupIT/horusec/internal/utils/mock" - "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"