Skip to content

Commit

Permalink
feat: experiments tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Feb 8, 2025
1 parent c8ee10e commit c5d6ca2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 25 deletions.
22 changes: 12 additions & 10 deletions internal/experiments/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import (
"strings"
)

type ExperimentHasInvalidValueError struct {
x Experiment
type InvalidValueError struct {
Name string
AllowedValues []string
Value string
}

func (err ExperimentHasInvalidValueError) Error() string {
func (err InvalidValueError) Error() string {
return fmt.Sprintf(
"task: Experiment %q has an invalid value %q (allowed values: %s)",
err.x.Name,
err.x.Value,
strings.Join(err.x.AllowedValues, ", "),
err.Name,
err.Value,
strings.Join(err.AllowedValues, ", "),
)
}

type ExperimentInactiveError struct {
x Experiment
type InactiveError struct {
Name string
}

func (err ExperimentInactiveError) Error() string {
func (err InactiveError) Error() string {
return fmt.Sprintf(
"task: Experiment %q is inactive and cannot be enabled",
err.x.Name,
err.Name,
)
}
23 changes: 21 additions & 2 deletions internal/experiments/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ type Experiment struct {
Value string // The version of the experiment that is enabled.
}

// New creates a new experiment with the given name and sets the values that can
// enable it.
func New(xName string, allowedValues ...string) Experiment {
value := getEnv(xName)
x := Experiment{
Name: xName,
AllowedValues: allowedValues,
Value: value,
}
xList = append(xList, x)
return x
}

func (x *Experiment) Enabled() bool {
return slices.Contains(x.AllowedValues, x.Value)
}
Expand All @@ -21,10 +34,16 @@ func (x *Experiment) Active() bool {

func (x Experiment) Valid() error {
if !x.Active() && x.Value != "" {
return ExperimentInactiveError{x}
return &InactiveError{
Name: x.Name,
}
}
if !x.Enabled() && x.Value != "" {
return ExperimentHasInvalidValueError{x}
return &InvalidValueError{
Name: x.Name,
AllowedValues: x.AllowedValues,
Value: x.Value,
}
}
return nil
}
Expand Down
74 changes: 74 additions & 0 deletions internal/experiments/experiment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package experiments_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/go-task/task/v3/internal/experiments"
)

func TestNew(t *testing.T) {
const (
exampleExperiment = "EXAMPLE"
exampleExperimentEnv = "TASK_X_EXAMPLE"
)
tests := []struct {
name string
allowedValues []string
value string
wantEnabled bool
wantActive bool
wantValid error
}{
{
name: `[] allowed, value=""`,
wantEnabled: false,
wantActive: false,
},
{
name: `[] allowed, value="1"`,
value: "1",
wantEnabled: false,
wantActive: false,
wantValid: &experiments.InactiveError{
Name: exampleExperiment,
},
},
{
name: `[1] allowed, value=""`,
allowedValues: []string{"1"},
wantEnabled: false,
wantActive: true,
},
{
name: `[1] allowed, value="1"`,
allowedValues: []string{"1"},
value: "1",
wantEnabled: true,
wantActive: true,
},
{
name: `[1] allowed, value="2"`,
allowedValues: []string{"1"},
value: "2",
wantEnabled: false,
wantActive: true,
wantValid: &experiments.InvalidValueError{
Name: exampleExperiment,
AllowedValues: []string{"1"},
Value: "2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(exampleExperimentEnv, tt.value)
x := experiments.New(exampleExperiment, tt.allowedValues...)
assert.Equal(t, exampleExperiment, x.Name)
assert.Equal(t, tt.wantEnabled, x.Enabled())
assert.Equal(t, tt.wantActive, x.Active())
assert.Equal(t, tt.wantValid, x.Valid())
})
}
}
13 changes: 0 additions & 13 deletions internal/experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ func init() {
EnvPrecedence = New("ENV_PRECEDENCE", "1")
}

// New creates a new experiment with the given name and sets the values that can
// enable it.
func New(xName string, allowedValues ...string) Experiment {
value := getEnv(xName)
x := Experiment{
Name: xName,
AllowedValues: allowedValues,
Value: value,
}
xList = append(xList, x)
return x
}

// Validate checks if any experiments have been enabled while being inactive.
// If one is found, the function returns an error.
func Validate() error {
Expand Down

0 comments on commit c5d6ca2

Please sign in to comment.