From 141988057e193a47600ae84cb37c4843a8bd0876 Mon Sep 17 00:00:00 2001 From: remib Date: Mon, 29 Sep 2025 17:09:37 +0200 Subject: [PATCH 1/2] Fix survey displayed in autocomplete --- completion_test.go | 28 ++++++++++++++++++++++------ main.go | 29 ++++++++++++++++++++++++++++- main_test.go | 8 ++++++++ plugins_test.go | 7 ++++--- utils/tests/utils.go | 10 ++++++++-- 5 files changed, 70 insertions(+), 12 deletions(-) diff --git a/completion_test.go b/completion_test.go index 040e9125d..17d1c4ca3 100644 --- a/completion_test.go +++ b/completion_test.go @@ -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)) +} diff --git a/main.go b/main.go index ddb58c509..d5d2750bf 100644 --- a/main.go +++ b/main.go @@ -89,6 +89,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) @@ -137,12 +152,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() { diff --git a/main_test.go b/main_test.go index ba4a3c52b..1cacf19ba 100644 --- a/main_test.go +++ b/main_test.go @@ -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" @@ -383,6 +384,13 @@ func TestIntro(t *testing.T) { assert.Contains(t, buffer.String(), "Thank you for installing version") } +func TestSurvey(t *testing.T) { + 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) diff --git a/plugins_test.go b/plugins_test.go index 8b477bfe4..e7aac844c 100644 --- a/plugins_test.go +++ b/plugins_test.go @@ -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" @@ -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 } @@ -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 } diff --git a/utils/tests/utils.go b/utils/tests/utils.go index 30b6adf10..2c44a3962 100644 --- a/utils/tests/utils.go +++ b/utils/tests/utils.go @@ -645,13 +645,18 @@ 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())) }() @@ -659,7 +664,8 @@ func GetCmdOutput(t *testing.T, jfrogCli *coreTests.JfrogCli, cmd ...string) ([] 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) { From 4739c74a8c77daec2fc91a14730d566c0822c8d9 Mon Sep 17 00:00:00 2001 From: remib Date: Thu, 9 Oct 2025 16:54:12 +0200 Subject: [PATCH 2/2] Fix test --- main_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main_test.go b/main_test.go index 1cacf19ba..1e21395a6 100644 --- a/main_test.go +++ b/main_test.go @@ -385,6 +385,7 @@ func TestIntro(t *testing.T) { } 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)