Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testutils: centralization of flags an commands for tests #731

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ horusec-analysis-*.json
cmd/horusec/start/examples/
vendor
obj/
/dist
/dist
108 changes: 24 additions & 84 deletions cmd/app/start/start_test.go

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions e2e/commands/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package generate_test

import (
"fmt"
"os"
"path/filepath"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"os"
"path/filepath"
"strings"

"github.com/ZupIT/horusec/internal/utils/testutil"
)
Expand All @@ -46,9 +47,9 @@ var _ = Describe("Run horusec CLI with generate argument", func() {

BeforeEach(func() {
flags = map[string]string{
"--config-file-path": configFilePath,
testutil.GlobalFlagConfigFilePath: configFilePath,
}
cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.GenerateCmd, flags)
cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.CmdGenerate, flags)
session, err = gexec.Start(cmd, outBuffer, errBuffer)
})

Expand Down
2 changes: 1 addition & 1 deletion e2e/commands/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ = Describe("Run horusec CLI with version argument", func() {
)

BeforeEach(func() {
cmd := testutil.GinkgoGetHorusecCmd(testutil.VersionCmd)
cmd := testutil.GinkgoGetHorusecCmd(testutil.CmdVersion)
outBuffer = gbytes.NewBuffer()
session, err = gexec.Start(cmd, outBuffer, outBuffer)
})
Expand Down
10 changes: 6 additions & 4 deletions e2e/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"path"

"github.com/ZupIT/horusec/internal/enums/outputtype"

"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -127,12 +129,12 @@ var _ = Describe("Scan vulnerabilities example folder", func() {
output := path.Join(os.TempDir(), fmt.Sprintf("horusec-analysis-%s.json", uuid.New().String()))

flags := map[string]string{
"-p": tt.target,
"-o": "json",
"-O": output,
testutil.StartFlagProjectPath: tt.target,
testutil.StartFlagOutputFormat: outputtype.JSON,
testutil.StartFlagJSONOutputFilePath: output,
}

cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.StartCmd, flags)
cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.CmdStart, flags)

stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
Expand Down
3 changes: 2 additions & 1 deletion internal/utils/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
"path/filepath"
"testing"

"github.com/ZupIT/horusec/internal/utils/testutil"
"github.com/stretchr/testify/assert"

"github.com/ZupIT/horusec/internal/utils/testutil"
)

func TestGetFilePathIntoBasePath(t *testing.T) {
Expand Down
20 changes: 4 additions & 16 deletions internal/utils/testutil/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,15 @@ import (
"github.com/onsi/ginkgo"
)

type HorusecCmd string

func (h HorusecCmd) String() string {
return string(h)
}

const (
VersionCmd HorusecCmd = "version"
StartCmd HorusecCmd = "start"
GenerateCmd HorusecCmd = "generate"
)

func GinkgoGetHorusecCmd(horusecCmd HorusecCmd) *exec.Cmd {
func GinkgoGetHorusecCmd(horusecCmd string) *exec.Cmd {
bin := ginkgoBuildHorusecBinary()
args := setLogLevelArgsToHorusecCmd(horusecCmd.String())
args := setLogLevelArgsToHorusecCmd(horusecCmd)
return exec.Command(bin, args...)
}

func GinkgoGetHorusecCmdWithFlags(cmdArg HorusecCmd, flags map[string]string) *exec.Cmd {
func GinkgoGetHorusecCmdWithFlags(cmdArg string, flags map[string]string) *exec.Cmd {
bin := ginkgoBuildHorusecBinary()
args := setLogLevelArgsToHorusecCmd(cmdArg.String())
args := setLogLevelArgsToHorusecCmd(cmdArg)
for flag, value := range flags {
args = append(args, fmt.Sprintf("%s=%s", flag, value))
}
Expand Down
74 changes: 74 additions & 0 deletions internal/utils/testutil/cli_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testutil

// Available Commands
const (
CmdVersion = "version"
CmdStart = "start"
CmdGenerate = "generate"
)

// Available Global Flags
const (
GlobalFlagConfigFilePath = "--config-file-path"
GlobalFlagHelp = "--help"
GlobalFlagLogFilePath = "--log-file-path"
GlobalFlagLogLevel = "--log-level"
)

// Available Flags for start command
const (
StartFlagAnalysisTimeout = "--analysis-timeout"
StartFlagAuthorization = "--authorization"
StartFlagCertificatePath = "--certificate-path"
StartFlagContainerBindProjectPath = "--container-bind-project-path"
StartFlagCustomRulesPath = "--custom-rules-path"
StartFlagDisableDocker = "--disable-docker"
StartFlagEnableCommitAuthor = "--enable-commit-author"
StartFlagEnableGitHistory = "--enable-git-history"
StartFlagEnableOwaspDependencyCheck = "--enable-owasp-dependency-check"
StartFlagEnableShellcheck = "--enable-shellcheck"
StartFlagFalsePositive = "--false-positive"
StartFlagHeaders = "--headers"
StartFlagHorusecURL = "--horusec-url"
StartFlagIgnore = "--ignore"
StartFlagIgnoreSeverity = "--ignore-severity"
StartFlagInformationSeverity = "--information-severity"
StartFlagInsecureSkipVerify = "--insecure-skip-verify"
StartFlagJSONOutputFilePath = "--json-output-file"
StartFlagMonitorRetryCount = "--monitor-retry-count"
StartFlagOutputFormat = "--output-format"
StartFlagProjectPath = "--project-path"
StartFlagRepositoryName = "--repository-name"
StartFlagRequestTimeout = "--request-timeout"
StartFlagReturnError = "--return-error"
StartFlagRiskAccept = "--risk-accept"
StartFlagShowVulnerabilitiesTypes = "--show-vulnerabilities-types"
)

func GetAllStartFlags() []string {
return []string{
StartFlagAnalysisTimeout, StartFlagAuthorization, StartFlagCertificatePath,
StartFlagContainerBindProjectPath, StartFlagCustomRulesPath, StartFlagDisableDocker,
StartFlagEnableCommitAuthor, StartFlagEnableGitHistory, StartFlagEnableOwaspDependencyCheck,
StartFlagEnableShellcheck, StartFlagFalsePositive, StartFlagHeaders,
StartFlagHorusecURL, StartFlagIgnore, StartFlagIgnoreSeverity,
StartFlagInformationSeverity, StartFlagInsecureSkipVerify, StartFlagJSONOutputFilePath,
StartFlagMonitorRetryCount, StartFlagOutputFormat, StartFlagProjectPath,
StartFlagRepositoryName, StartFlagRequestTimeout, StartFlagReturnError,
StartFlagRiskAccept, StartFlagShowVulnerabilitiesTypes,
}
}