Skip to content

Commit

Permalink
Merge branch 'uber-go:main' into v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lverma14 authored Apr 2, 2024
2 parents c8ace71 + be15f28 commit 2604c6e
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x

- name: Load cached dependencies
uses: actions/cache@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: 1.22.x

- name: Checkout
uses: actions/checkout@v4
Expand Down
37 changes: 37 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,43 @@ func runIntegrationTest(t *testing.T, testFile string) {
}
}

func TestGeneratedE2e(t *testing.T) {
patch := "testdata/patch/time.patch"
t.Parallel()
tests := []struct {
name string
path string
want string
}{
{
name: "go special comment for generated code",
path: "testdata/test_files/skip_generated_files/special_notation_generated.go",
},
{
name: "@generated",
path: "testdata/test_files/skip_generated_files/simple_generated.go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
want, err := os.ReadFile(tt.path)
require.NoError(t, err)
var stdoutbuf bytes.Buffer
cmd := mainCmd{
Stdout: &stdoutbuf,
Getwd: os.Getwd,
}
err = cmd.Run([]string{"--skip-generated", "-p", patch, tt.path})
require.NoError(t, err, "could not run patch")
assert.Empty(t, stdoutbuf)
// verify the target file's content didn't modify.
got, err := os.ReadFile(tt.path)
require.NoError(t, err)
assert.Equal(t, want, got)
})
}
}

// includeSkipImportTest returns whether we should include a testfile for
// the skipImportProcessing flag testing
func includeSkipImportTest(testFile, testName string) bool {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module github.com/uber-go/gopatch

go 1.18
go 1.22

require (
github.com/google/go-intervals v0.0.2
github.com/jessevdk/go-flags v1.5.0
github.com/kr/pretty v0.3.1
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/rogpeppe/go-internal v1.12.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
go.uber.org/multierr v1.11.0
golang.org/x/tools v0.18.0
golang.org/x/tools v0.19.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type options struct {
DisplayVersion bool `long:"version"`
Print bool `long:"print-only"`
SkipImportProcessing bool `long:"skip-import-processing"`
SkipGenerated bool `long:"skip-generated"`
Args arguments `positional-args:"yes"`
Verbose bool `short:"v" long:"verbose"`
}
Expand Down Expand Up @@ -94,6 +95,10 @@ func newArgParser() (*flags.Parser, *options) {

parser.FindOptionByLongName("skip-import-processing").
Description = "Skips processing of imports."

parser.FindOptionByLongName("skip-generated").
Description = "Skips running on files with generated code."

parser.Args()[0].
Description = "One or more files or directores containing Go code. " +
"When directories are provided, all Go files in them and their " +
Expand Down Expand Up @@ -284,6 +289,11 @@ func (cmd *mainCmd) Run(args []string) error {
continue
}

if opts.SkipGenerated && checkGeneratedCode(f) {
log.Printf("generated file %s: skipped", filename)
continue
}

f, comments, ok := patchRunner.Apply(filename, f)
// If at least one patch didn't match, there's nothing to do.
// If --print-only was passed, print the contents out as-is.
Expand Down Expand Up @@ -341,6 +351,18 @@ func (cmd *mainCmd) Run(args []string) error {
return multierr.Combine(errors...)
}

func checkGeneratedCode(f *ast.File) bool {
if ast.IsGenerated(f) {
return true
}
for _, comm := range f.Doc.List {
if strings.Contains(comm.Text, "@generated") {
return true
}
}
return false
}

func (cmd *mainCmd) preview(
filename string,
originalContent, modifiedContent []byte,
Expand Down
49 changes: 49 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package main

import (
"bytes"
"go/ast"
"go/token"
"os"
"path/filepath"
Expand Down Expand Up @@ -60,6 +61,15 @@ func TestNewArgParser(t *testing.T) {
SkipImportProcessing: false,
},
},
{
desc: "skip-generated",
give: []string{"--skip-generated", "-p", "testdata/patch/replace_to_with_ptr.patch", "testdata/test_files/skip_generated_files/test1.go"},
want: options{
Patches: []string{"testdata/patch/replace_to_with_ptr.patch"},
Args: arguments{Patterns: []string{"testdata/test_files/skip_generated_files/test1.go"}},
SkipGenerated: true,
},
},
{
desc: "skip-import-processing",
give: []string{"--skip-import-processing", "-p", "testdata/patch/replace_to_with_ptr.patch", "testdata/test_files/skip_import_processing_example/test1.go"},
Expand All @@ -78,6 +88,7 @@ func TestNewArgParser(t *testing.T) {
assert.Equal(t, tt.want.Patches, opts.Patches)
assert.Equal(t, tt.want.Args, opts.Args)
assert.Equal(t, tt.want.Diff, opts.Diff)
assert.Equal(t, tt.want.SkipGenerated, opts.SkipGenerated)
assert.Equal(t, tt.want.SkipImportProcessing, opts.SkipImportProcessing)
})
}
Expand Down Expand Up @@ -211,3 +222,41 @@ func TestPreview(t *testing.T) {
stdout.String())
})
}

func TestCheckGeneratedCode(t *testing.T) {
t.Parallel()
tests := []struct {
name string
file *ast.File
want bool
}{
{
name: "@generated",
file: &ast.File{
Doc: &ast.CommentGroup{
List: []*ast.Comment{
{Text: "@generated"},
},
},
},
want: true,
},
{
name: "non-generated code",
file: &ast.File{
Doc: &ast.CommentGroup{
List: []*ast.Comment{
{Text: "sad times"},
{Text: "not generated code"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := checkGeneratedCode(tt.file)
assert.Equal(t, tt.want, got)
})
}
}
13 changes: 13 additions & 0 deletions testdata/test_files/skip_generated_files/simple_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/uber-go/gopatch/tools

go 1.18
go 1.22

require (
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
honnef.co/go/tools v0.4.6
)
Expand Down
5 changes: 3 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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/exp/typeparams v0.0.0-20221208152030-732eee02a75a h1:Jw5wfR+h9mnIYH+OtGT2im5wV1YGGDora5vTv/aa5bE=
Expand All @@ -19,6 +19,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
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/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
Expand Down

0 comments on commit 2604c6e

Please sign in to comment.