From 73cf09d5d1fa4bbf3c2c52b7a9962f70fdac331c Mon Sep 17 00:00:00 2001
From: Hannah Shi <hannahshisfb@gmail.com>
Date: Fri, 29 Oct 2021 15:57:34 -0700
Subject: [PATCH] [objc] podfile cleanup and add core test to script to run
 (#27740)

* podfile cleanup and add core test to script to run

* downgrade Xcode project version

* downgrade IPHONEOS_DEPLOYMENT_TARGET version
---
 gRPC-Core.podspec                             |  3 +-
 .../CoreTests.xcodeproj/project.pbxproj       | 10 ++--
 src/objective-c/tests/CoreTests/Podfile       | 11 +---
 .../tests/CoreTests/build_and_run_tests.sh    | 40 +++++++++++++++
 .../tests/CoreTests/build_tests.sh            | 51 +++++++++++++++++++
 .../tests/CoreTests/verbose_time.sh           | 20 ++++++++
 templates/gRPC-Core.podspec.template          |  3 +-
 tools/run_tests/run_tests.py                  |  7 +++
 8 files changed, 126 insertions(+), 19 deletions(-)
 create mode 100755 src/objective-c/tests/CoreTests/build_and_run_tests.sh
 create mode 100755 src/objective-c/tests/CoreTests/build_tests.sh
 create mode 100755 src/objective-c/tests/CoreTests/verbose_time.sh

diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec
index 39ba1973d3e..257b0d922ce 100644
--- a/gRPC-Core.podspec
+++ b/gRPC-Core.podspec
@@ -2278,8 +2278,7 @@ Pod::Spec.new do |s|
   s.prepare_command = <<-END_OF_COMMAND
     set -e
     find src/core -type f \\( -path '*.h' -or -path '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include <openssl/(.*)>;#if COCOAPODS==1\\\n  #include <openssl_grpc/\\1>\\\n#else\\\n  #include <openssl/\\1>\\\n#endif;g'
-    find src/core -type f \\( -path '*.h' -or -path '*.cc' -or -path '*.c' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include <uv.h>;#if COCOAPODS==1\\\n  #include <uv/uv.h>\\\n#else\\\n  #include  <uv.h>\\\n#endif;g'
     find third_party/xxhash  -type f -name xxhash.h -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;@param([^,]*),;@param\\1 ,;g'
-    find src/core/ third_party/xxhash/ third_party/uv/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
+    find src/core/ third_party/xxhash/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
   END_OF_COMMAND
 end
diff --git a/src/objective-c/tests/CoreTests/CoreTests.xcodeproj/project.pbxproj b/src/objective-c/tests/CoreTests/CoreTests.xcodeproj/project.pbxproj
index 9c90705fe25..1d25ab19a64 100644
--- a/src/objective-c/tests/CoreTests/CoreTests.xcodeproj/project.pbxproj
+++ b/src/objective-c/tests/CoreTests/CoreTests.xcodeproj/project.pbxproj
@@ -3,7 +3,7 @@
 	archiveVersion = 1;
 	classes = {
 	};
-	objectVersion = 55;
+	objectVersion = 46;
 	objects = {
 
 /* Begin PBXBuildFile section */
@@ -217,7 +217,7 @@
 				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
 				GCC_WARN_UNUSED_FUNCTION = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 15.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
 				MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
 				MTL_FAST_MATH = YES;
 				ONLY_ACTIVE_ARCH = YES;
@@ -271,7 +271,7 @@
 				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
 				GCC_WARN_UNUSED_FUNCTION = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				IPHONEOS_DEPLOYMENT_TARGET = 15.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				MTL_FAST_MATH = YES;
 				SDKROOT = iphoneos;
@@ -295,7 +295,7 @@
 					"\"${PODS_ROOT}/Headers/Public/abseil\"",
 					"\"${PODS_ROOT}/Headers/Public/gRPC-Core\"",
 				);
-				IPHONEOS_DEPLOYMENT_TARGET = 15.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
 				LD_RUNPATH_SEARCH_PATHS = (
 					"$(inherited)",
 					"@executable_path/Frameworks",
@@ -325,7 +325,7 @@
 					"\"${PODS_ROOT}/Headers/Public/abseil\"",
 					"\"${PODS_ROOT}/Headers/Public/gRPC-Core\"",
 				);
-				IPHONEOS_DEPLOYMENT_TARGET = 15.0;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
 				LD_RUNPATH_SEARCH_PATHS = (
 					"$(inherited)",
 					"@executable_path/Frameworks",
diff --git a/src/objective-c/tests/CoreTests/Podfile b/src/objective-c/tests/CoreTests/Podfile
index 068d2e9e96f..3efbb24b945 100644
--- a/src/objective-c/tests/CoreTests/Podfile
+++ b/src/objective-c/tests/CoreTests/Podfile
@@ -5,6 +5,7 @@ install! 'cocoapods', :deterministic_uuids => false
 GRPC_LOCAL_SRC = '../../../..'
 
 def grpc_deps
+  pod 'BoringSSL-GRPC', :podspec => "#{GRPC_LOCAL_SRC}/src/objective-c"
   pod 'gRPC-Core', :path => GRPC_LOCAL_SRC
   pod 'gRPC-Core/Tests', :path => GRPC_LOCAL_SRC
 end
@@ -13,13 +14,3 @@ target 'CoreTests' do
   platform :ios, '9.0'
   grpc_deps
 end
-
-post_install do |installer|
-  installer.pods_project.targets.each do |target|
-    if target.name == "CoreTests"
-      target.build_configurations.each do |config|
-        config.build_settings['USER_HEADER_SEARCH_PATHS'] = "$(inherited) $(PODS_ROOT)/../#{GRPC_LOCAL_SRC}"
-      end
-    end
-  end
-end
diff --git a/src/objective-c/tests/CoreTests/build_and_run_tests.sh b/src/objective-c/tests/CoreTests/build_and_run_tests.sh
new file mode 100755
index 00000000000..678ab59eb14
--- /dev/null
+++ b/src/objective-c/tests/CoreTests/build_and_run_tests.sh
@@ -0,0 +1,40 @@
+#!/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.
+
+# Don't run this script standalone. Instead, run from the repository root:
+# ./tools/run_tests/run_tests.py -l c++
+
+set -ev
+
+cd "$(dirname "$0")"
+
+echo "TIME:  $(date)"
+
+./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 \
+    -workspace CoreTests.xcworkspace \
+    -scheme CoreTests \
+    -destination name="iPhone 8" \
+    test \
+    | ./verbose_time.sh \
+    | grep -E -v "$XCODEBUILD_FILTER" \
+    | grep -E -v '^$' -
diff --git a/src/objective-c/tests/CoreTests/build_tests.sh b/src/objective-c/tests/CoreTests/build_tests.sh
new file mode 100755
index 00000000000..890955ac913
--- /dev/null
+++ b/src/objective-c/tests/CoreTests/build_tests.sh
@@ -0,0 +1,51 @@
+#!/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.
+
+# Don't run this script standalone. Instead, run from the repository root:
+# ./tools/run_tests/run_tests.py -l objc
+
+set -e
+
+# CocoaPods requires the terminal to be using UTF-8 encoding.
+export LANG=en_US.UTF-8
+
+cd "$(dirname "$0")"
+
+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
+}
+
+# clean the directory
+rm -rf Pods
+rm -rf Tests.xcworkspace
+rm -f Podfile.lock
+rm -rf RemoteTestClientCpp/src
+
+echo "TIME:  $(date)"
+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.
+# TODO(yulinliang): Delete it after solving the issue.
+if [ -d "./Pods/Headers/Public/Protobuf-C++/google/protobuf" ]
+then
+    echo "Protobuf-C++/google/protobuf/ has been imported."
+    number_of_files=$(find Pods/Headers/Public/Protobuf-C++/google/protobuf -name "*.h" | wc -l)
+    echo "The number of header files in Pods/Headers/Public/Protobuf-C++/google/protobuf/ is $number_of_files"
+else
+    echo "Error: Protobuf-C++/google/protobuf/ hasn't been imported."
+fi
diff --git a/src/objective-c/tests/CoreTests/verbose_time.sh b/src/objective-c/tests/CoreTests/verbose_time.sh
new file mode 100755
index 00000000000..05926caea85
--- /dev/null
+++ b/src/objective-c/tests/CoreTests/verbose_time.sh
@@ -0,0 +1,20 @@
+#!/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/templates/gRPC-Core.podspec.template b/templates/gRPC-Core.podspec.template
index 869db66a9b5..c578ca61a34 100644
--- a/templates/gRPC-Core.podspec.template
+++ b/templates/gRPC-Core.podspec.template
@@ -243,8 +243,7 @@
     s.prepare_command = <<-END_OF_COMMAND
       set -e
       find src/core -type f \\( -path '*.h' -or -path '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include <openssl/(.*)>;#if COCOAPODS==1\\\n  #include <openssl_grpc/\\1>\\\n#else\\\n  #include <openssl/\\1>\\\n#endif;g'
-      find src/core -type f \\( -path '*.h' -or -path '*.cc' -or -path '*.c' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include <uv.h>;#if COCOAPODS==1\\\n  #include <uv/uv.h>\\\n#else\\\n  #include  <uv.h>\\\n#endif;g'
       find third_party/xxhash  -type f -name xxhash.h -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;@param([^,]*),;@param\\1 ,;g'
-      find src/core/ third_party/xxhash/ third_party/uv/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
+      find src/core/ third_party/xxhash/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
     END_OF_COMMAND
   end
diff --git a/tools/run_tests/run_tests.py b/tools/run_tests/run_tests.py
index 9fa3b4b950a..efe06680254 100755
--- a/tools/run_tests/run_tests.py
+++ b/tools/run_tests/run_tests.py
@@ -1079,6 +1079,13 @@ class ObjCLanguage(object):
                 shortname='ios-test-cfstream-tests',
                 cpu_cost=1e6,
                 environ=_FORCE_ENVIRON_FOR_WRAPPERS))
+        out.append(
+            self.config.job_spec(
+                ['src/objective-c/tests/CoreTests/build_and_run_tests.sh'],
+                timeout_seconds=60 * 60,
+                shortname='ios-test-core-tests',
+                cpu_cost=1e6,
+                environ=_FORCE_ENVIRON_FOR_WRAPPERS))
         # TODO: replace with run_one_test_bazel.sh when Bazel-Xcode is stable
         out.append(
             self.config.job_spec(['src/objective-c/tests/run_one_test.sh'],