From ae6eb78ed69ecefc2b3d9cf056777b272e47fd0a Mon Sep 17 00:00:00 2001 From: Ricky Stewart Date: Thu, 5 Aug 2021 16:04:07 -0500 Subject: [PATCH] dev: teach dev how to lint Closes #68547. Release note: None --- pkg/cmd/dev/lint.go | 39 ++++++++++++++++++++----- pkg/cmd/dev/test.go | 3 +- pkg/cmd/dev/testdata/lint.txt | 15 ++++++++++ pkg/cmd/dev/testdata/recording/lint.txt | 35 ++++++++++++++++++++++ 4 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 pkg/cmd/dev/testdata/lint.txt create mode 100644 pkg/cmd/dev/testdata/recording/lint.txt diff --git a/pkg/cmd/dev/lint.go b/pkg/cmd/dev/lint.go index c2ed5a32ad2d..87c604f9831f 100644 --- a/pkg/cmd/dev/lint.go +++ b/pkg/cmd/dev/lint.go @@ -10,14 +10,13 @@ package main -import ( - "github.com/cockroachdb/errors" - "github.com/spf13/cobra" -) +import "github.com/spf13/cobra" + +const shortFlag = "short" // makeLintCmd constructs the subcommand used to run the specified linters. func makeLintCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.Command { - return &cobra.Command{ + lintCmd := &cobra.Command{ Use: "lint", Short: `Run the specified linters`, Long: `Run the specified linters.`, @@ -26,9 +25,33 @@ func makeLintCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.Comm Args: cobra.NoArgs, RunE: runE, } + addCommonTestFlags(lintCmd) + lintCmd.Flags().Bool(shortFlag, false, "run only short lints") + return lintCmd } -func (*dev) lint(cmd *cobra.Command, args []string) error { - // TODO(irfansharif): Flesh out the example usage patterns. - return errors.New("unimplemented") +func (d *dev) lint(cmd *cobra.Command, _ []string) error { + ctx := cmd.Context() + filter := mustGetFlagString(cmd, filterFlag) + timeout := mustGetFlagDuration(cmd, timeoutFlag) + short := mustGetFlagBool(cmd, shortFlag) + + var args []string + // NOTE the --config=test here. It's very important we compile the test binary with the + // appropriate stuff (gotags, etc.) + args = append(args, "run", "--color=yes", "--config=test", "//build/bazelutil:lint") + args = append(args, getConfigFlags()...) + args = append(args, mustGetRemoteCacheArgs(remoteCacheAddr)...) + args = append(args, "--", "-test.v") + if short { + args = append(args, "-test.short") + } + if timeout > 0 { + args = append(args, "-test.timeout", timeout.String()) + } + if filter != "" { + args = append(args, "-test.run", filter) + } + + return d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...) } diff --git a/pkg/cmd/dev/test.go b/pkg/cmd/dev/test.go index 605a7a5b7c6e..9f6616e8d19d 100644 --- a/pkg/cmd/dev/test.go +++ b/pkg/cmd/dev/test.go @@ -186,8 +186,7 @@ func (d *dev) runUnitTest(cmd *cobra.Command, pkgs []string) error { args = append(args, "--test_output", "errors") } - err := d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...) - return err + return d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...) } func (d *dev) runLogicTest(cmd *cobra.Command) error { diff --git a/pkg/cmd/dev/testdata/lint.txt b/pkg/cmd/dev/testdata/lint.txt new file mode 100644 index 000000000000..00946e364f7f --- /dev/null +++ b/pkg/cmd/dev/testdata/lint.txt @@ -0,0 +1,15 @@ +dev lint +---- +getenv PATH +which cc +readlink /usr/local/opt/ccache/libexec/cc +export PATH=/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin +bazel run --color=yes --config=test //build/bazelutil:lint --config=dev -- -test.v + +dev lint --short --timeout=5m +---- +getenv PATH +which cc +readlink /usr/local/opt/ccache/libexec/cc +export PATH=/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin +bazel run --color=yes --config=test //build/bazelutil:lint --config=dev -- -test.v -test.short -test.timeout 5m0s diff --git a/pkg/cmd/dev/testdata/recording/lint.txt b/pkg/cmd/dev/testdata/recording/lint.txt new file mode 100644 index 000000000000..0c4102b56d3a --- /dev/null +++ b/pkg/cmd/dev/testdata/recording/lint.txt @@ -0,0 +1,35 @@ +getenv PATH +---- +/usr/local/opt/ccache/libexec:/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin + +which cc +---- +/usr/local/opt/ccache/libexec/cc + +readlink /usr/local/opt/ccache/libexec/cc +---- +../bin/ccache + +export PATH=/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin +---- + +bazel run --color=yes --config=test //build/bazelutil:lint --config=dev -- -test.v +---- + +getenv PATH +---- +/usr/local/opt/ccache/libexec:/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin + +which cc +---- +/usr/local/opt/ccache/libexec/cc + +readlink /usr/local/opt/ccache/libexec/cc +---- +../bin/ccache + +export PATH=/usr/local/opt/make/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin +---- + +bazel run --color=yes --config=test //build/bazelutil:lint --config=dev -- -test.v -test.short -test.timeout 5m0s +----