Skip to content

Commit

Permalink
Add documentation about passing custom flags
Browse files Browse the repository at this point in the history
  • Loading branch information
maruina committed Feb 16, 2023
1 parent db4e9fe commit 2d86c18
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/custom_flags/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Using custom flags

You can pass additional custom flags to the CLI
using the `flag` package and defining the custom flags before calling `envconf.NewFromFlags()`.

For example:

```console
go test -v ./... -args --my-custom-flag hello
=== RUN TestWithCustomFlag
=== RUN TestWithCustomFlag/feature
=== RUN TestWithCustomFlag/feature/custom_flag
custom_flags_test.go:52: Custom flag my-custom-flag: hello
--- PASS: TestWithCustomFlag (0.00s)
--- PASS: TestWithCustomFlag/feature (0.00s)
--- PASS: TestWithCustomFlag/feature/custom_flag (0.00s)
PASS
ok sigs.k8s.io/e2e-framework/examples/custom_flags 0.491s
```

or by compiling the test code

```console
go test -c -o custom_flags.test .
./custom_flags.test --help 2>&1 | grep -A2 my-custom
-my-custom-flag string
my custom flag for my tests
-namespace string
```
56 changes: 56 additions & 0 deletions examples/custom_flags/custom_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package custom_flags

import (
"context"
"flag"
"os"
"testing"

log "k8s.io/klog/v2"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

var (
test env.Environment
myCustomFlag string
)

func TestMain(m *testing.M) {
flag.StringVar(&myCustomFlag, "my-custom-flag", "", "my custom flag for my tests")
// create config from flags (always in TestMain because it calls
// flag.Parse())
cfg, err := envconf.NewFromFlags()
if err != nil {
log.Fatalf("failed to build envconf from flags: %s", err)
}
test = env.NewWithConfig(cfg)
os.Exit(test.Run(m))
}

func TestWithCustomFlag(t *testing.T) {
f := features.New("feature")
f.Assess("custom flag", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
t.Logf("Custom flag my-custom-flag: %v", myCustomFlag)
return ctx
})
test.Test(t, f.Feature())
}

0 comments on commit 2d86c18

Please sign in to comment.