Browse Source

Add coverage test workflow. (#7231)

This PR is a prerequisite for generating code coverage report.

get_updated_files.sh will help get updated files to determine which sdk workflow to trigger.
pod_test_code_coverage_report.sh will run pod tests and create xcresult bundles, with code coverage report, for SDKs
The workflow test_coverage.yml will determine testing jobs to trigger and all xcresult bundles will be uploaded as artifacts on Github Actions.

An upcoming PR will fetch these artifacts and combine coverage reports into a json file and then an API request will be sent with the json file to the Metrics Service, which will help generate a report.
Gran 5 years ago
parent
commit
199ef50b3a

+ 79 - 0
.github/workflows/test_coverage.yml

@@ -0,0 +1,79 @@
+name: test_coverage
+
+on: [pull_request]
+#run specific jobs when specific files are updated.
+#https://github.community/t/how-to-execute-jobs-if-folder-updated-recursive/117344/5
+
+jobs:
+  check:
+    name: Check changed files
+    outputs:
+      database_run_job: ${{ steps.check_files.outputs.database_run_job }}
+      functions_run_job: ${{ steps.check_files.outputs.functions_run_job }}
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v2
+        with:
+          fetch-depth: 0
+      - name: check files
+        id: check_files
+        env:
+          pr_branch: ${{ github.event.pull_request.head.ref }}
+        run: ./scripts/code_coverage_report/get_updated_files.sh
+  pod-lib-lint-database:
+    # Don't run on private repo unless it is a PR.
+    needs: check
+    if: github.repository == 'Firebase/firebase-ios-sdk' && needs.check.outputs.database_run_job == 'true'
+    runs-on: macOS-latest
+
+    strategy:
+      matrix:
+        target: [ios, tvos, macos]
+    steps:
+    - uses: actions/checkout@v2
+    - name: Setup Bundler
+      run: scripts/setup_bundler.sh
+    - name: Build and test
+      env:
+        SDK: database
+      run: ./scripts/code_coverage_report/pod_test_code_coverage_report.sh FirebaseDatabase "${{ matrix.target }}"
+    - uses: actions/upload-artifact@v2
+      with:
+        name: database-codecoverage
+        path: /Users/runner/*.xcresult
+
+  pod-lib-lint-functions:
+    # Don't run on private repo unless it is a PR.
+    needs: check
+    if: github.repository == 'Firebase/firebase-ios-sdk' && needs.check.outputs.functions_run_job == 'true'
+    runs-on: macOS-latest
+
+    strategy:
+      matrix:
+        target: [ios, tvos, macos]
+    steps:
+    - uses: actions/checkout@v2
+    - name: Setup Bundler
+      run: scripts/setup_bundler.sh
+    - name: Build and test
+      run: ./scripts/code_coverage_report/pod_test_code_coverage_report.sh FirebaseFunctions "${{ matrix.target }}"
+    - uses: actions/upload-artifact@v2
+      with:
+        name: functions-codecoverage
+        path: /Users/runner/*.xcresult
+
+  create_report:
+    needs: [pod-lib-lint-functions, pod-lib-lint-database]
+    if: always()
+    runs-on: macOS-latest
+    steps:
+      - uses: actions/download-artifact@v2
+        id: download
+        with:
+          path: /Users/runner/test
+      - name: display results
+        run: |
+          if [ -d "${{steps.download.outputs.download-path}}" ]; then
+          find "/Users/runner/test" -print -regex ".*/.*\.xcresult" -exec xcrun xccov view --report {} \;
+          fi

+ 60 - 0
scripts/code_coverage_report/get_updated_files.sh

@@ -0,0 +1,60 @@
+# Copyright 2021 Google LLC
+#
+# 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.
+
+DATABASE_PATHS=("FirebaseDatabase.*" \
+  ".github/workflows/database\\.yml" \
+  "Example/Database/" \
+  "Interop/Auth/Public/\.\*.h")
+FUNCTIONS_PATHS=("Functions.*" \
+  ".github/workflows/functions\\.yml" \
+  "Interop/Auth/Public/\.\*.h" \
+  "FirebaseMessaging/Sources/Interop/\.\*.h")
+
+# Set default flag variables which will determine if an sdk workflow will be triggererd.
+echo "::set-output name=database_run_job::false"
+echo "::set-output name=functions_run_job::false"
+
+# Get most rescent ancestor commit.
+common_commit=$(git merge-base remotes/origin/${pr_branch} remotes/origin/master)
+echo "The common commit is ${common_commit}."
+
+# List changed file from the base commit. This is generated by comparing the
+# head of the branch and the common commit from the master branch.
+echo "=============== list changed files ==============="
+cat < <(git diff --name-only $common_commit remotes/origin/${pr_branch})
+
+echo "============= paths of changed files ============="
+git diff --name-only $common_commit remotes/origin/${pr_branch} > updated_files.txt
+
+# Loop over updated files if their path match patterns then flags to trigger
+# SDK pod test workflow will be set to true.
+while IFS= read -r file
+do
+  for path in "${DATABASE_PATHS[@]}"
+  do
+    if [[ "${file}" =~ $path ]]; then
+      echo "This file is updated under the path, ${path}"
+      echo "::set-output name=database_run_job::true"
+      break
+    fi
+  done
+  for path in "${FUNCTIONS_PATHS[@]}"
+  do
+    if [[ "${file}" =~ $path ]]; then
+      echo "This file is updated under the path, ${path}"
+      echo "::set-output name=functions_run_job::true"
+      break
+    fi
+  done
+done < updated_files.txt

+ 24 - 0
scripts/code_coverage_report/pod_test_code_coverage_report.sh

@@ -0,0 +1,24 @@
+# Copyright 2021 Google LLC
+#
+# 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
+#
+#     https://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.
+
+SDK="$1"
+platform="$2"
+default_output_path="/Users/runner/${SDK}-${platform}.xcresult"
+output_path="${3:-default_output_path}"
+if [ -d "/Users/runner/Library/Developer/Xcode/DerivedData" ]; then
+rm -r /Users/runner/Library/Developer/Xcode/DerivedData/*
+fi
+scripts/third_party/travis/retry.sh scripts/pod_lib_lint.rb "${SDK}".podspec --platforms="${platform}" --test-specs=unit
+find /Users/runner/Library/Developer/Xcode/DerivedData -type d -regex ".*/.*\.xcresult" -execdir cp -R '{}' "${output_path}" \;
+