Skip to content
Open
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
54 changes: 54 additions & 0 deletions packages/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
package cmd

import (
"bytes"
"reflect"
"testing"

"github.com/Infisical/infisical-merge/packages/models"
"github.com/Infisical/infisical-merge/packages/telemetry"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func TestTelemetryFlagDisablesTelemetry(t *testing.T) {
flag := RootCmd.PersistentFlags().Lookup("telemetry")
originalFlagValue := flag.Value.String()
originalFlagChanged := flag.Changed
silentFlag := RootCmd.PersistentFlags().Lookup("silent")
originalSilentValue := silentFlag.Value.String()
originalSilentChanged := silentFlag.Changed
originalOut := RootCmd.OutOrStdout()
originalErr := RootCmd.ErrOrStderr()
originalTelemetry := Telemetry
originalAPIKey := telemetry.POSTHOG_API_KEY_FOR_CLI
originalLogger := log.Logger
originalLogLevel := zerolog.GlobalLevel()
testCommand := &cobra.Command{Use: "telemetry-regression-test", Run: func(*cobra.Command, []string) {}}
RootCmd.AddCommand(testCommand)
t.Cleanup(func() {
RootCmd.RemoveCommand(testCommand)
_ = flag.Value.Set(originalFlagValue)
flag.Changed = originalFlagChanged
_ = silentFlag.Value.Set(originalSilentValue)
silentFlag.Changed = originalSilentChanged
RootCmd.SetArgs(nil)
RootCmd.SetOut(originalOut)
RootCmd.SetErr(originalErr)
Telemetry = originalTelemetry
telemetry.POSTHOG_API_KEY_FOR_CLI = originalAPIKey
log.Logger = originalLogger
zerolog.SetGlobalLevel(originalLogLevel)
})

telemetry.POSTHOG_API_KEY_FOR_CLI = "test-api-key"
Telemetry = telemetry.NewTelemetry(true)
telemetry.POSTHOG_API_KEY_FOR_CLI = ""
RootCmd.SetOut(&bytes.Buffer{})
RootCmd.SetErr(&bytes.Buffer{})
RootCmd.SetArgs([]string{"--telemetry=false", "--silent", testCommand.Name()})

if _, err := RootCmd.ExecuteC(); err != nil {
t.Fatalf("execute root command: %v", err)
}
if Telemetry == nil {
t.Fatal("telemetry was not initialized")
}
if reflect.ValueOf(Telemetry).Elem().FieldByName("isEnabled").Bool() {
t.Fatal("telemetry remained enabled after --telemetry=false")
}
}

func TestFilterReservedEnvVars(t *testing.T) {

// some test env vars.
Expand Down
9 changes: 7 additions & 2 deletions packages/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func topLevelCommandName(cmd *cobra.Command) string {
func init() {
util.GetStderrWriter = RootCmdStderrWriter
util.GetStdoutWriter = RootCmdStdoutWriter
cobra.OnInitialize(initLog, initLogOutput)
cobra.OnInitialize(initLog, initLogOutput, initTelemetry)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Opt-out lacks regression coverage

The fix has no automated test for the behavior it changes, so a later change could silently break the opt-out again. Add a test that executes a child command through Cobra with --telemetry=false and with no telemetry flag. Calling initTelemetry directly would miss the original flag-parsing bug.

Use a test key or an injected constructor: an empty POSTHOG_API_KEY_FOR_CLI makes NewTelemetry disable telemetry in both cases. This is a non-blocking recommendation.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added automated regression test for the opt-out flag

RootCmd.PersistentFlags().StringP("log-level", "l", "", "log level (trace, debug, info, warn, error, fatal)")
RootCmd.PersistentFlags().StringVar(&logFormat, "log-format", "", "log output format: console (default, colored), plain (no color), json (structured). Set NO_COLOR=1 to disable colors in console mode. Can also set via LOG_FORMAT env var.")
RootCmd.PersistentFlags().StringVar(&logDestination, "log-destination", "", "log output destination: stderr (default), stdout. Can also set via LOG_DESTINATION env var.")
Expand Down Expand Up @@ -211,8 +211,13 @@ func init() {
util.EnableProfileNotice(cmd.ErrOrStderr())
}
}
}

isTelemetryOn, _ := RootCmd.PersistentFlags().GetBool("telemetry")
func initTelemetry() {
isTelemetryOn, err := RootCmd.PersistentFlags().GetBool("telemetry")
if err != nil {
util.HandleError(err)
}
Telemetry = telemetry.NewTelemetry(isTelemetryOn)
}

Expand Down