From 8ac23654064f8154e5fbd47869eb3368adc69b2e Mon Sep 17 00:00:00 2001 From: Donny Xia Date: Thu, 28 May 2020 12:34:58 -0700 Subject: [PATCH] Check replace in go.mod before releasing --- releasing/releasing/go.mod | 5 ++- releasing/releasing/go.sum | 11 +++++ releasing/releasing/main.go | 6 +++ releasing/releasing/modulemeta.go | 37 ++++++++++++++++- releasing/releasing/modulemeta_test.go | 57 ++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/releasing/releasing/go.mod b/releasing/releasing/go.mod index 7b7020029..3d5bb6e0b 100644 --- a/releasing/releasing/go.mod +++ b/releasing/releasing/go.mod @@ -2,4 +2,7 @@ module sigs.k8s.io/kustomize/releasing go 1.13 -require github.com/spf13/cobra v1.0.0 +require ( + github.com/spf13/cobra v1.0.0 + golang.org/x/mod v0.3.0 +) diff --git a/releasing/releasing/go.sum b/releasing/releasing/go.sum index 193d57c78..dd3cee8f6 100644 --- a/releasing/releasing/go.sum +++ b/releasing/releasing/go.sum @@ -103,29 +103,40 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= diff --git a/releasing/releasing/main.go b/releasing/releasing/main.go index 821fd946d..2459da61c 100644 --- a/releasing/releasing/main.go +++ b/releasing/releasing/main.go @@ -212,6 +212,12 @@ func releaseCmdImpl(args []string) error { mod.Tag(), ) + // Check is there replace statement in go.mod + err = mod.CheckModReplace() + if err != nil { + return err + } + // Run module tests output, err := mod.RunTest() if err != nil { diff --git a/releasing/releasing/modulemeta.go b/releasing/releasing/modulemeta.go index 4e90fb255..66fc5cfab 100644 --- a/releasing/releasing/modulemeta.go +++ b/releasing/releasing/modulemeta.go @@ -1,14 +1,20 @@ package main import ( + "fmt" + "io/ioutil" + "os" "os/exec" "path/filepath" + "strings" + + "golang.org/x/mod/modfile" ) type module struct { // Module name name string - // Module path. This path is only used for running test. + // Module path path string // Module version version moduleVersion @@ -23,6 +29,35 @@ func (m *module) UpdateVersion(tags string) error { return nil } +func (m *module) CheckModReplace() error { + goModPath := filepath.Join(m.path, m.name, "go.mod") + info, err := os.Stat(goModPath) + if os.IsNotExist(err) || info.IsDir() { + return nil + } + + goModContent, err := ioutil.ReadFile(goModPath) + if err != nil { + return err + } + return checkModReplace(goModPath, goModContent) +} + +func checkModReplace(path string, data []byte) error { + f, err := modfile.Parse(path, data, nil) + if err != nil { + return err + } + if len(f.Replace) > 0 { + var msg strings.Builder + for _, replace := range f.Replace { + fmt.Fprintf(&msg, " - Please update go.mod to pin a specific version of %s\n", replace.Old.Path) + } + return fmt.Errorf("Found replace in %s\n%s", path, msg.String()) + } + return nil +} + func (m *module) Tag() string { return m.name + "/" + m.version.String() } diff --git a/releasing/releasing/modulemeta_test.go b/releasing/releasing/modulemeta_test.go index c8764ac4e..15754037e 100644 --- a/releasing/releasing/modulemeta_test.go +++ b/releasing/releasing/modulemeta_test.go @@ -66,3 +66,60 @@ func TestModuleTags(t *testing.T) { t.Errorf("Tag %s doesn't match expected %s", m.Tag(), expect) } } + +func TestCheckModReplace1(t *testing.T) { + path := "testpath" + dataString := `module sigs.k8s.io/kustomize/kustomize/v3 + + go 1.13 + + replace ( + sigs.k8s.io/kustomize/cmd/kubectl v0.0.3 => ../cmd/kubectl + sigs.k8s.io/kustomize/kstatus v0.0.1 => ../kstatus + )` + + expect := `Found replace in testpath + - Please update go.mod to pin a specific version of sigs.k8s.io/kustomize/cmd/kubectl + - Please update go.mod to pin a specific version of sigs.k8s.io/kustomize/kstatus +` + + err := checkModReplace(path, []byte(dataString)) + if err.Error() != expect { + t.Errorf("Error %s doesn't match expected %s", err.Error(), expect) + } +} + +func TestCheckModReplace2(t *testing.T) { + path := "testpath" + dataString := `module sigs.k8s.io/kustomize/kustomize/v3 + + go 1.13 + + replace sigs.k8s.io/kustomize/cmd/kubectl v0.0.3 => ../cmd/kubectl` + + expect := `Found replace in testpath + - Please update go.mod to pin a specific version of sigs.k8s.io/kustomize/cmd/kubectl +` + + err := checkModReplace(path, []byte(dataString)) + if err.Error() != expect { + t.Errorf("Error %s doesn't match expected %s", err.Error(), expect) + } +} + +func TestCheckModReplace3(t *testing.T) { + path := "testpath" + dataString := `module sigs.k8s.io/kustomize/kustomize/v3 + + go 1.13 + + exclude ( + github.com/russross/blackfriday v2.0.0+incompatible + sigs.k8s.io/kustomize/api v0.2.0 + )` + + err := checkModReplace(path, []byte(dataString)) + if err != nil { + t.Errorf("Error %s is not expected", err.Error()) + } +}