Skip to content

Commit

Permalink
dev: test using datadriven
Browse files Browse the repository at this point in the history
This commit groups a few changes to speed up dev and make it easier to
test.

1. Update `dev bench` to run benchmarks using `bazel test` directly
   instead of first grepping for benchmarks, querying test targets, and
   only then invoking test binaries using `bazel run`. The latter
   approach was both slower and more difficult to test. `dev bench` now
   looks identical to the `dev test` implementation.

2. Simplify `dev test`; now that protobuf targets are directly
   buildable, we don't need any manual querying of build targets and
   filtering for only go_test ones -- we can more easy `bazel test`
   whatever was top-level package was specified. This reduces how much
   I/O needs to happen with the host environment which both speeds
   things up and makes it easier to test.
   a. It also allows us to partially revert #74808 while still retaining
      informative error messages about missing targets (bazel's ones
      out-of-the-box are pretty good).

3. Finally, introduce TestDataDriven and TestRecorderDriven.
   a. TestDataDriven makes use of datadriven to capture all operations
      executed by individual dev invocations. The testcases are defined
      under testdata/datadriven/*. DataDriven divvies up these files as
      subtests, so individual "files" are runnable through:

          dev test pkg/cmd/dev -f TestDataDrivenDriven/<fname> [--rewrite]
      OR  go test ./pkg/cmd/dev -run TestDataDrivenDriven/<fname> [-rewrite]

   b. TestRecorderDriven makes use of datadriven/recorder to record (if
      --rewrite is specified) or play back (if --rewrite is omitted) all
      operations executed by individual dev invocations. The testcases
      are defined under testdata/recorderdriven/*; each test file
      corresponds to a captured recording found in
      testdata/recorderdriven/*.rec.

          dev test pkg/cmd/dev -f TestRecorderDriven/<fname>
      OR  go test ./pkg/cmd/dev -run TestRecorderDriven/<fname> [-rewrite]

      Recordings are used to mock out "system" behavior. When --rewrite
      is specified, attempts to shell out to bazel or perform other OS
      operations (like creating, removing, symlinking filepaths) are
      intercepted and system responses are recorded for future playback.

---

It's worth comparing TestDataDriven and TestRecorderDriven.

1. TestDataDriven is well suited for exercising flows that don't depend
   on reading external state in order to function (simply translating a
   `dev test <target>` to its corresponding bazel invocation for e.g.)
   All operations are run in "dry-run" mode when --rewrite is specified;
   all exec/os commands return successfully with no error + an empty
   response, and just the commands are recorded as test data.

2. TestRecorderDriven in contrast works better for flows that do depend
   on external state during execution (like reading the set of targets
   from a file for e.g., or hoisting files from a sandbox by searching
   through the file system directly). With these, when --rewrite is
   specified, we actually do shell out  and record the data for future
   playback.

We previously (#68514) ripped out the equivalent of TestRecorderDriven
because it was difficult to use (large recordings, execution errors
failing the test, etc.) -- issues this PR tries to address. There are
some real limitations here though that may still make this approach
untenable: When --rewrite is used for TestRecorderDriven, because it
shells out, will:
- takes time proportional to the actual dev invocations;
- makes it difficult to run under bazel (through without --rewrite it
  works just fine);

The few remaining tests for this recorder stuff are probably examples of
dev doing too much, when it should instead push the logic down into
bazel rules. As we continue doing so, we should re-evaluate whether this
harness provides much value.

Release note: None
  • Loading branch information
irfansharif committed Feb 8, 2022
1 parent 3651e3c commit 30a0874
Show file tree
Hide file tree
Showing 57 changed files with 1,245 additions and 1,933 deletions.
60 changes: 60 additions & 0 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/dustin/go-humanize/com_github_dustin_go_humanize-v1.0.0.zip",
],
)
go_repository(
name = "com_github_dvyukov_go_fuzz",
build_file_proto_mode = "disable_global",
importpath = "github.com/dvyukov/go-fuzz",
sha256 = "0a4c4bc0a550c729115d74f6a636e5802894b33bc50aa8af99c4a70196d5990b",
strip_prefix = "github.com/dvyukov/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/dvyukov/go-fuzz/com_github_dvyukov_go_fuzz-v0.0.0-20210103155950-6a8e9d1f2415.zip",
],
)
go_repository(
name = "com_github_eapache_go_resiliency",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -2173,6 +2183,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/elastic/gosigar/com_github_elastic_gosigar-v0.14.1.zip",
],
)
go_repository(
name = "com_github_elazarl_go_bindata_assetfs",
build_file_proto_mode = "disable_global",
importpath = "github.com/elazarl/go-bindata-assetfs",
sha256 = "ee91e4dedf0efd24ddf201e8f8b62f0b79a64efd0d205b30bcd9fa95f905cd15",
strip_prefix = "github.com/elazarl/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/elazarl/go-bindata-assetfs/com_github_elazarl_go_bindata_assetfs-v1.0.1.zip",
],
)
go_repository(
name = "com_github_elazarl_goproxy",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -4107,6 +4127,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/influxdata/usage-client/com_github_influxdata_usage_client-v0.0.0-20160829180054-6d3895376368.zip",
],
)
go_repository(
name = "com_github_irfansharif_recorder",
build_file_proto_mode = "disable_global",
importpath = "github.com/irfansharif/recorder",
sha256 = "4a2f085d5339eba18558059c51110de1ff6d9ab8389ece8818fd2f62b7b2e7ab",
strip_prefix = "github.com/irfansharif/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/irfansharif/recorder/com_github_irfansharif_recorder-v0.0.0-20211218081646-a21b46510fd6.zip",
],
)
go_repository(
name = "com_github_iris_contrib_blackfriday",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -4567,6 +4597,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/julienschmidt/httprouter/com_github_julienschmidt_httprouter-v1.3.0.zip",
],
)
go_repository(
name = "com_github_julusian_godocdown",
build_file_proto_mode = "disable_global",
importpath = "github.com/Julusian/godocdown",
sha256 = "1bd26f1d29b20d40b3eb0a5678691a2e6e153c473efe079b8b1bbd97a7cc1f57",
strip_prefix = "github.com/Julusian/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/Julusian/godocdown/com_github_julusian_godocdown-v0.0.0-20170816220326-6d19f8ff2df8.zip",
],
)
go_repository(
name = "com_github_jung_kurt_gofpdf",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -6603,6 +6643,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/retailnext/hllpp/com_github_retailnext_hllpp-v1.0.1-0.20180308014038-101a6d2f8b52.zip",
],
)
go_repository(
name = "com_github_robertkrimen_godocdown",
build_file_proto_mode = "disable_global",
importpath = "github.com/robertkrimen/godocdown",
sha256 = "789ed4a63a797e0dbac7c358eafa8fec4c9885f67ee61da941af4bad2d8c3b55",
strip_prefix = "github.com/robertkrimen/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/robertkrimen/godocdown/com_github_robertkrimen_godocdown-v0.0.0-20130622164427-0bfa04905481.zip",
],
)
go_repository(
name = "com_github_robfig_cron_v3",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -7093,6 +7143,16 @@ def go_deps():
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/stefanberger/go-pkcs11uri/com_github_stefanberger_go_pkcs11uri-v0.0.0-20201008174630-78d3cae3a980.zip",
],
)
go_repository(
name = "com_github_stephens2424_writerset",
build_file_proto_mode = "disable_global",
importpath = "github.com/stephens2424/writerset",
sha256 = "a5444ddf04cda5666c4511e5ca793a80372d560376c4193a1fa2e2294d0760dc",
strip_prefix = "github.com/stephens2424/[email protected]",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/stephens2424/writerset/com_github_stephens2424_writerset-v1.0.2.zip",
],
)
go_repository(
name = "com_github_stoewer_go_strcase",
build_file_proto_mode = "disable_global",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/goware/modvendor v0.5.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/irfansharif/recorder v0.0.0-20211218081646-a21b46510fd6
github.com/jackc/pgconn v1.10.0
github.com/jackc/pgproto3/v2 v2.1.1
github.com/jackc/pgtype v1.8.1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtix
github.com/HdrHistogram/hdrhistogram-go v1.1.0/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4KhCrqr6GRJjdC/gNfTdxkIXvuGZZda2VM=
github.com/Julusian/godocdown v0.0.0-20170816220326-6d19f8ff2df8/go.mod h1:INZr5t32rG59/5xeltqoCJoNY7e5x/3xoY9WSWVWg74=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Masterminds/glide v0.13.2/go.mod h1:STyF5vcenH/rUqTEv+/hBXlSTo7KYwg2oc2f4tzPWic=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
Expand Down Expand Up @@ -637,6 +638,7 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-resiliency v1.2.0 h1:v7g92e/KSN71Rq7vSThKaWIq68fL4YHvWyiUKorFR1Q=
github.com/eapache/go-resiliency v1.2.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
Expand All @@ -650,6 +652,7 @@ github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaB
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/elastic/gosigar v0.14.1 h1:T0aQ7n/n2ZA9W7DmAnj60v+qzqKERdBgJBO1CG2W6rc=
github.com/elastic/gosigar v0.14.1/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs=
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/dot v0.15.0 h1:XDBW0Xco1QNyRb33cqLe10cT04yMWL1XpCZfa98Q6Og=
Expand Down Expand Up @@ -1229,6 +1232,8 @@ github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bS
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
github.com/influxdata/tdigest v0.0.2-0.20210216194612-fc98d27c9e8b/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po=
github.com/irfansharif/recorder v0.0.0-20211218081646-a21b46510fd6 h1:fwL5Wt/OpP14udrdhV+INmmRES4GWoPWqHamttadwKU=
github.com/irfansharif/recorder v0.0.0-20211218081646-a21b46510fd6/go.mod h1:0vDkLIc8rDX+zYp5wX/DG5MAWaHBAqmtXH/SE54vhpY=
github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI=
Expand Down Expand Up @@ -1841,6 +1846,7 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/retailnext/hllpp v1.0.1-0.20180308014038-101a6d2f8b52/go.mod h1:RDpi1RftBQPUCDRw6SmxeaREsAaRKnOclghuzp/WRzc=
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down Expand Up @@ -1952,6 +1958,7 @@ github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44=
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h1:AO3tvPzVZ/ayst6UlUKUv6rcPQInYe3IknH3jYhAKu8=
github.com/stephens2424/writerset v1.0.2/go.mod h1:aS2JhsMn6eA7e82oNmW4rfsgAOp9COBTTl8mzkwADnc=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
Expand Down Expand Up @@ -2507,6 +2514,7 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/dev/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ go_test(
name = "dev_test",
srcs = [
"datadriven_test.go",
"dev_test.go",
"recorderdriven_test.go",
],
data = glob(["testdata/**"]),
embed = [":dev_lib"],
deps = [
"//pkg/cmd/dev/io/exec",
"//pkg/cmd/dev/io/os",
"//pkg/cmd/dev/recording",
"//pkg/testutils",
"//pkg/testutils/skip",
"@com_github_alessio_shellescape//:shellescape",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_irfansharif_recorder//:recorder",
"@com_github_stretchr_testify//require",
],
)
130 changes: 60 additions & 70 deletions pkg/cmd/dev/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ package main

import (
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,6 +40,7 @@ func makeBenchCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.Com
benchCmd.Flags().BoolP(vFlag, "v", false, "show benchmark process output")
benchCmd.Flags().BoolP(showLogsFlag, "", false, "show crdb logs in-line")
benchCmd.Flags().Int(countFlag, 1, "run benchmark n times")
benchCmd.Flags().Bool(ignoreCacheFlag, false, "ignore cached benchmark runs")
// We use a string flag for benchtime instead of a duration; the go test
// runner accepts input of the form "Nx" to run the benchmark N times (see
// `go help testflag`).
Expand All @@ -55,101 +54,92 @@ func (d *dev) bench(cmd *cobra.Command, commandLine []string) error {
pkgs, additionalBazelArgs := splitArgsAtDash(cmd, commandLine)
ctx := cmd.Context()
var (
filter = mustGetFlagString(cmd, filterFlag)
timeout = mustGetFlagDuration(cmd, timeoutFlag)
short = mustGetFlagBool(cmd, shortFlag)
showLogs = mustGetFlagBool(cmd, showLogsFlag)
verbose = mustGetFlagBool(cmd, vFlag)
count = mustGetFlagInt(cmd, countFlag)
benchTime = mustGetFlagString(cmd, benchTimeFlag)
benchMem = mustGetFlagBool(cmd, benchMemFlag)
filter = mustGetFlagString(cmd, filterFlag)
ignoreCache = mustGetFlagBool(cmd, ignoreCacheFlag)
timeout = mustGetFlagDuration(cmd, timeoutFlag)
short = mustGetFlagBool(cmd, shortFlag)
showLogs = mustGetFlagBool(cmd, showLogsFlag)
verbose = mustGetFlagBool(cmd, vFlag)
count = mustGetFlagInt(cmd, countFlag)
benchTime = mustGetFlagString(cmd, benchTimeFlag)
benchMem = mustGetFlagBool(cmd, benchMemFlag)
)

// Enumerate all benches to run.
if len(pkgs) == 0 {
// Empty `dev bench` does the same thing as `dev bench pkg/...`
pkgs = append(pkgs, "pkg/...")
}
benchesMap := make(map[string]bool)

var args []string
args = append(args, "test")
args = append(args, mustGetRemoteCacheArgs(remoteCacheAddr)...)
if numCPUs != 0 {
args = append(args, fmt.Sprintf("--local_cpu_resources=%d", numCPUs))
}
if timeout > 0 {
args = append(args, fmt.Sprintf("--test_timeout=%d", int(timeout.Seconds())))
}

var testTargets []string
for _, pkg := range pkgs {
dir, isRecursive, tag, err := d.parsePkg(pkg)
if err != nil {
return err
pkg = strings.TrimPrefix(pkg, "//")
pkg = strings.TrimPrefix(pkg, "./")
pkg = strings.TrimRight(pkg, "/")

if !strings.HasPrefix(pkg, "pkg/") {
return fmt.Errorf("malformed package %q, expecting %q", pkg, "pkg/{...}")
}
if isRecursive {
// Use `git grep` to find all Go files that contain benchmark tests.
out, err := d.exec.CommandContextSilent(ctx, "git", "grep", "-l", "^func Benchmark", "--", dir+"/*_test.go")
if err != nil {
return err
}
files := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, file := range files {
dir, _ = filepath.Split(file)
dir = strings.TrimSuffix(dir, "/")
benchesMap[dir] = true
}
} else if tag != "" {
return fmt.Errorf("malformed package %q, tags not supported in 'bench' command", pkg)

var target string
if strings.Contains(pkg, ":") {
// For parity with bazel, we allow specifying named build targets.
target = pkg
} else {
benchesMap[dir] = true
target = fmt.Sprintf("%s:all", pkg)
}
testTargets = append(testTargets, target)
}
// De-duplicate and sort the list of benches to run.
var benches []string
for pkg := range benchesMap {
benches = append(benches, pkg)

args = append(args, testTargets...)
if ignoreCache {
args = append(args, "--nocache_test_results")
}
sort.Slice(benches, func(i, j int) bool { return benches[i] < benches[j] })

var argsBase []string
// NOTE the --config=test here. It's very important we compile the test binary with the
// appropriate stuff (gotags, etc.)
argsBase = append(argsBase, "run", "--config=test", "--test_sharding_strategy=disabled")
argsBase = append(argsBase, mustGetRemoteCacheArgs(remoteCacheAddr)...)
if numCPUs != 0 {
argsBase = append(argsBase, fmt.Sprintf("--local_cpu_resources=%d", numCPUs))
if filter == "" {
args = append(args, "--test_arg", "-test.bench=.")
} else {
args = append(args, "--test_arg", fmt.Sprintf("-test.bench=%s", filter))
}
if short {
args = append(args, "--test_arg", "-test.short")
}
if verbose {
argsBase = append(argsBase, "--test_arg", "-test.v")
args = append(args, "--test_arg", "-test.v")
}
if showLogs {
argsBase = append(argsBase, "--test_arg", "-show-logs")
args = append(args, "--test_arg", "-show-logs")
}
if count != 1 {
argsBase = append(argsBase, "--test_arg", fmt.Sprintf("-test.count=%d", count))
args = append(args, "--test_arg", fmt.Sprintf("-test.count=%d", count))
}
if benchTime != "" {
argsBase = append(argsBase, "--test_arg", fmt.Sprintf("-test.benchtime=%s", benchTime))
args = append(args, "--test_arg", fmt.Sprintf("-test.benchtime=%s", benchTime))
}
if benchMem {
argsBase = append(argsBase, "--test_arg", "-test.benchmem")
args = append(args, "--test_arg", "-test.benchmem")
}

for _, bench := range benches {
args := make([]string, len(argsBase))
copy(args, argsBase)
base := filepath.Base(bench)
target := "//" + bench + ":" + base + "_test"
args = append(args, target)
args = append(args, additionalBazelArgs...)
args = append(args, "--", "-test.run=-")
if filter == "" {
args = append(args, "-test.bench=.")
} else {
args = append(args, "-test.bench="+filter)
}
if timeout > 0 {
args = append(args, fmt.Sprintf("-test.timeout=%s", timeout.String()))
}
if short {
args = append(args, "-test.short", "-test.benchtime=1ns")
}
logCommand("bazel", args...)
err := d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...)
if err != nil {
return err
{ // Handle test output flags.
testOutputArgs := []string{"--test_output", "errors"}
if verbose || showLogs {
testOutputArgs = []string{"--test_output", "all"}
}
args = append(args, testOutputArgs...)
}

return nil
args = append(args, additionalBazelArgs...)

logCommand("bazel", args...)
return d.exec.CommandContextInheritingStdStreams(ctx, "bazel", args...)
}
2 changes: 1 addition & 1 deletion pkg/cmd/dev/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func makeBuilderCmd(runE func(cmd *cobra.Command, args []string) error) *cobra.C
func (d *dev) builder(cmd *cobra.Command, extraArgs []string) error {
ctx := cmd.Context()
volume := mustGetFlagString(cmd, volumeFlag)
args, err := d.getDockerRunArgs(ctx, volume, true)
args, err := d.getDockerRunArgs(ctx, volume, false)
args = append(args, extraArgs...)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 30a0874

Please sign in to comment.