Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Don't init flags in init()
Browse files Browse the repository at this point in the history
An issue prevents unit tests to run if flags are initialized in an
init() function. More details there:
golang/go#31859.

A workaround is to init flags another way (here by calling a function
from main()).
  • Loading branch information
sebgl committed Nov 28, 2019
1 parent 053c9f0 commit 16e1a60
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type apiPackage struct {

func (v *apiPackage) identifier() string { return fmt.Sprintf("%s/%s", v.apiGroup, v.apiVersion) }

func init() {
func initFlags() {
klog.InitFlags(nil)
flag.Set("alsologtostderr", "true") // for klog
flag.Parse()
Expand Down Expand Up @@ -114,6 +114,8 @@ func resolveTemplateDir(dir string) error {
func main() {
defer klog.Flush()

initFlags()

f, err := os.Open(*flConfig)
if err != nil {
klog.Fatalf("failed to open config file: %+v", err)
Expand Down

0 comments on commit 16e1a60

Please sign in to comment.