gopls/integration/govim: enable running at main

We're going to switch to running govim tests at main as post-submit CI
rather than presubmit, and will also switch to running them via Kokoro
using the run_local script rather than cloud build.

Enable this by changing the semantics of run_local.sh to default to
main.

For golang/go#40451

Change-Id: I9c311dea8326a36a3f8335eddbfae0ce7f02f6bf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/245539
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/gopls/integration/govim/Dockerfile b/gopls/integration/govim/Dockerfile
index f729e2f..75944eb 100644
--- a/gopls/integration/govim/Dockerfile
+++ b/gopls/integration/govim/Dockerfile
@@ -5,16 +5,12 @@
 # govim requires a more recent version of vim than is available in most
 # distros, so we build from their base image.
 FROM govim/govim:latest-vim
+ARG GOVIM_REF
 
-# We use a pinned version of govim so that this build is repeatable, and so
-# that we're not sensitive to test breakages in govim.
-# TODO(findleyr): Once a version of govim has been tagged containing
-# https://github.com/govim/govim/pull/629, switch this to @latest.
 ENV GOPROXY=https://proxy.golang.org GOPATH=/go VIM_FLAVOR=vim
 WORKDIR /src
 
 # Clone govim. In order to use the go command for resolving latest, we download
 # a redundant copy of govim to the build cache using `go mod download`.
-RUN GOVIM_VERSION=$(go mod download -json github.com/govim/govim@latest | jq -r .Version) && \
-    git clone https://github.com/govim/govim /src/govim && cd /src/govim && \
-    git checkout $GOVIM_VERSION
+RUN git clone https://github.com/govim/govim /src/govim && cd /src/govim && \
+    git checkout $GOVIM_REF
diff --git a/gopls/integration/govim/run_local.sh b/gopls/integration/govim/run_local.sh
index 361a676..73d0bf0 100755
--- a/gopls/integration/govim/run_local.sh
+++ b/gopls/integration/govim/run_local.sh
@@ -8,15 +8,22 @@
 
 usage() {
   cat <<EOUSAGE
-Usage: $0 [--sudo] [--short]
+Usage: $0 [--sudo] [--short] [--version (semver|latest)]
 
-Run govim tests against HEAD using local docker. If --sudo is set, run docker
-with sudo. If --short is set, run `go test` with `-short`.
+Args:
+  --sudo     run docker with sudo
+  --short    run `go test` with `-short`
+  --version  run on the specific tagged Go version (or latest) rather
+             than the default branch
+
+Run govim tests against HEAD using local docker.
 EOUSAGE
 }
 
 SUDO_IF_NEEDED=
 TEST_SHORT=
+DOCKERFILE=gopls/integration/govim/Dockerfile
+GOVIM_REF=main
 while [[ $# -gt 0 ]]; do
   case "$1" in
     "-h" | "--help" | "help")
@@ -31,6 +38,20 @@
       TEST_SHORT="-short"
       shift
       ;;
+    "--version")
+      if [[ -z "$2" ]]; then
+        usage
+        exit 1
+      fi
+      GOVIM_REF=$2
+      if [[ "${GOVIM_REF}" == "latest" ]]; then
+        TMPGOPATH=$(mktemp -d)
+        trap "GOPATH=${TMPGOPATH} go clean -modcache && rm -r ${TMPGOPATH}" EXIT
+        GOVIM_REF=$(GOPATH=${TMPGOPATH} go mod download -json \
+          github.com/govim/govim@latest | jq -r .Version)
+      fi
+      shift 2
+      ;;
     *)
       usage
       exit 1
@@ -49,8 +70,12 @@
 
 # Build the test harness. Here we are careful to pass in a very limited build
 # context so as to optimize caching.
+echo "Checking out govim@${GOVIM_REF}"
 cd "${tools_dir}"
-${SUDO_IF_NEEDED}docker build -t gopls-govim-harness -f gopls/integration/govim/Dockerfile \
+${SUDO_IF_NEEDED}docker build \
+  --build-arg GOVIM_REF="${GOVIM_REF}" \
+  -t gopls-govim-harness:${GOVIM_REF} \
+  -f gopls/integration/govim/Dockerfile \
   gopls/integration/govim
 
 # Run govim integration tests.
@@ -60,6 +85,6 @@
   -v "${tools_dir}:/src/tools" \
   -w "/src/govim" \
   --ulimit memlock=-1:-1 \
-  gopls-govim-harness \
+  gopls-govim-harness:${GOVIM_REF} \
   go test ${TEST_SHORT} ./cmd/govim \
     -gopls "/src/tools/gopls/${temp_gopls_name}"