Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent b596957 commit 323501a
Show file tree
Hide file tree
Showing 35 changed files with 506 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package gen
package embed

import (
"github.com/spf13/pflag"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 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 embed_test

import (
"bytes"
"errors"
"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/deepcopy-gen/embed"
"testing"
)

func TestDeepCopy(t *testing.T) {
t.Parallel()
var out bytes.Buffer
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
fs.SetOutput(&out)
args := []string{"--help"}

err := embed.DeepCopy(fs, args)
if !errors.Is(err, pflag.ErrHelp) {
t.Errorf("unexpected error: %v", err)
}
if out.Len() == 0 {
t.Errorf("expected output, got none")
}
}
4 changes: 2 additions & 2 deletions staging/src/k8s.io/code-generator/cmd/deepcopy-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ package main

import (
"flag"
"k8s.io/code-generator/cmd/defaulter-gen/gen"
"k8s.io/code-generator/cmd/defaulter-gen/embed"
"os"

"github.com/spf13/pflag"
Expand All @@ -59,7 +59,7 @@ func main() {
flag.Set("logtostderr", "true")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

if err := gen.Defaulter(pflag.CommandLine, os.Args[1:]); err != nil {
if err := embed.Defaulter(pflag.CommandLine, os.Args[1:]); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package gen
package embed

import (
"github.com/spf13/pflag"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 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 embed_test

import (
"bytes"
"errors"
"github.com/spf13/pflag"
"k8s.io/code-generator/cmd/defaulter-gen/embed"
"testing"
)

func TestDefaulter(t *testing.T) {
t.Parallel()
var out bytes.Buffer
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
fs.SetOutput(&out)
args := []string{"--help"}

err := embed.Defaulter(fs, args)
if !errors.Is(err, pflag.ErrHelp) {
t.Errorf("unexpected error: %v", err)
}
if out.Len() == 0 {
t.Errorf("expected output, got none")
}
}
4 changes: 2 additions & 2 deletions staging/src/k8s.io/code-generator/cmd/defaulter-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ package main

import (
"flag"
"k8s.io/code-generator/cmd/defaulter-gen/gen"
"k8s.io/code-generator/cmd/defaulter-gen/embed"
"os"

"github.com/spf13/pflag"
Expand All @@ -55,7 +55,7 @@ func main() {
flag.Set("logtostderr", "true")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)

if err := gen.Defaulter(pflag.CommandLine, os.Args[1:]); err != nil {
if err := embed.Defaulter(pflag.CommandLine, os.Args[1:]); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/code-generator/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMainFn(t *testing.T) {
main.RunMain(func(ex *execution.Vars) {
ex.Args = []string{"--help"}
ex.Out = &out
ex.Err = &err
ex.Out = &err
ex.Exit = func(code int) {
retcode = &code
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Generator interface {
// Command is the command for generating client code.
type Command struct {
Gen Generator
*goflag.FlagSet
}

func (c Command) Matches(ex *execution.Vars) bool {
Expand All @@ -40,18 +41,18 @@ func (c Command) Matches(ex *execution.Vars) bool {
func (c Command) Run(ex *execution.Vars) {
args := &client.Args{}
fs := pflag.NewFlagSet(ex.Args[0], pflag.ContinueOnError)
fs.AddGoFlagSet(goflag.CommandLine) // make sure we get the klog flags
fs.AddGoFlagSet(c.flags()) // make sure we get the klog flags
defineFlags(fs, args)
if err := fs.Parse(ex.Args[1:]); err != nil {
ex.PrintErrln("Error parsing arguments:", err)
ex.PrintErrln()
ex.PrintErrln(c.Help())
ex.Println("Error parsing arguments:", err)
ex.Println()
ex.Println(c.Help())
ex.Exit(8)
return
}
gen := c.createOrGetGenerator()
if err := gen.Generate(args); err != nil {
ex.PrintErrln("Error generating clients:", err)
ex.Println("Error generating clients:", err)
ex.Exit(9)
return
}
Expand All @@ -63,3 +64,10 @@ func (c Command) createOrGetGenerator() Generator {
}
return c.Gen
}

func (c Command) flags() *goflag.FlagSet {
if c.FlagSet == nil {
return goflag.CommandLine
}
return c.FlagSet
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client_test

import (
"bytes"
goflag "flag"
"k8s.io/code-generator/internal/codegen/command/client"
"k8s.io/code-generator/internal/codegen/execution"
pkgclient "k8s.io/code-generator/pkg/codegen/client"
Expand All @@ -41,7 +42,10 @@ func TestCommandRun(t *testing.T) {
t.Parallel()

gen := &testGen{}
cmd := client.Command{Gen: gen}
cmd := client.Command{
Gen: gen,
FlagSet: goflag.NewFlagSet("test", goflag.ContinueOnError),
}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{
cmd.Name(),
Expand Down Expand Up @@ -71,10 +75,13 @@ func TestCommandRunInvalidArgs(t *testing.T) {
var errstream bytes.Buffer
retcode := math.MinInt32
gen := &testGen{}
cmd := client.Command{Gen: gen}
cmd := client.Command{
Gen: gen,
FlagSet: goflag.NewFlagSet("test", goflag.ContinueOnError),
}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{cmd.Name(), "--input-foo", "foo"}
v.Err = &errstream
v.Out = &errstream
v.Exit = func(code int) {
retcode = code
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func (c Command) Run(ex *execution.Vars) {
cmds := append([]command.Usage{c}, c.Others...)
for _, c := range cmds {
if c.Name() == cmdName {
ex.PrintErrln(c.Help())
ex.Println(c.Help())
return
}
}
i := InvalidCommand{}
i.Run(ex)
return
}
ex.PrintErrln(c.Help())
ex.Println(c.Help())
}

func stripHelp(args []string) []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestHelpCommand(t *testing.T) {
}
var err bytes.Buffer
c.Run(execution.New(func(v *execution.Vars) {
v.Err = &err
v.Out = &err
v.Args = []string{"help"}
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (i InvalidCommand) Matches(ex *execution.Vars) bool {
}

func (i InvalidCommand) Run(ex *execution.Vars) {
ex.PrintErrf("%s: %s\n", i.OneLine(), strings.Join(ex.Args, " "))
ex.PrintErrln()
ex.PrintErrln(i.Usage.Help())
ex.Printf("%s: %s\n", i.OneLine(), strings.Join(ex.Args, " "))
ex.Println()
ex.Println(i.Usage.Help())
ex.Exit(6)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestInvalidCommand(t *testing.T) {
retcode := math.MinInt32
v := execution.New(func(v *execution.Vars) {
v.Args = []string{"invalid-cmd"}
v.Err = &err
v.Out = &err
v.Exit = func(code int) {
retcode = code
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Generator interface {

type Command struct {
Gen Generator
*goflag.FlagSet
}

func (c Command) Matches(ex *execution.Vars) bool {
Expand All @@ -39,7 +40,7 @@ func (c Command) Matches(ex *execution.Vars) bool {
func (c Command) Run(ex *execution.Vars) {
args := &helpers.Args{}
fs := pflag.NewFlagSet(ex.Args[0], pflag.ContinueOnError)
fs.AddGoFlagSet(goflag.CommandLine) // make sure we get the klog flags
fs.AddGoFlagSet(c.flags()) // make sure we get the klog flags
defineFlags(fs, args)
if err := fs.Parse(ex.Args[1:]); err != nil {
c.printErrorWithUsage(ex, "Error parsing arguments:", err)
Expand All @@ -51,16 +52,16 @@ func (c Command) Run(ex *execution.Vars) {
}
gen := c.createOrGetGenerator()
if err := gen.Generate(args); err != nil {
ex.PrintErrln("Error generating helpers:", err)
ex.Println("Error generating helpers:", err)
ex.Exit(11)
return
}
}

func (c Command) printErrorWithUsage(ex *execution.Vars, i ...any) {
ex.PrintErrln(i...)
ex.PrintErrln()
ex.PrintErrln(c.Help())
ex.Println(i...)
ex.Println()
ex.Println(c.Help())
ex.Exit(10)
}

Expand All @@ -70,3 +71,10 @@ func (c Command) createOrGetGenerator() Generator {
}
return c.Gen
}

func (c Command) flags() *goflag.FlagSet {
if c.FlagSet == nil {
return goflag.CommandLine
}
return c.FlagSet
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package helpers_test

import (
"bytes"
goflag "flag"
"k8s.io/code-generator/internal/codegen/command/helpers"
"k8s.io/code-generator/internal/codegen/execution"
pkghelpers "k8s.io/code-generator/pkg/codegen/helpers"
"math"
"os"
"path"
"strings"
"testing"
)

Expand All @@ -41,13 +45,17 @@ func TestCommandRun(t *testing.T) {
t.Parallel()

gen := &testGen{}
cmd := helpers.Command{Gen: gen}
cmd := helpers.Command{
Gen: gen,
FlagSet: goflag.NewFlagSet("test", goflag.ContinueOnError),
}
ob := t.TempDir()
_ = os.MkdirAll(path.Join(ob, "foo"), 0x755)
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{
cmd.Name(),
"--input-pkg-root", "foo",
"--output-base", "bar",
"--boilerplate", "baz",
"--output-base", ob,
}
})
cmd.Run(ex)
Expand All @@ -56,13 +64,16 @@ func TestCommandRun(t *testing.T) {
}
call := gen.calls[0]
if call.InputPkgRoot != "foo" {
t.Errorf("expected input package to be foo, got %s", call.InputPkgRoot)
t.Errorf("expected input package to be foo, got %s",
call.InputPkgRoot)
}
if call.OutputBase != "bar" {
t.Errorf("expected output base to be bar, got %s", call.OutputBase)
if call.OutputBase != ob {
t.Errorf("expected output base to be %s, got %s",
ob, call.OutputBase)
}
if call.Boilerplate != "baz" {
t.Errorf("expected boilerplate to be baz, got %s", call.Boilerplate)
if !strings.Contains(call.Boilerplate, "boilerplate.go.txt") {
t.Errorf("expected boilerplate to point to boilerplate.go.txt, got %s",
call.Boilerplate)
}
}

Expand All @@ -71,10 +82,13 @@ func TestCommandRunInvalidArgs(t *testing.T) {
var errstream bytes.Buffer
retcode := math.MinInt32
gen := &testGen{}
cmd := helpers.Command{Gen: gen}
cmd := helpers.Command{
Gen: gen,
FlagSet: goflag.NewFlagSet("test", goflag.ContinueOnError),
}
ex := execution.New(func(v *execution.Vars) {
v.Args = []string{cmd.Name(), "--input-foo", "foo"}
v.Err = &errstream
v.Out = &errstream
v.Exit = func(code int) {
retcode = code
}
Expand Down

0 comments on commit 323501a

Please sign in to comment.