From 32770542b21fde8d318bbcc1ab5081e6e38a0d47 Mon Sep 17 00:00:00 2001
From: Jan Tattermusch <jtattermusch@users.noreply.github.com>
Date: Tue, 14 Dec 2021 09:22:04 +0100
Subject: [PATCH] Cleanup of ObjC test scripts (#28333)

* cleanup cfstream tests scripts

* add xcodebuild_filter_output script

* cleanup scripts for ObjC CoreTests

* clean one more build_tests.sh file

* cleanup more scripts

* cleanup ios cpp scripts

* remove last copy of verbose_time

* revert readlink change
---
 .../tests/CoreTests/build_and_run_tests.sh    | 18 ++------
 .../tests/CoreTests/build_tests.sh            |  9 ++--
 .../tests/CoreTests/verbose_time.sh           | 20 --------
 src/objective-c/tests/build_one_example.sh    | 25 +++++-----
 src/objective-c/tests/build_tests.sh          |  8 ++--
 src/objective-c/tests/run_one_test.sh         | 18 +++-----
 src/objective-c/tests/verbose_time.sh         | 20 --------
 .../tests/xcodebuild_filter_output.sh         | 37 +++++++++++++++
 .../ios/CFStreamTests/build_and_run_tests.sh  | 46 +++++--------------
 .../iomgr/ios/CFStreamTests/build_tests.sh    |  9 ++--
 test/cpp/ios/build_and_run_tests.sh           | 19 +++-----
 test/cpp/ios/build_tests.sh                   |  6 +--
 test/cpp/ios/verbose_time.sh                  | 20 --------
 13 files changed, 95 insertions(+), 160 deletions(-)
 delete mode 100755 src/objective-c/tests/CoreTests/verbose_time.sh
 delete mode 100755 src/objective-c/tests/verbose_time.sh
 create mode 100755 src/objective-c/tests/xcodebuild_filter_output.sh
 delete mode 100755 test/cpp/ios/verbose_time.sh

diff --git a/src/objective-c/tests/CoreTests/build_and_run_tests.sh b/src/objective-c/tests/CoreTests/build_and_run_tests.sh
index 678ab59eb14..c75670c1a3c 100755
--- a/src/objective-c/tests/CoreTests/build_and_run_tests.sh
+++ b/src/objective-c/tests/CoreTests/build_and_run_tests.sh
@@ -16,25 +16,17 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l c++
 
-set -ev
+set -ex
+set -o pipefail  # preserve xcodebuild exit code when piping output
 
 cd "$(dirname "$0")"
 
-echo "TIME:  $(date)"
+time ./build_tests.sh
 
-./build_tests.sh | ./verbose_time.sh
-
-echo "TIME:  $(date)"
-
-set -o pipefail
-
-XCODEBUILD_FILTER='(^CompileC |^Ld |^ *[^ ]*clang |^ *cd |^ *export |^Libtool |^ *[^ ]*libtool |^CpHeader |^ *builtin-copy )'
+XCODEBUILD_FILTER_OUTPUT_SCRIPT="../xcodebuild_filter_output.sh"
 
 xcodebuild \
     -workspace CoreTests.xcworkspace \
     -scheme CoreTests \
     -destination name="iPhone 8" \
-    test \
-    | ./verbose_time.sh \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' -
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
diff --git a/src/objective-c/tests/CoreTests/build_tests.sh b/src/objective-c/tests/CoreTests/build_tests.sh
index 890955ac913..40917d017f9 100755
--- a/src/objective-c/tests/CoreTests/build_tests.sh
+++ b/src/objective-c/tests/CoreTests/build_tests.sh
@@ -16,27 +16,30 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -e
+set -ex
 
 # CocoaPods requires the terminal to be using UTF-8 encoding.
 export LANG=en_US.UTF-8
 
 cd "$(dirname "$0")"
 
+# Check that required tools are available.
 hash pod 2>/dev/null || { echo >&2 "Cocoapods needs to be installed."; exit 1; }
 hash xcodebuild 2>/dev/null || {
     echo >&2 "XCode command-line tools need to be installed."
     exit 1
 }
 
+# TODO(jtattermusch): make the cleanup command consistent in all build_tests.sh directories.
 # clean the directory
 rm -rf Pods
+# TODO(jtattermusch): is this wrong directory?
 rm -rf Tests.xcworkspace
 rm -f Podfile.lock
+# TODO(jtattermusch): is this wrong directory?
 rm -rf RemoteTestClientCpp/src
 
-echo "TIME:  $(date)"
-pod install
+time pod install
 
 # ios-cpp-test-cronet flakes sometimes because of missing files in Protobuf-C++,
 # add some log to help find out the root cause.
diff --git a/src/objective-c/tests/CoreTests/verbose_time.sh b/src/objective-c/tests/CoreTests/verbose_time.sh
deleted file mode 100755
index 05926caea85..00000000000
--- a/src/objective-c/tests/CoreTests/verbose_time.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-# Copyright 2019 gRPC authors.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-#!/bin/bash
-
-while IFS= read -r line; do
-  echo "$(date) - $line"
-done
diff --git a/src/objective-c/tests/build_one_example.sh b/src/objective-c/tests/build_one_example.sh
index 762af5e07f3..de686c31524 100755
--- a/src/objective-c/tests/build_one_example.sh
+++ b/src/objective-c/tests/build_one_example.sh
@@ -16,7 +16,7 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -ev
+set -ex
 
 # Params:
 # EXAMPLE_PATH - directory of the example
@@ -27,7 +27,7 @@ export LANG=en_US.UTF-8
 
 TEST_PATH=$(cd "$(dirname $0)" > /dev/null ; pwd)
 
-cd `dirname $0`/../../..
+cd $(dirname $0)/../../..
 
 cd $EXAMPLE_PATH
 
@@ -36,12 +36,14 @@ rm -rf Pods
 rm -rf *.xcworkspace
 rm -f Podfile.lock
 
-pod install | $TEST_PATH/verbose_time.sh
+time pod install
+
+set -o pipefail  # preserve xcodebuild exit code when piping output
+
+XCODEBUILD_FILTER_OUTPUT_SCRIPT="${TEST_PATH}/xcodebuild_filter_output.sh"
 
-set -o pipefail
-XCODEBUILD_FILTER='(^CompileC |^Ld |^.*clang |^ *cd |^ *export |^Libtool |^.*libtool |^CpHeader |^ *builtin-copy )'
 if [ "$SCHEME" == "tvOS-sample" ]; then
-  xcodebuild \
+  time xcodebuild \
     build \
     -workspace *.xcworkspace \
     -scheme $SCHEME \
@@ -50,11 +52,9 @@ if [ "$SCHEME" == "tvOS-sample" ]; then
     CODE_SIGN_IDENTITY="" \
     CODE_SIGNING_REQUIRED=NO \
     CODE_SIGNING_ALLOWED=NO \
-    | $TEST_PATH/verbose_time.sh \
-    | egrep -v "$XCODEBUILD_FILTER" \
-    | egrep -v "^$" -
+    | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
 else
-  xcodebuild \
+  time xcodebuild \
     build \
     -workspace *.xcworkspace \
     -scheme $SCHEME \
@@ -63,8 +63,5 @@ else
     CODE_SIGN_IDENTITY="" \
     CODE_SIGNING_REQUIRED=NO \
     CODE_SIGNING_ALLOWED=NO \
-    | $TEST_PATH/verbose_time.sh \
-    | egrep -v "$XCODEBUILD_FILTER" \
-    | egrep -v "^$" -
+    | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
 fi
-
diff --git a/src/objective-c/tests/build_tests.sh b/src/objective-c/tests/build_tests.sh
index 95d17cd74d7..7a964aaf4af 100755
--- a/src/objective-c/tests/build_tests.sh
+++ b/src/objective-c/tests/build_tests.sh
@@ -16,13 +16,14 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -ev
+set -ex
 
 # CocoaPods requires the terminal to be using UTF-8 encoding.
 export LANG=en_US.UTF-8
 
-cd $(dirname $0)
+cd "$(dirname "$0")"
 
+# Check that required tools are available.
 hash pod 2>/dev/null || { echo >&2 "Cocoapods needs to be installed."; exit 1; }
 hash xcodebuild 2>/dev/null || {
     echo >&2 "XCode command-line tools need to be installed."
@@ -34,8 +35,7 @@ rm -Rf Pods Podfile.lock Tests.xcworkspace
 rm -f RemoteTestClient/*.{h,m}
 pod cache clean --all
 
-echo "TIME:  $(date)"
-pod install --verbose | ./verbose_time.sh
+time pod install --verbose
 
 # verify pod header installation
 if [ -d "./Pods/Headers/Public/gRPC-Core/grpc/impl/codegen" ]
diff --git a/src/objective-c/tests/run_one_test.sh b/src/objective-c/tests/run_one_test.sh
index 53a813da19b..94020c5c8a7 100755
--- a/src/objective-c/tests/run_one_test.sh
+++ b/src/objective-c/tests/run_one_test.sh
@@ -16,7 +16,7 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -ev
+set -ex
 
 cd $(dirname $0)
 
@@ -40,6 +40,8 @@ INTEROP=../../../bazel-out/darwin-fastbuild/bin/test/cpp/interop/interop_server
 PLAIN_PORT=$(curl localhost:32766/get)
 TLS_PORT=$(curl localhost:32766/get)
 
+# start interop_server for plaintext and interop_server for TLS on random ports obtained
+# from the port server.
 $INTEROP --port=$PLAIN_PORT --max_send_message_size=8388608 &
 $INTEROP --port=$TLS_PORT --max_send_message_size=8388608 --use_tls &
 
@@ -63,9 +65,7 @@ function finish {
 }
 trap finish EXIT
 
-set -o pipefail
-
-XCODEBUILD_FILTER='(^CompileC |^Ld |^ *[^ ]*clang |^ *cd |^ *export |^Libtool |^ *[^ ]*libtool |^CpHeader |^ *builtin-copy )'
+set -o pipefail  # preserve xcodebuild exit code when piping output
 
 if [ -z $PLATFORM ]; then
 DESTINATION='name=iPhone 8'
@@ -77,8 +77,9 @@ elif [ $PLATFORM == tvos ]; then
 DESTINATION='platform=tvOS Simulator,name=Apple TV'
 fi
 
+XCODEBUILD_FILTER_OUTPUT_SCRIPT="./xcodebuild_filter_output.sh"
 
-xcodebuild \
+time xcodebuild \
     -workspace Tests.xcworkspace \
     -scheme $SCHEME \
     -destination "$DESTINATION" \
@@ -86,9 +87,4 @@ xcodebuild \
     HOST_PORT_LOCAL=localhost:$PLAIN_PORT \
     HOST_PORT_REMOTE=grpc-test.sandbox.googleapis.com \
     GCC_OPTIMIZATION_LEVEL=s \
-    test \
-    | ./verbose_time.sh \
-    | egrep -v "$XCODEBUILD_FILTER" \
-    | egrep -v '^$' \
-    | egrep -v "(GPBDictionary|GPBArray)" -
-
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
diff --git a/src/objective-c/tests/verbose_time.sh b/src/objective-c/tests/verbose_time.sh
deleted file mode 100755
index 05926caea85..00000000000
--- a/src/objective-c/tests/verbose_time.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-# Copyright 2019 gRPC authors.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-#!/bin/bash
-
-while IFS= read -r line; do
-  echo "$(date) - $line"
-done
diff --git a/src/objective-c/tests/xcodebuild_filter_output.sh b/src/objective-c/tests/xcodebuild_filter_output.sh
new file mode 100755
index 00000000000..218ea691308
--- /dev/null
+++ b/src/objective-c/tests/xcodebuild_filter_output.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+# Copyright 2021 The gRPC Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Be default xcodebuild generates gigabytes of useless output. We
+# use this script to make the logs smaller, while still keeping them
+# useful (e.g. we need to be able to see the logs printed by a test
+# when it fails on the CI).
+# Alternatives considered:
+# * "xcodebuild -quiet" prints much less output, but doesn't display the test outputs.
+# * "xcpretty" prints a nice and readable log, but doesn't display logs printed
+#   by the tests when they fail (and we need those)
+#
+# Usage:
+# set -o pipefail  # preserve xcodebuild's exit code when piping the output
+# xcodebuild ... | xcodebuild_filter_output.sh
+
+# need to be careful not to exclude important logs, so the patterns here need to be very specific.
+XCODEBUILD_FILTER='(^CompileC |^Ld |^.*clang |^ *cd |^ *export |^Libtool |^.*libtool |^CpHeader |^ *builtin-copy )'
+
+# we expect xcodebuild output piped to the stdin
+# - also skip empty lines
+grep -E -v "$XCODEBUILD_FILTER" - | grep -E -v "^$" -
+
+# TODO: What is this for?
+#| grep -E -v "(GPBDictionary|GPBArray)" -
diff --git a/test/core/iomgr/ios/CFStreamTests/build_and_run_tests.sh b/test/core/iomgr/ios/CFStreamTests/build_and_run_tests.sh
index 3c2b1a32783..4d770917288 100755
--- a/test/core/iomgr/ios/CFStreamTests/build_and_run_tests.sh
+++ b/test/core/iomgr/ios/CFStreamTests/build_and_run_tests.sh
@@ -16,57 +16,35 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -ev
-set -o pipefail
+set -ex
+set -o pipefail  # preserve xcodebuild exit code when piping output
 
 cd "$(dirname "$0")"
 
-echo "TIME:  $(date)"
+XCODEBUILD_FILTER_OUTPUT_SCRIPT="../../../../../src/objective-c/tests/xcodebuild_filter_output.sh"
 
-./build_tests.sh
+time ./build_tests.sh
 
-echo "TIME:  $(date)"
-
-XCODEBUILD_FILTER='(^CompileC |^Ld |^ *[^ ]*clang |^ *cd |^ *export |^Libtool |^ *[^ ]*libtool |^CpHeader |^ *builtin-copy )'
-
-xcodebuild \
+time xcodebuild \
     -workspace CFStreamTests.xcworkspace \
     -scheme CFStreamTests \
     -destination name="iPhone 8" \
-    test \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' \
-    | grep -E -v "(GPBDictionary|GPBArray)" -
-
-echo "TIME:  $(date)"
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
 
-xcodebuild \
+time xcodebuild \
     -workspace CFStreamTests.xcworkspace \
     -scheme CFStreamTests_Asan \
     -destination name="iPhone 8" \
-    test \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' \
-    | grep -E -v "(GPBDictionary|GPBArray)" -
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
 
-echo "TIME:  $(date)"
-
-xcodebuild \
+time xcodebuild \
     -workspace CFStreamTests.xcworkspace \
     -scheme CFStreamTests_Tsan \
     -destination name="iPhone 8" \
-    test \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' \
-    | grep -E -v "(GPBDictionary|GPBArray)" -
-
-echo "TIME:  $(date)"
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
 
-xcodebuild \
+time xcodebuild \
     -workspace CFStreamTests.xcworkspace \
     -scheme CFStreamTests_Msan \
     -destination name="iPhone 8" \
-    test \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' \
-    | grep -E -v "(GPBDictionary|GPBArray)" -
+    test | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
diff --git a/test/core/iomgr/ios/CFStreamTests/build_tests.sh b/test/core/iomgr/ios/CFStreamTests/build_tests.sh
index f4262456012..f2ea145b215 100755
--- a/test/core/iomgr/ios/CFStreamTests/build_tests.sh
+++ b/test/core/iomgr/ios/CFStreamTests/build_tests.sh
@@ -16,13 +16,14 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -ev
+set -ex
 
 # CocoaPods requires the terminal to be using UTF-8 encoding.
 export LANG=en_US.UTF-8
 
 cd "$(dirname "$0")"
 
+# Check that required tools are available.
 hash pod 2>/dev/null || { echo >&2 "Cocoapods needs to be installed."; exit 1; }
 hash xcodebuild 2>/dev/null || {
     echo >&2 "XCode command-line tools need to be installed."
@@ -31,10 +32,9 @@ hash xcodebuild 2>/dev/null || {
 
 # clean up pod cache and existing pods
 rm -Rf Pods Podfile.lock CFStreamTests.xcworkspace
-pod cache clean --all
+time pod cache clean --all
 
-echo "TIME:  $(date)"
-pod install --verbose
+time pod install --verbose
 
 # ios-test-cfstream-tests flakes sometimes because of missing files in gRPC-Core,
 # add some log to help find out the root cause.
@@ -47,4 +47,3 @@ then
 else
     echo "Error: grpc/impl/codegen/ hasn't been imported."
 fi
-
diff --git a/test/cpp/ios/build_and_run_tests.sh b/test/cpp/ios/build_and_run_tests.sh
index 7d674fcb2c7..53925f63cff 100755
--- a/test/cpp/ios/build_and_run_tests.sh
+++ b/test/cpp/ios/build_and_run_tests.sh
@@ -16,25 +16,18 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l c++
 
-set -ev
+set -ex
+set -o pipefail  # preserve xcodebuild exit code when piping output
 
 cd "$(dirname "$0")"
 
-echo "TIME:  $(date)"
+time ./build_tests.sh
 
-./build_tests.sh | ./verbose_time.sh
+XCODEBUILD_FILTER_OUTPUT_SCRIPT="../../../src/objective-c/tests/xcodebuild_filter_output.sh"
 
-echo "TIME:  $(date)"
-
-set -o pipefail
-
-XCODEBUILD_FILTER='(^CompileC |^Ld |^ *[^ ]*clang |^ *cd |^ *export |^Libtool |^ *[^ ]*libtool |^CpHeader |^ *builtin-copy )'
-
-xcodebuild \
+time xcodebuild \
     -workspace Tests.xcworkspace \
     -scheme CronetTests \
     -destination name="iPhone 8" \
     test \
-    | ./verbose_time.sh \
-    | grep -E -v "$XCODEBUILD_FILTER" \
-    | grep -E -v '^$' -
+    | "${XCODEBUILD_FILTER_OUTPUT_SCRIPT}"
diff --git a/test/cpp/ios/build_tests.sh b/test/cpp/ios/build_tests.sh
index 176cc083a01..27c103663c5 100755
--- a/test/cpp/ios/build_tests.sh
+++ b/test/cpp/ios/build_tests.sh
@@ -16,13 +16,14 @@
 # Don't run this script standalone. Instead, run from the repository root:
 # ./tools/run_tests/run_tests.py -l objc
 
-set -e
+set -ex
 
 # CocoaPods requires the terminal to be using UTF-8 encoding.
 export LANG=en_US.UTF-8
 
 cd "$(dirname "$0")"
 
+# Check that required tools are available.
 hash pod 2>/dev/null || { echo >&2 "Cocoapods needs to be installed."; exit 1; }
 hash xcodebuild 2>/dev/null || {
     echo >&2 "XCode command-line tools need to be installed."
@@ -35,8 +36,7 @@ rm -rf Tests.xcworkspace
 rm -f Podfile.lock
 rm -rf RemoteTestClientCpp/src
 
-echo "TIME:  $(date)"
-pod install
+time pod install
 
 # ios-cpp-test-cronet flakes sometimes because of missing files in Protobuf-C++,
 # add some log to help find out the root cause.
diff --git a/test/cpp/ios/verbose_time.sh b/test/cpp/ios/verbose_time.sh
deleted file mode 100755
index 05926caea85..00000000000
--- a/test/cpp/ios/verbose_time.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-# Copyright 2019 gRPC authors.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-#!/bin/bash
-
-while IFS= read -r line; do
-  echo "$(date) - $line"
-done