From ae9d85674d55c287d98317d75cb8601d3d168cfe Mon Sep 17 00:00:00 2001
From: Marko Justinek <marko.justinek@gmail.com>
Date: Fri, 26 Apr 2024 13:21:36 +1000
Subject: [PATCH] fix: Add workflow_call config for secrets

---
 .github/workflows/build_test.yml         |  2 ++
 .github/workflows/test_macos14_arm64.yml |  8 ++++++--
 Support/build_test                       | 15 +++++++++------
 Support/utils.sh                         | 16 ++++++++++++++++
 4 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml
index 178a3fc..0a6dcd8 100644
--- a/.github/workflows/build_test.yml
+++ b/.github/workflows/build_test.yml
@@ -51,6 +51,8 @@ jobs:
     name: "🤖 Unit tests"
     needs: [sharedInputs, buildFFI]
     uses: ./.github/workflows/test_macos14_arm64.yml
+    secrets:
+      codecov_token: ${{ secrets.CODECOV_TOKEN }}
     with:
       rust-target-path: ${{ needs.sharedInputs.outputs.rust-target-path }}
       binaries-path: ${{ needs.sharedInputs.outputs.binaries-path }}
diff --git a/.github/workflows/test_macos14_arm64.yml b/.github/workflows/test_macos14_arm64.yml
index 88d4df3..42a4b0a 100644
--- a/.github/workflows/test_macos14_arm64.yml
+++ b/.github/workflows/test_macos14_arm64.yml
@@ -15,6 +15,10 @@ on:
       cache-restore-key:
         required: true
         type: string
+    secrets:
+      codecov_token:
+        required: true
+  workflow_dispatch:
 
 jobs:
   testMacOS14:
@@ -64,9 +68,9 @@ jobs:
 
       - name: "⬆️  Upload coverage reports"
         uses: codecov/codecov-action@v4
-        env:
-          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
         with:
+          token: ${{ secrets.codecov_token }}
+          file: "${{ env.SCHEME }}-TestResultBundle.xcresult"
           fail_ci_if_error: true
           flags: unittests
           name: PactSwiftMockServer
diff --git a/Support/build_test b/Support/build_test
index d0d36fc..86f307e 100755
--- a/Support/build_test
+++ b/Support/build_test
@@ -26,7 +26,7 @@ source "$SOURCE_DIR/utils.sh"
 
 # Properties
 SCHEME_TARGET_PAIRS=()
-BUILD_PATH=".build"
+BUILD_PATH="$SOURCE_DIR/../.build"
 
 ###############
 # "private"
@@ -74,15 +74,18 @@ else
   done
 fi
 
-# Remove build data by previous runs
-echo "ℹ️  Cleanning up build data of previous runs in '$BUILD_PATH'"
-executeCommand "rm -fr $BUILD_PATH"
-
 # Run tests
 for INDEX in "${SCHEME_TARGET_PAIRS[@]}"; do
   SCHEME="${INDEX%%:::*}"
   TARGET="${INDEX##*::}"
+  RESULT_BUNDLE_PATH="$BUILD_PATH/artifacts/$SCHEME-TestResultBundle.xcresult"
+
+  if [ $(folderExists "$RESULT_BUNDLE_PATH") == true ]; then
+    # Remove build data by previous runs
+    echo "ℹ️  Removing results bundle from previous runs..."
+    executeCommand "rm -fr $RESULT_BUNDLE_PATH"
+  fi
 
   echo "🧪  Running tests"
-  executeCommand "xcodebuild clean test -project \"PactSwiftMockServer.xcodeproj\" -scheme \"$SCHEME\" -destination \"$TARGET\" -enableCodeCoverage YES -derivedDataPath \"$BUILD_PATH/DerivedData\" GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES | xcbeautify"
+  executeCommand "xcodebuild -project \"PactSwiftMockServer.xcodeproj\" -scheme \"$SCHEME\" -destination \"$TARGET\" -enableCodeCoverage YES -derivedDataPath \"$BUILD_PATH/DerivedData\" -resultBundlePath \"$RESULT_BUNDLE_PATH\" GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES build test | xcbeautify"
 done
diff --git a/Support/utils.sh b/Support/utils.sh
index a80f1ea..d86dd19 100644
--- a/Support/utils.sh
+++ b/Support/utils.sh
@@ -29,3 +29,19 @@ function executeCommand {
     eval "$COMMAND"
   fi
 }
+
+function folderExists {
+  if [ ! -d "$1" ]; then
+    echo false
+  else
+    echo true
+  fi
+}
+
+function fileExists {
+  if [ ! -f "$1" ]; then
+    echo false
+  else
+    echo true
+  fi
+}