From 057290f4d3de050c27605051934d405ba3e58b0c Mon Sep 17 00:00:00 2001 From: Ahmad ATWI Date: Wed, 14 Aug 2024 11:00:30 +0200 Subject: [PATCH] [#674] Transform flavor from string to enum - Implement a new Variant enum - Replace the usage of relaxed and btcr by the enum values --- src/cli/terminal_ui_test.go | 2 +- src/config/param_variant.go | 3 ++- src/config/tcr_config.go | 3 ++- src/config/tcr_config_test.go | 3 ++- src/engine/tcr.go | 9 ++++---- src/engine/tcr_test.go | 14 +++++------ src/engine/tcr_test_fake.go | 3 ++- src/params/params.go | 3 ++- src/params/params_test_data_builder.go | 5 ++-- src/variant/variant.go | 32 ++++++++++++++++++++++++++ 10 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 src/variant/variant.go diff --git a/src/cli/terminal_ui_test.go b/src/cli/terminal_ui_test.go index 2d933a51..d8bee609 100644 --- a/src/cli/terminal_ui_test.go +++ b/src/cli/terminal_ui_test.go @@ -512,7 +512,7 @@ func Test_show_session_info(t *testing.T) { asCyanTrace("Work Directory: fake") + asCyanTrace("Language=fake, Toolchain=fake") + asYellowTrace("VCS \"fake\" is unknown") + - asCyanTrace("Variant is nice") + asCyanTrace("Variant is relaxed") assert.Equal(t, expected, capturer.CaptureStdout(func() { term, _, _ := terminalSetup(*params.AParamSet()) diff --git a/src/config/param_variant.go b/src/config/param_variant.go index 5d320c67..abf5c6da 100644 --- a/src/config/param_variant.go +++ b/src/config/param_variant.go @@ -23,6 +23,7 @@ SOFTWARE. package config import ( + "github.com/murex/tcr/variant" "github.com/spf13/cobra" ) @@ -44,7 +45,7 @@ func AddVariantParam(cmd *cobra.Command) *StringParam { }, v: paramValueString{ value: "", - defaultValue: "relaxed", + defaultValue: string(variant.Relaxed), }, } param.addToCommand(cmd) diff --git a/src/config/tcr_config.go b/src/config/tcr_config.go index 185eb4d2..1ebc45b9 100644 --- a/src/config/tcr_config.go +++ b/src/config/tcr_config.go @@ -28,6 +28,7 @@ import ( "github.com/murex/tcr/settings" "github.com/murex/tcr/toolchain" "github.com/murex/tcr/utils" + "github.com/murex/tcr/variant" "github.com/spf13/cobra" "github.com/spf13/viper" "io" @@ -220,7 +221,7 @@ func UpdateEngineParams(p *params.Params) { p.Toolchain = Config.Toolchain.GetValue() p.PollingPeriod = Config.PollingPeriod.GetValue() p.AutoPush = Config.AutoPush.GetValue() - p.Variant = Config.Variant.GetValue() + p.Variant = variant.Variant(Config.Variant.GetValue()) p.CommitFailures = Config.CommitFailures.GetValue() p.VCS = Config.VCS.GetValue() p.MessageSuffix = Config.MessageSuffix.GetValue() diff --git a/src/config/tcr_config_test.go b/src/config/tcr_config_test.go index 8e3071ce..6d9cedbe 100644 --- a/src/config/tcr_config_test.go +++ b/src/config/tcr_config_test.go @@ -28,6 +28,7 @@ import ( "github.com/murex/tcr/params" "github.com/murex/tcr/toolchain" "github.com/murex/tcr/utils" + "github.com/murex/tcr/variant" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "os" @@ -244,7 +245,7 @@ func Test_show_tcr_config_with_default_values(t *testing.T) { fmt.Sprintf("%v.tcr.language: %v", prefix, ""), fmt.Sprintf("%v.tcr.toolchain: %v", prefix, ""), fmt.Sprintf("%v.tcr.trace: %v", prefix, "none"), - fmt.Sprintf("%v.tcr.variant: %v", prefix, "relaxed"), + fmt.Sprintf("%v.tcr.variant: %v", prefix, variant.Relaxed), fmt.Sprintf("%v.vcs.name: %v", prefix, "git"), } utils.AssertSimpleTrace(t, expected, diff --git a/src/engine/tcr.go b/src/engine/tcr.go index aecba66b..c442ebd4 100644 --- a/src/engine/tcr.go +++ b/src/engine/tcr.go @@ -40,6 +40,7 @@ import ( "github.com/murex/tcr/toolchain" "github.com/murex/tcr/toolchain/command" "github.com/murex/tcr/ui" + "github.com/murex/tcr/variant" "github.com/murex/tcr/vcs" "github.com/murex/tcr/vcs/factory" "gopkg.in/tomb.v2" @@ -58,7 +59,7 @@ type ( ToggleAutoPush() SetAutoPush(flag bool) SetCommitOnFail(flag bool) - SetVariant(variant string) + SetVariant(variant variant.Variant) GetCurrentRole() role.Role RunAsDriver() RunAsNavigator() @@ -93,7 +94,7 @@ type ( // before starting a new one roleMutex sync.Mutex commitOnFail bool - variant string + variant variant.Variant messageSuffix string // shoot channel is used for handling interruptions coming from the UI shoot chan bool @@ -108,7 +109,7 @@ type ( } ) -func (tcr *TCREngine) SetVariant(variant string) { +func (tcr *TCREngine) SetVariant(variant variant.Variant) { tcr.variant = variant } @@ -685,7 +686,7 @@ func (tcr *TCREngine) GetSessionInfo() SessionInfo { VCSSessionSummary: tcr.vcs.SessionSummary(), GitAutoPush: tcr.vcs.IsAutoPushEnabled(), CommitOnFail: tcr.commitOnFail, - Variant: tcr.variant, + Variant: string(tcr.variant), MessageSuffix: tcr.messageSuffix, } } diff --git a/src/engine/tcr_test.go b/src/engine/tcr_test.go index 795c101d..8462a366 100644 --- a/src/engine/tcr_test.go +++ b/src/engine/tcr_test.go @@ -35,6 +35,7 @@ import ( "github.com/murex/tcr/toolchain" "github.com/murex/tcr/toolchain/command" "github.com/murex/tcr/ui" + "github.com/murex/tcr/variant" "github.com/murex/tcr/vcs" "github.com/murex/tcr/vcs/factory" "github.com/murex/tcr/vcs/fake" @@ -469,17 +470,16 @@ func Test_set_commit_on_fail(t *testing.T) { func Test_set_variant(t *testing.T) { var tcr TCRInterface testFlags := []struct { - desc string - variant string + variant variant.Variant }{ - {"Nice Variant", "nice"}, - {"Original Variant", "original"}, + {variant.Relaxed}, + {variant.BTCR}, } for _, tt := range testFlags { - t.Run(tt.desc, func(t *testing.T) { + t.Run(fmt.Sprintf("Variant %v", tt.variant), func(t *testing.T) { tcr, _ = initTCREngineWithFakes(nil, nil, nil, nil) tcr.SetVariant(tt.variant) - assert.Equal(t, tt.variant, tcr.GetSessionInfo().Variant) + assert.Equal(t, string(tt.variant), tcr.GetSessionInfo().Variant) }) } } @@ -536,7 +536,7 @@ func Test_get_session_info(t *testing.T) { ToolchainName: "fake-toolchain", VCSName: fake.Name, VCSSessionSummary: "VCS session \"" + fake.Name + "\"", - Variant: "nice", + Variant: string(variant.Relaxed), GitAutoPush: false, } assert.Equal(t, expected, tcr.GetSessionInfo()) diff --git a/src/engine/tcr_test_fake.go b/src/engine/tcr_test_fake.go index d54cf275..2768819a 100644 --- a/src/engine/tcr_test_fake.go +++ b/src/engine/tcr_test_fake.go @@ -30,6 +30,7 @@ import ( "github.com/murex/tcr/status" "github.com/murex/tcr/timer" "github.com/murex/tcr/ui" + "github.com/murex/tcr/variant" ) // TCRCall is used to track calls to TCR operations @@ -79,7 +80,7 @@ func NewFakeTCREngine() *FakeTCREngine { VCSSessionSummary: "VCS session \"fake\"", GitAutoPush: false, CommitOnFail: false, - Variant: "nice", + Variant: string(variant.Relaxed), }, } } diff --git a/src/params/params.go b/src/params/params.go index 55b9f85d..16533e95 100644 --- a/src/params/params.go +++ b/src/params/params.go @@ -24,6 +24,7 @@ package params import ( "github.com/murex/tcr/runmode" + "github.com/murex/tcr/variant" "time" ) @@ -36,7 +37,7 @@ type Params struct { Toolchain string MobTurnDuration time.Duration AutoPush bool - Variant string + Variant variant.Variant CommitFailures bool PollingPeriod time.Duration Mode runmode.RunMode diff --git a/src/params/params_test_data_builder.go b/src/params/params_test_data_builder.go index 47c12709..862c20b9 100644 --- a/src/params/params_test_data_builder.go +++ b/src/params/params_test_data_builder.go @@ -26,6 +26,7 @@ package params import ( "github.com/murex/tcr/runmode" + "github.com/murex/tcr/variant" "time" ) @@ -39,7 +40,7 @@ func AParamSet(builders ...func(params *Params)) *Params { Toolchain: "", MobTurnDuration: 0, AutoPush: false, - Variant: "nice", + Variant: variant.Relaxed, PollingPeriod: 0, Mode: runmode.OneShot{}, VCS: "git", @@ -109,7 +110,7 @@ func WithAutoPush(value bool) func(params *Params) { } // WithVariant sets the provided value as the variant to be used -func WithVariant(variant string) func(params *Params) { +func WithVariant(variant variant.Variant) func(params *Params) { return func(params *Params) { params.Variant = variant } diff --git a/src/variant/variant.go b/src/variant/variant.go new file mode 100644 index 00000000..04e952ec --- /dev/null +++ b/src/variant/variant.go @@ -0,0 +1,32 @@ +/* +Copyright (c) 2024 Murex + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package variant + +// Variant represents the possible values for the TCR Variant +// https://medium.com/@tdeniffel/tcr-variants-test-commit-revert-bf6bd84b17d3 +type Variant string + +const ( + Relaxed Variant = "relaxed" + BTCR Variant = "btcr" +)