Skip to content

Commit

Permalink
[#674] Transform flavor from string to enum
Browse files Browse the repository at this point in the history
- Implement a new Variant enum
- Replace the usage of relaxed and btcr by the enum values
  • Loading branch information
aatwi authored and mengdaming committed Oct 1, 2024
1 parent 685bcd2 commit 8e0370b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/cli/terminal_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion src/config/param_variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SOFTWARE.
package config

import (
"github.com/murex/tcr/variant"
"github.com/spf13/cobra"
)

Expand All @@ -44,7 +45,7 @@ func AddVariantParam(cmd *cobra.Command) *StringParam {
},
v: paramValueString{
value: "",
defaultValue: "relaxed",
defaultValue: string(variant.Relaxed),
},
}
param.addToCommand(cmd)
Expand Down
3 changes: 2 additions & 1 deletion src/config/tcr_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/config/tcr_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -58,7 +59,7 @@ type (
ToggleAutoPush()
SetAutoPush(flag bool)
SetCommitOnFail(flag bool)
SetVariant(variant string)
SetVariant(variant variant.Variant)
GetCurrentRole() role.Role
RunAsDriver()
RunAsNavigator()
Expand Down Expand Up @@ -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
Expand All @@ -108,7 +109,7 @@ type (
}
)

func (tcr *TCREngine) SetVariant(variant string) {
func (tcr *TCREngine) SetVariant(variant variant.Variant) {
tcr.variant = variant
}

Expand Down Expand Up @@ -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,
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion src/engine/tcr_test_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,7 +80,7 @@ func NewFakeTCREngine() *FakeTCREngine {
VCSSessionSummary: "VCS session \"fake\"",
GitAutoPush: false,
CommitOnFail: false,
Variant: "nice",
Variant: string(variant.Relaxed),
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package params

import (
"github.com/murex/tcr/runmode"
"github.com/murex/tcr/variant"
"time"
)

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/params/params_test_data_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package params

import (
"github.com/murex/tcr/runmode"
"github.com/murex/tcr/variant"
"time"
)

Expand All @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 32 additions & 0 deletions src/variant/variant.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 8e0370b

Please sign in to comment.