Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: TestProtoMarshal correctly avoids Marshal methods #18496

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 131 additions & 47 deletions build/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,72 @@ func TestStyle(t *testing.T) {

t.Run("TestEnvutil", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `os\.(Getenv|LookupEnv)`, "--", "*.go")
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
re string
excludes []string
}{
{re: `\bos\.(Getenv|LookupEnv)\("COCKROACH`},
{
re: `\bos\.(Getenv|LookupEnv)\(`,
excludes: []string{
":!acceptance",
":!build/style_test.go",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't in pkg, is it? Why would it get picked up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think git grep doesn't care about the directory you're in, or at least it doesn't when you specify '*.go'.

":!ccl/acceptanceccl/backup_test.go",
":!ccl/sqlccl/backup_cloud_test.go",
":!ccl/storageccl/export_storage_test.go",
":!cmd",
":!util/envutil/env.go",
":!util/log/clog.go",
":!util/log/color.go",
":!util/sdnotify/sdnotify_unix.go",
},
},
} {
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
append([]string{
"grep",
"-nE",
tc.re,
"--",
"*.go",
}, tc.excludes...)...,
)
if err != nil {
t.Fatal(err)
}

if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`^cmd(/.*)?/\w+\.go\b`),
stream.GrepNot(`^build/style_test\.go\b`),
stream.GrepNot(`^ccl/(sqlccl/backup_cloud|storageccl/export_storage|acceptanceccl/backup)_test\.go\b`),
stream.GrepNot(`^acceptance(/.*)?/\w+\.go\b`),
stream.GrepNot(`^util/(log|envutil|sdnotify)/\w+\.go\b`),
), func(s string) {
t.Errorf(`%s <- forbidden; use "envutil" instead`, s)
}); err != nil {
t.Error(err)
}
if err := stream.ForEach(filter, func(s string) {
t.Errorf(`%s <- forbidden; use "envutil" instead`, s)
}); err != nil {
t.Error(err)
}

if err := cmd.Wait(); err != nil {
if out := stderr.String(); len(out) > 0 {
t.Fatalf("err=%s, stderr=%s", err, out)
if err := cmd.Wait(); err != nil {
if out := stderr.String(); len(out) > 0 {
t.Fatalf("err=%s, stderr=%s", err, out)
}
}
}
})

t.Run("TestSyncutil", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `sync\.(RW)?Mutex`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\bsync\.(RW)?Mutex`,
"--",
"*.go",
":!util/syncutil/mutex_sync.go",
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -169,10 +203,7 @@ func TestStyle(t *testing.T) {
t.Fatal(err)
}

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`^util/syncutil/mutex_sync\.go\b`),
), func(s string) {
if err := stream.ForEach(filter, func(s string) {
t.Errorf(`%s <- forbidden; use "syncutil.{,RW}Mutex" instead`, s)
}); err != nil {
t.Error(err)
Expand All @@ -187,6 +218,7 @@ func TestStyle(t *testing.T) {

t.Run("TestTodoStyle", func(t *testing.T) {
t.Parallel()
// TODO(tamird): enforce presence of name.
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `\sTODO\([^)]*\)[^:]`, "--", "*.go")
if err != nil {
t.Fatal(err)
Expand All @@ -211,7 +243,23 @@ func TestStyle(t *testing.T) {

t.Run("TestTimeutil", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `time\.(Now|Since)`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\btime\.(Now|Since)`,
"--",
"*.go",
":!util/log/clog_test.go",
":!util/log/clog.go",
":!util/log/crash_reporting.go",
":!util/log/file_test.go",
":!util/timeutil/now_unix.go",
":!util/timeutil/now_windows.go",
":!util/tracing/tracer_span.go",
":!util/tracing/tracer.go",
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -220,10 +268,7 @@ func TestStyle(t *testing.T) {
t.Fatal(err)
}

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`^util/(log|syncutil|timeutil|tracing)/\w+\.go\b`),
), func(s string) {
if err := stream.ForEach(filter, func(s string) {
t.Errorf(`%s <- forbidden; use "timeutil" instead`, s)
}); err != nil {
t.Error(err)
Expand All @@ -238,7 +283,18 @@ func TestStyle(t *testing.T) {

t.Run("TestGrpc", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `grpc.NewServer\([^)]*\)`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\bgrpc\.NewServer\(`,
"--",
"*.go",
":!rpc/context_test.go",
":!rpc/context.go",
":!util/grpcutil/grpc_util_test.go",
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -247,11 +303,7 @@ func TestStyle(t *testing.T) {
t.Fatal(err)
}

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`^rpc/context(_test)?\.go\b`),
stream.GrepNot(`^util/grpcutil/`),
), func(s string) {
if err := stream.ForEach(filter, func(s string) {
t.Errorf(`%s <- forbidden; use "rpc.NewServer" instead`, s)
}); err != nil {
t.Error(err)
Expand All @@ -266,7 +318,17 @@ func TestStyle(t *testing.T) {

t.Run("TestProtoClone", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `\.Clone\([^)]+\)`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\.Clone\([^)]`,
"--",
"*.go",
":!util/protoutil/clone_test.go",
":!util/protoutil/clone.go",
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -277,8 +339,7 @@ func TestStyle(t *testing.T) {

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`protoutil\.Clone\([^)]+\)`),
stream.GrepNot(`^util/protoutil/clone(_test)?\.go\b`),
stream.GrepNot(`protoutil\.Clone\(`),
), func(s string) {
t.Errorf(`%s <- forbidden; use "protoutil.Clone" instead`, s)
}); err != nil {
Expand All @@ -294,7 +355,17 @@ func TestStyle(t *testing.T) {

t.Run("TestProtoMarshal", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `\.Marshal\([^)]+\)`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\.Marshal\(`,
"--",
"*.go",
":!util/protoutil/marshal.go",
":!settings/settings_test.go",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern really seems to deserve a grepExcluding(regexpattern, "file", "file2", ...) function, but this is an improvement as is so no need to hold up this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I'm not sure I agree that there's much benefit to the indirection. Feel free to follow up if you feel strongly enough.

if err != nil {
t.Fatal(err)
}
Expand All @@ -305,8 +376,7 @@ func TestStyle(t *testing.T) {

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`(json|yaml|protoutil|Field)\.Marshal`),
stream.GrepNot(`^util/protoutil/marshal(_test)?\.go\b`),
stream.GrepNot(`(json|yaml|protoutil|\.Field)\.Marshal\(`),
), func(s string) {
t.Errorf(`%s <- forbidden; use "protoutil.Marshal" instead`, s)
}); err != nil {
Expand All @@ -322,7 +392,16 @@ func TestStyle(t *testing.T) {

t.Run("TestProtoUnmarshal", func(t *testing.T) {
t.Parallel()
cmd, stderr, filter, err := dirCmd(pkg.Dir, "git", "grep", "-nE", `\.Unmarshal\([^)]+\)`, "--", "*.go")
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
"git",
"grep",
"-nE",
`\.Unmarshal\(`,
"--",
"*.go",
":!*.pb.go",
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -333,8 +412,7 @@ func TestStyle(t *testing.T) {

if err := stream.ForEach(stream.Sequence(
filter,
stream.GrepNot(`(json|jsonpb|yaml|proto)\.Unmarshal`),
stream.GrepNot(`\.pb\.go\b`),
stream.GrepNot(`(json|jsonpb|yaml|proto)\.Unmarshal\(`),
), func(s string) {
t.Errorf(`%s <- forbidden; use "proto.Unmarshal" instead`, s)
}); err != nil {
Expand Down Expand Up @@ -649,6 +727,9 @@ func TestStyle(t *testing.T) {
// TODO(tamird): replace this with errcheck.NewChecker() when
// https://github.com/dominikh/go-tools/issues/57 is fixed.
t.Run("TestErrCheck", func(t *testing.T) {
if testing.Short() {
t.Skip("short flag")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason you're sidecar-ing this bit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this just got missed earlier; returncheck is roughly the same thing and it has this logic.

// errcheck uses 1GB of ram (as of 2017-02-18), so don't parallelize it.
cmd, stderr, filter, err := dirCmd(
pkg.Dir,
Expand Down Expand Up @@ -679,6 +760,9 @@ func TestStyle(t *testing.T) {
})

t.Run("TestReturnCheck", func(t *testing.T) {
if testing.Short() {
t.Skip("short flag")
}
// returncheck uses 1GB of ram (as of 2017-02-18), so don't parallelize it.
cmd, stderr, filter, err := dirCmd(pkg.Dir, "returncheck", pkgScope)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/ccl/acceptanceccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl"
"github.com/cockroachdb/cockroach/pkg/sql/jobs"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -58,8 +59,8 @@ type benchmarkTest struct {
}

func (bt *benchmarkTest) Start(ctx context.Context) {
licenseKey := os.Getenv("COCKROACH_DEV_LICENSE")
if licenseKey == "" {
licenseKey, ok := envutil.EnvString("COCKROACH_DEV_LICENSE", 0)
if !ok {
bt.b.Fatal("testing enterprise features requires setting COCKROACH_DEV_LICENSE")
}
bt.f = acceptance.MakeFarmer(bt.b, bt.prefix, acceptance.GetStopper())
Expand Down
3 changes: 2 additions & 1 deletion pkg/ccl/sqlccl/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/interval"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
Expand Down Expand Up @@ -315,7 +316,7 @@ func writeBackupDescriptor(
) error {
sort.Sort(backupFileDescriptors(desc.Files))

descBuf, err := desc.Marshal()
descBuf, err := protoutil.Marshal(desc)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ccl/sqlccl/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/pkg/errors"
Expand Down Expand Up @@ -658,7 +659,7 @@ func finalizeCSVBackup(
backupDesc.NodeID = execCfg.NodeID.Get()
backupDesc.ClusterID = execCfg.ClusterID()
}
descBuf, err := backupDesc.Marshal()
descBuf, err := protoutil.Marshal(backupDesc)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ccl/sqlccl/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
)

// Load converts r into SSTables and backup descriptors. database is the name
Expand Down Expand Up @@ -218,7 +219,7 @@ func Load(
}
}

descBuf, err := backup.Marshal()
descBuf, err := protoutil.Marshal(&backup)
if err != nil {
return BackupDescriptor{}, errors.Wrap(err, "marshal backup descriptor")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ccl/sqlccl/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/interval"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
Expand Down Expand Up @@ -757,7 +758,7 @@ func restore(
// Get TableRekeys to use when importing raw data.
var rekeys []roachpb.ImportRequest_TableRekey
for i := range tables {
newDescBytes, err := sqlbase.WrapDescriptor(tables[i]).Marshal()
newDescBytes, err := protoutil.Marshal(sqlbase.WrapDescriptor(tables[i]))
if err != nil {
return failed, errors.Wrap(err, "marshalling descriptor")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ccl/storageccl/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
)

func BenchmarkAddSSTable(b *testing.B) {
Expand Down Expand Up @@ -175,7 +176,7 @@ func BenchmarkImport(b *testing.B) {
oldStartKey = sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.PrimaryIndex.ID)
newDesc := *tableDesc
newDesc.ID = id
newDescBytes, err := sqlbase.WrapDescriptor(&newDesc).Marshal()
newDescBytes, err := protoutil.Marshal(sqlbase.WrapDescriptor(&newDesc))
if err != nil {
panic(err)
}
Expand Down
Loading