Skip to content

Commit

Permalink
Feature flag assertion validations
Browse files Browse the repository at this point in the history
  • Loading branch information
jtigger committed May 9, 2022
1 parent 4acf2b9 commit 2e928a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/ytt/ytt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func main() {
}

func enableExperimentalFeatures() {
if experiments, isSet := os.LookupEnv("YTTEXPERIMENTS"); isSet {
if experiments, isSet := os.LookupEnv("YTTEXPERIMENTS"); isSet && experiments != "" {
// if we didn't ignore empty string, we'd attempt to Enable(""); that would panic (rather than ignore).
for _, experiment := range strings.Split(experiments, ",") {
feature.Flags().Enable(experiment)
fmt.Fprintf(os.Stderr, "Experimental feature %q enabled.\n", experiment)
Expand Down
5 changes: 5 additions & 0 deletions pkg/assertions/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"

"github.com/k14s/starlark-go/starlark"
"github.com/vmware-tanzu/carvel-ytt/pkg/feature"
"github.com/vmware-tanzu/carvel-ytt/pkg/template"
"github.com/vmware-tanzu/carvel-ytt/pkg/yamlmeta"
)
Expand All @@ -23,9 +24,13 @@ const (
// When a Node's value is invalid, the errors are collected and returned in an AssertCheck.
// Otherwise, returns empty AssertCheck and nil error.
func ProcessAndRunValidations(n yamlmeta.Node, threadName string) (AssertCheck, error) {
if !feature.Flags().IsEnabled(feature.Validations) {
return AssertCheck{}, nil
}
if n == nil {
return AssertCheck{}, nil
}

err := yamlmeta.Walk(n, &convertAssertAnnsToValidations{})
if err != nil {
return AssertCheck{}, err
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/template/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
package template_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmdtpl "github.com/vmware-tanzu/carvel-ytt/pkg/cmd/template"
"github.com/vmware-tanzu/carvel-ytt/pkg/cmd/ui"
"github.com/vmware-tanzu/carvel-ytt/pkg/feature"
"github.com/vmware-tanzu/carvel-ytt/pkg/files"
)

// TestMain is invoked when any tests are run in this package, *instead of* those tests being run directly.
// This allows for setup to occur before *any* test is run.
func TestMain(m *testing.M) {
// Enable experimental code
feature.Flags().Enable(feature.Validations)

exitVal := m.Run() // execute the specified tests

os.Exit(exitVal) // required in order to properly report the error level when tests fail.
}

func TestLoad(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
Expand Down
5 changes: 3 additions & 2 deletions pkg/feature/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import "fmt"

// Names of features that can be toggled
const (
Noop = "noop"
Noop = "noop"
Validations = "validations"
)

// allFeatures is the total list of features. It must contain all the constants defined, above.
var allFeatures = []string{Noop}
var allFeatures = []string{Noop, Validations}

// Flags returns the singleton instance of feature flags.
func Flags() *Flagset {
Expand Down

0 comments on commit 2e928a0

Please sign in to comment.