internal: split test logs to per module

Fixes #1801

Change-Id: I0e01794b09d6b448d6efb06c946310fbf4187bb3
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/52590
Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
Reviewed-by: kokoro <noreply+kokoro@google.com>
diff --git a/internal/kokoro/continuous.sh b/internal/kokoro/continuous.sh
index 17cb37c..909f823 100755
--- a/internal/kokoro/continuous.sh
+++ b/internal/kokoro/continuous.sh
@@ -36,14 +36,12 @@
 
 go version
 
-# Set $GOPATH
-export GOPATH="$HOME/go"
-export GOCLOUD_HOME=$GOPATH/src/cloud.google.com/go/
+export GOCLOUD_HOME=$KOKORO_ARTIFACTS_DIR/google-cloud-go/
 export PATH="$GOPATH/bin:$PATH"
 export GO111MODULE=on
 export GOPROXY=https://proxy.golang.org
 
-# Move code into $GOPATH and get dependencies
+# Move code into artifacts dir
 mkdir -p $GOCLOUD_HOME
 git clone . $GOCLOUD_HOME
 cd $GOCLOUD_HOME
@@ -55,30 +53,23 @@
 go install github.com/jstemmer/go-junit-report
 ./internal/kokoro/vet.sh
 
-mkdir $KOKORO_ARTIFACTS_DIR/tests
-
-# Takes the kokoro output log (raw stdout) and creates a machine-parseable xml
-# file (xUnit). Then it exits with whatever exit code the last command had.
-create_junit_xml() {
-  cat $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_CHANGE_NUMBER.txt \
-    | go-junit-report > $KOKORO_ARTIFACTS_DIR/tests/sponge_log.xml
-
-  if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"continuous"* ]]; then
-    chmod +x $KOKORO_GFILE_DIR/linux_amd64/buildcop
-    $KOKORO_GFILE_DIR/linux_amd64/buildcop -logs_dir=$KOKORO_ARTIFACTS_DIR
-  fi
-}
-
-trap create_junit_xml EXIT ERR
-
 exit_code=0
 # Run tests and tee output to log file, to be pushed to GCS as artifact.
 for i in `find . -name go.mod`; do
   pushd `dirname $i`;
     go test -race -v -timeout 30m ./... 2>&1 \
-      | tee -a $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_CHANGE_NUMBER.txt
+      | tee sponge_log.log
+    # Takes the kokoro output log (raw stdout) and creates a machine-parseable xml
+    # file (xUnit). Then it exits with whatever exit code the last command had.
+    cat sponge_log.log \
+      | go-junit-report -set-exit-code > sponge_log.xml
     exit_code=$(($exit_code + $?))
   popd;
 done
 
+if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"continuous"* ]]; then
+  chmod +x $KOKORO_GFILE_DIR/linux_amd64/buildcop
+  $KOKORO_GFILE_DIR/linux_amd64/buildcop -logs_dir=$GOCLOUD_HOME
+fi
+
 exit $exit_code
diff --git a/internal/kokoro/presubmit.sh b/internal/kokoro/presubmit.sh
index 73c7bd4..8801eaf 100755
--- a/internal/kokoro/presubmit.sh
+++ b/internal/kokoro/presubmit.sh
@@ -26,14 +26,12 @@
 
 go version
 
-# Set $GOPATH
-export GOPATH="$HOME/go"
-export GOCLOUD_HOME=$GOPATH/src/cloud.google.com/go/
+export GOCLOUD_HOME=$KOKORO_ARTIFACTS_DIR/google-cloud-go/
 export PATH="$GOPATH/bin:$PATH"
 export GO111MODULE=on
 export GOPROXY=https://proxy.golang.org
 
-# Move code into $GOPATH and get dependencies
+# Move code into artifacts dir
 mkdir -p $GOCLOUD_HOME
 git clone . $GOCLOUD_HOME
 cd $GOCLOUD_HOME
@@ -46,23 +44,16 @@
 ./internal/kokoro/vet.sh
 ./internal/kokoro/check_incompat_changes.sh
 
-mkdir $KOKORO_ARTIFACTS_DIR/tests
-
-# Takes the kokoro output log (raw stdout) and creates a machine-parseable xml
-# file (xUnit). Then it exits with whatever exit code the last command had.
-create_junit_xml() {
-  cat $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_CHANGE_NUMBER.txt \
-    | go-junit-report > $KOKORO_ARTIFACTS_DIR/tests/sponge_log.xml
-}
-
-trap create_junit_xml EXIT ERR
-
 exit_code=0
 # Run tests and tee output to log file, to be pushed to GCS as artifact.
 for i in `find . -name go.mod`; do
   pushd `dirname $i`;
     go test -race -v -timeout 15m -short ./... 2>&1 \
-      | tee -a $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_CHANGE_NUMBER.txt
+      | tee sponge_log.log
+    # Takes the kokoro output log (raw stdout) and creates a machine-parseable xml
+    # file (xUnit). Then it exits with whatever exit code the last command had.
+    cat sponge_log.log \
+      | go-junit-report -set-exit-code > sponge_log.xml
     exit_code=$(($exit_code + $?))
   popd;
 done