From 237f07ac5f3be6bc876172f16003dddd5dd2fde1 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:36:36 -0500 Subject: [PATCH] fix: Preventing log enrichment for bare mode (#3916) * feat: Adding failing test * fix: flag destination * fix: unit tests --------- Co-authored-by: Levko Burburas --- internal/cli/bool_flag.go | 4 ++++ internal/cli/bool_flag_test.go | 6 ++++-- internal/cli/generic_flag.go | 4 ++++ internal/cli/generic_flag_test.go | 6 ++++-- test/integration_test.go | 14 ++++++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/cli/bool_flag.go b/internal/cli/bool_flag.go index fc08133e6..388f0f06d 100644 --- a/internal/cli/bool_flag.go +++ b/internal/cli/bool_flag.go @@ -44,6 +44,10 @@ func (flag *BoolFlag) Apply(set *libflag.FlagSet) error { return ApplyFlag(flag, set) } + if flag.Destination == nil { + flag.Destination = new(bool) + } + valueType := newBoolVar(flag.Destination, flag.Negative) value := newGenericValue(valueType, flag.Setter) diff --git a/internal/cli/bool_flag_test.go b/internal/cli/bool_flag_test.go index 94c12d4f3..6d2d65735 100644 --- a/internal/cli/bool_flag_test.go +++ b/internal/cli/bool_flag_test.go @@ -122,10 +122,12 @@ func testBoolFlagApply(t *testing.T, flag *cli.BoolFlag, args []string, envs map expectedDefaultValue string ) - if val := flag.Destination; val != nil { - expectedDefaultValue = strconv.FormatBool(*val) + if flag.Destination == nil { + flag.Destination = new(bool) } + expectedDefaultValue = strconv.FormatBool(*flag.Destination) + flag.LookupEnvFunc = func(key string) []string { if envs == nil { return nil diff --git a/internal/cli/generic_flag.go b/internal/cli/generic_flag.go index f0b65b0ac..dc11c8162 100644 --- a/internal/cli/generic_flag.go +++ b/internal/cli/generic_flag.go @@ -46,6 +46,10 @@ func (flag *GenericFlag[T]) Apply(set *libflag.FlagSet) error { return ApplyFlag(flag, set) } + if flag.Destination == nil { + flag.Destination = new(T) + } + valueType := &genericVar[T]{dest: flag.Destination} value := newGenericValue(valueType, flag.Setter) diff --git a/internal/cli/generic_flag_test.go b/internal/cli/generic_flag_test.go index 8e2cf879b..52a8adc72 100644 --- a/internal/cli/generic_flag_test.go +++ b/internal/cli/generic_flag_test.go @@ -187,10 +187,12 @@ func testGenericFlagApply[T cli.GenericType](t *testing.T, flag *cli.GenericFlag expectedDefaultValue string ) - if val := flag.Destination; val != nil { - expectedDefaultValue = fmt.Sprintf("%v", *val) + if flag.Destination == nil { + flag.Destination = new(T) } + expectedDefaultValue = fmt.Sprintf("%v", *flag.Destination) + flag.LookupEnvFunc = func(key string) []string { if envs == nil { return nil diff --git a/test/integration_test.go b/test/integration_test.go index 155798d63..f3d50d06e 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -4112,3 +4112,17 @@ func TestLogStreaming(t *testing.T) { require.GreaterOrEqualf(t, secondTimestamp.Sub(firstTimestamp), 1*time.Second, "Second log entry for unit %s is not at least 1 second after the first log entry", unit) } } + +func TestLogFormatBare(t *testing.T) { + t.Parallel() + + tmpEnvPath := helpers.CopyEnvironment(t, testFixtureEmptyState) + helpers.CleanupTerraformFolder(t, tmpEnvPath) + testPath := util.JoinPath(tmpEnvPath, testFixtureEmptyState) + + stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt init --log-format=bare --no-color --non-interactive --working-dir "+testPath) + require.NoError(t, err) + + assert.Contains(t, stdout, "Initializing the backend...") + assert.NotContains(t, stdout, "STDO[0000] Initializing the backend...") +}