Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Ensure the environment.InitFlags() is called to avoid confusion. #269

Closed
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ import (
// config as well as the parsing level and state flags.
var global environment.GlobalEnvironment

func init() {
// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// environment.InitFlags registers state and level filter flags.
environment.InitFlags(flag.CommandLine)
}

// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// We get a chance to parse flags to include the framework flags for the
// framework as well as any additional flags included in the integration.
flag.Parse()
Expand Down
9 changes: 7 additions & 2 deletions pkg/environment/magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package environment
import (
"context"
"errors"
"fmt"
"regexp"
"testing"
"time"
Expand All @@ -35,7 +34,13 @@ import (
)

func NewGlobalEnvironment(ctx context.Context) GlobalEnvironment {
fmt.Printf("level %s, state %s, feature %s\n\n", l, s, *f)
log := logging.FromContext(ctx)
if l.Valid() && s.Valid() {
log.Infof("Global environment settings: level %s, state %s, feature %#v", l, s, *f)
} else {
log.Fatal("Flags are not initialized properly. Ensure to call " +
"environment.InitFlags() func.")
}

return &MagicGlobalEnvironment{
c: ctx,
Expand Down
10 changes: 10 additions & 0 deletions pkg/feature/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func (l Levels) String() string {
return b.String()
}

func (l Levels) Valid() bool {
values := []Levels{Must, MustNot, Should, ShouldNot, May, All}
for _, value := range values {
if l == value {
return true
}
}
return false
}

cardil marked this conversation as resolved.
Show resolved Hide resolved
var LevelMapping = map[Levels]string{
Must: "MUST",
MustNot: "MUST NOT",
Expand Down
10 changes: 10 additions & 0 deletions pkg/feature/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (s States) String() string {
return b.String()
}

func (s States) Valid() bool {
values := []States{Alpha, Beta, Stable, Any}
for _, value := range values {
if s == value {
return true
}
}
return false
}

var StatesMapping = map[States]string{
Alpha: "Alpha",
Beta: "Beta",
Expand Down
8 changes: 3 additions & 5 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ import (
// config as well as the parsing level and state flags.
var global environment.GlobalEnvironment

func init() {
// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// environment.InitFlags registers state, level and feature filter flags.
environment.InitFlags(flag.CommandLine)
}

// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// We get a chance to parse flags to include the framework flags for the
// framework as well as any additional flags included in the integration.
flag.Parse()
Expand All @@ -53,7 +51,7 @@ func TestMain(m *testing.M) {
// testing framework for namespace management, and could be leveraged by
// features to pull Kubernetes clients or the test environment out of the
// context passed in the features.
ctx, startInformers := injection.EnableInjectionOrDie(nil, nil) //nolint
ctx, startInformers := injection.EnableInjectionOrDie(nil, nil) // nolint
startInformers()

// global is used to make instances of Environments, NewGlobalEnvironment
Expand Down
8 changes: 3 additions & 5 deletions test/example/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ import (
// config as well as the parsing level and state flags.
var global environment.GlobalEnvironment

func init() {
// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// environment.InitFlags registers state and level filter flags.
environment.InitFlags(flag.CommandLine)
Copy link
Contributor

@dprotaso dprotaso Jan 11, 2022

Choose a reason for hiding this comment

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

I'm surprised InitFlags isn't instanced - ie. we use local variable vs. package ones global.InitFlags

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on this? I do not follow...

Copy link
Contributor

@dprotaso dprotaso Jan 11, 2022

Choose a reason for hiding this comment

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

InitFlags is initializing package global variables in reconciler-test

When it could simply just modify values on the declared global environment in the example. At least reconciler-test vars aren't exposed publicly so maybe that's ok. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I agree such refactor would be beneficial. Mind opening an issue to track it?

}

// TestMain is the first entry point for `go test`.
func TestMain(m *testing.M) {
// We get a chance to parse flags to include the framework flags for the
// framework as well as any additional flags included in the integration.
flag.Parse()
Expand All @@ -53,7 +51,7 @@ func TestMain(m *testing.M) {
// testing framework for namespace management, and could be leveraged by
// features to pull Kubernetes clients or the test environment out of the
// context passed in the features.
ctx, startInformers := injection.EnableInjectionOrDie(nil, nil) //nolint
ctx, startInformers := injection.EnableInjectionOrDie(nil, nil) // nolint
startInformers()

// global is used to make instances of Environments, NewGlobalEnvironment
Expand Down