diff --git a/go.mod b/go.mod index d6fb5c7..4eece65 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module kubemodcmp go 1.22.1 require ( + github.com/rogpeppe/go-internal v1.12.0 github.com/urfave/cli v1.22.14 golang.org/x/mod v0.17.0 ) @@ -10,4 +11,6 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/tools v0.13.0 // indirect ) diff --git a/go.sum b/go.sum index b3d9c1c..e4f76f6 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -19,6 +21,10 @@ github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk= github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 8be5e50..8f1f3b3 100644 --- a/main.go +++ b/main.go @@ -352,6 +352,8 @@ func checkCmd() cli.Command { if err = runGo(ctx.Args().First(), "mod", "edit", fmt.Sprintf("-go=%s", k8s.GoVersion)); err != nil { return fmt.Errorf("fixing Go version: %w", err) } + } else { + return fmt.Errorf("wrong Go version: %w", err) } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..e681ed9 --- /dev/null +++ b/main_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/rogpeppe/go-internal/testscript" +) + +func TestMain(m *testing.M) { + os.Exit(testscript.RunMain(m, map[string]func() int{ + "kubemodcmp": func() (exitCode int) { + defer func() { + if val := recover(); val != nil { + exitCode = recover().(int) + } + }() + main() + return 0 + }, + })) +} + +func TestScript(t *testing.T) { + testscript.Run(t, testscript.Params{ + Dir: "testdata", + TestWork: true, + Setup: func(env *testscript.Env) error { + path := os.Getenv("GOPATH") + if os.Getenv("GOPATH") == "" { + path = filepath.Join(env.WorkDir, ".go") + } + env.Setenv("GOPATH", path) + + if os.Getenv("GOCACHE") != "" { + env.Setenv("GOCACHE", os.Getenv("GOCACHE")) + } else { + env.Setenv("GOCACHE", filepath.Join(path, "cache")) + } + + // TODO: Use global cache instead of one per txtar + if os.Getenv("GOMODCACHE") != "" { + env.Setenv("GOMODCACHE", os.Getenv("GOMODCACHE")) + } else { + env.Setenv("GOMODCACHE", filepath.Join(path, "modcache")) + } + return nil + }, + }) +} diff --git a/testdata/check-dep-need-downgrade.txt b/testdata/check-dep-need-downgrade.txt new file mode 100644 index 0000000..f491fda --- /dev/null +++ b/testdata/check-dep-need-downgrade.txt @@ -0,0 +1,41 @@ +exec go mod tidy +! kubemodcmp check . +stderr 'golang.org/x/net' + +kubemodcmp check --fix . +kubemodcmp check . +! stderr . + +-- go.mod -- +module foo + +go 1.20 + +toolchain go1.22.2 + +require k8s.io/apimachinery v0.28.6 + +require ( + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) +-- main.go -- +package main + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +var _ any = metav1.TypeMeta{} + +func main() {} diff --git a/testdata/check-go-need-downgrade.txt b/testdata/check-go-need-downgrade.txt new file mode 100644 index 0000000..bb20e66 --- /dev/null +++ b/testdata/check-go-need-downgrade.txt @@ -0,0 +1,39 @@ +exec go mod tidy +! kubemodcmp check . +stderr . + +kubemodcmp check --fix . +kubemodcmp check . +! stderr . + +-- go.mod -- +module foo + +go 1.22 + +require k8s.io/apimachinery v0.28.6 + +require ( + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) +-- main.go -- +package main + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +var _ any = metav1.TypeMeta{} + +func main() {} diff --git a/testdata/check-go-need-upgrade.txt b/testdata/check-go-need-upgrade.txt new file mode 100644 index 0000000..06938a8 --- /dev/null +++ b/testdata/check-go-need-upgrade.txt @@ -0,0 +1,39 @@ +exec go mod tidy +! kubemodcmp check . +stderr . + +kubemodcmp check --fix . +kubemodcmp check . +! stderr . + +-- go.mod -- +module foo + +go 1.19 + +require k8s.io/apimachinery v0.28.6 + +require ( + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) +-- main.go -- +package main + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +var _ any = metav1.TypeMeta{} + +func main() {} diff --git a/testdata/check-ignore-absent-deps.txt b/testdata/check-ignore-absent-deps.txt new file mode 100644 index 0000000..65f5e5e --- /dev/null +++ b/testdata/check-ignore-absent-deps.txt @@ -0,0 +1,62 @@ +exec go mod tidy + +# golang.org/x/sys is not inside the go.mod file, but it ends up in the +# dependencies via `go list -m all`. Since it's not part of the go.mod file, +# the check command shouldn't trip on it. + +exec cat go.mod +! stdout `golang.org/x/sync` + +exec go list -m all +# That version of kubernetes imports golang.org/x/sync v0.5.0, but +stdout 'golang.org/x/sync v0.2.0' + +kubemodcmp check . +! stderr . + +-- go.mod -- +module foo + +go 1.20 + +require ( + github.com/prometheus/client_golang v1.16.0 + k8s.io/apimachinery v0.28.6 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/prometheus/client_model v0.4.0 // indirect + github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/procfs v0.10.1 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) +-- main.go -- +package main + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +import "github.com/prometheus/client_golang/prometheus" + +var _ any = metav1.TypeMeta{} +var _ any = prometheus.DescribeByCollect + +func main() {} diff --git a/testdata/check-success.txt b/testdata/check-success.txt new file mode 100644 index 0000000..35729fd --- /dev/null +++ b/testdata/check-success.txt @@ -0,0 +1,34 @@ +exec go mod tidy +kubemodcmp check . + +-- go.mod -- +module foo + +go 1.20 + +require k8s.io/apimachinery v0.28.6 + +require ( + github.com/go-logr/logr v1.2.4 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + k8s.io/klog/v2 v2.100.1 // indirect + k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect +) +-- main.go -- +package main + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +var _ any = metav1.TypeMeta{} + +func main() {}