Skip to content
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
28 changes: 22 additions & 6 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,39 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBashCompletion(t *testing.T) {
func TestBashCompletionSetup(t *testing.T) {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, err := tests.GetCmdOutput(t, jfrogCli, "completion", "bash")
content, _, err := tests.GetCmdOutput(t, jfrogCli, "completion", "bash")
assert.NoError(t, err)
assert.Equal(t, bash.BashAutocomplete, string(content))
}

func TestZshCompletion(t *testing.T) {
func TestZshCompletionSetup(t *testing.T) {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, err := tests.GetCmdOutput(t, jfrogCli, "completion", "zsh")
content, _, err := tests.GetCmdOutput(t, jfrogCli, "completion", "zsh")
assert.NoError(t, err)
assert.Equal(t, zsh.ZshAutocomplete, string(content))
}

func TestFishCompletion(t *testing.T) {
func TestFishCompletionSetup(t *testing.T) {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, err := tests.GetCmdOutput(t, jfrogCli, "completion", "fish")
content, _, err := tests.GetCmdOutput(t, jfrogCli, "completion", "fish")
assert.NoError(t, err)
assert.Contains(t, string(content), "complete -c")
}

func TestBashCompletionRt(t *testing.T) {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, errContent, err := tests.GetCmdOutput(t, jfrogCli, "rt", "--generate-bash-completion")
assert.NoError(t, err)
assert.Contains(t, string(content), "permission-target-create")
assert.Empty(t, string(errContent))
}

func TestBashCompletionJf(t *testing.T) {
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
content, errContent, err := tests.GetCmdOutput(t, jfrogCli, "", "--generate-bash-completion")
assert.NoError(t, err)
assert.Contains(t, string(content), "rt")
assert.Empty(t, string(errContent))
}
29 changes: 28 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func execMain() error {
cliutils.SetCliExecutableName(args[0])
app.EnableBashCompletion = true
commands, err := getCommands()
autoCompleteCalled := false
trackAutocompleteFunc := func() {
autoCompleteCalled = true
}
bashCompleteFunc := app.BashComplete
app.BashComplete = func(c *cli.Context) {
trackAutocompleteFunc()
bashCompleteFunc(c)
}
for i, command := range commands {
commands[i] = wrapAutoComplete(command, trackAutocompleteFunc)
for j := range command.Subcommands {
commands[i].Subcommands[j] = wrapAutoComplete(commands[i].Subcommands[j], trackAutocompleteFunc)
}
}
if err != nil {
clientlog.Error(err)
os.Exit(1)
Expand Down Expand Up @@ -138,12 +153,24 @@ func execMain() error {
err = app.Run(args)
logTraceIdOnFailure(err)

if err == nil {
if err == nil && !autoCompleteCalled {
displaySurveyLinkIfNeeded()
}
return err
}

func wrapAutoComplete(cmd cli.Command, callback func()) cli.Command {
if cmd.BashComplete == nil {
cmd.BashComplete = cli.DefaultAppComplete
}
originalBashComplete := cmd.BashComplete
cmd.BashComplete = func(c *cli.Context) {
callback()
originalBashComplete(c)
}
return cmd
}

// displaySurveyLinkIfNeeded checks if the survey should be hidden based on the JFROG_CLI_HIDE_SURVEY environment variable
func displaySurveyLinkIfNeeded() {
if cliutils.ShouldHideSurveyLink() {
Expand Down
9 changes: 9 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

artifactoryCLI "github.com/jfrog/jfrog-cli-artifactory/cli"
"github.com/jfrog/jfrog-cli/artifactory"
"github.com/stretchr/testify/require"

buildinfo "github.com/jfrog/build-info-go/entities"
commandUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
Expand Down Expand Up @@ -383,6 +384,14 @@ func TestIntro(t *testing.T) {
assert.Contains(t, buffer.String(), "Thank you for installing version")
}

func TestSurvey(t *testing.T) {
t.Setenv("CI", "false")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
_, contentErr, err := tests.GetCmdOutput(t, jfrogCli, "intro")
require.NoError(t, err)
assert.Contains(t, string(contentErr), "https://") // not doing more check as url can change
}

func TestGenerateAndLogTraceIdToken(t *testing.T) {
traceIdToken, err := generateTraceIdToken()
assert.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions plugins_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
biutils "github.com/jfrog/build-info-go/utils"
"os"
"path"
"path/filepath"
"strings"
"testing"

biutils "github.com/jfrog/build-info-go/utils"

clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"

"github.com/buger/jsonparser"
Expand Down Expand Up @@ -141,7 +142,7 @@ func installAndAssertPlugin(t *testing.T, jfrogCli *coreTests.JfrogCli, pluginVe

func verifyPluginSignature(t *testing.T, jfrogCli *coreTests.JfrogCli) error {
// Get signature from plugin.
content, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, plugins.SignatureCommandName)
content, _, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, plugins.SignatureCommandName)
if err != nil {
return err
}
Expand All @@ -166,7 +167,7 @@ func verifyPluginSignature(t *testing.T, jfrogCli *coreTests.JfrogCli) error {

func verifyPluginVersion(t *testing.T, jfrogCli *coreTests.JfrogCli, expectedVersion string) error {
// Run plugin's -v command.
content, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, "-v")
content, _, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, "-v")
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions utils/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,21 +645,27 @@ func RedirectLogOutputToNil() (previousLog log.Log) {
// Redirect output to a file, execute the command and read output.
// The reason for redirecting to a file and not to a buffer is the limited
// size of the buffer while using os.Pipe.
func GetCmdOutput(t *testing.T, jfrogCli *coreTests.JfrogCli, cmd ...string) ([]byte, error) {
func GetCmdOutput(t *testing.T, jfrogCli *coreTests.JfrogCli, cmd ...string) ([]byte, []byte, error) {
oldStdout := os.Stdout
oldStdErr := os.Stderr
temp, err := os.CreateTemp("", "output")
assert.NoError(t, err)
tempErr, err := os.CreateTemp("", "output")
assert.NoError(t, err)
os.Stdout = temp
os.Stderr = tempErr
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStdErr
assert.NoError(t, temp.Close())
assert.NoError(t, os.Remove(temp.Name()))
}()
err = jfrogCli.Exec(cmd...)
assert.NoError(t, err)
content, err := os.ReadFile(temp.Name())
assert.NoError(t, err)
return content, err
errContent, err := os.ReadFile(tempErr.Name())
return content, errContent, err
}

func VerifySha256DetailedSummaryFromBuffer(t *testing.T, buffer *bytes.Buffer, logger log.Log) {
Expand Down
Loading