|
220 | 220 | required: false |
221 | 221 | type: boolean |
222 | 222 | default: false |
| 223 | + selected-shinytests: |
| 224 | + description: | |
| 225 | + Should shinytests2 tests only run per modified corresponding R file in R/ folder? |
| 226 | + If enabled and there is a module modificated only that shinytest2 file will be tested. |
| 227 | + Might not apply to most packages! Because it replaces skip_if_too_deep(5) to skip_if_too_deep(3). |
| 228 | + Will be ignored if the commit message contains [run-all-tests]. |
| 229 | + required: false |
| 230 | + type: boolean |
| 231 | + default: false |
223 | 232 |
|
224 | 233 | concurrency: |
225 | 234 | group: r-cmd-${{ inputs.concurrency-group }}-${{ github.event.pull_request.number || github.ref }} |
@@ -268,7 +277,6 @@ jobs: |
268 | 277 | |
269 | 278 | if: github.event_name == 'pull_request' |
270 | 279 | with: |
271 | | - ref: ${{ steps.branch-name.outputs.head_ref_branch }} |
272 | 280 | path: ${{ github.event.repository.name }} |
273 | 281 | repository: ${{ github.event.pull_request.head.repo.full_name }} |
274 | 282 | fetch-depth: 0 |
@@ -484,6 +492,89 @@ jobs: |
484 | 492 | with: |
485 | 493 | path: "${{ inputs.additional-caches }}" |
486 | 494 | key: additional-caches-${{ runner.os }} |
| 495 | + steps: |
| 496 | + - name: Get changed files 📃 |
| 497 | + id: changed-files |
| 498 | + if: inputs.selected-shinytests == true |
| 499 | + # v45.0.8 |
| 500 | + uses: tj-actions/changed-files@a284dc1814e3fd07f2e34267fc8f81227ed29fb8 |
| 501 | + with: |
| 502 | + path: ${{ github.event.repository.name }}/${{ inputs.package-subdirectory }} |
| 503 | + base_sha: "main" |
| 504 | + files: | |
| 505 | + tests/testthat/*.R |
| 506 | + R/*.R |
| 507 | +
|
| 508 | + - name: Check only affected modules 🎯 |
| 509 | + if: inputs.selected-shinytests == true |
| 510 | + working-directory: ${{ github.event.repository.name }}/${{ inputs.package-subdirectory }} |
| 511 | + env: |
| 512 | + ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
| 513 | + run: | |
| 514 | + # Bash script run |
| 515 | + commit_msg=$( git log -1 --pretty=%B ) |
| 516 | +
|
| 517 | + # Set default TESTING_DEPTH |
| 518 | + td=$TESTING_DEPTH |
| 519 | + if [ -z "$td" ] |
| 520 | + then { |
| 521 | + echo "No TESTING_DEPTH default." |
| 522 | + echo "Setting TESTING_DEPTH=5" |
| 523 | + echo "TESTING_DEPTH=5" >> "$GITHUB_ENV" |
| 524 | + td=5 |
| 525 | + } fi |
| 526 | +
|
| 527 | + echo "Commit msg is: ${commit_msg}" |
| 528 | + # Exit early if tag is on commit message even if it set to true |
| 529 | + test_all=$( echo "${commit_msg}" | grep -zvF "[run-all-tests]" | tr -d '\0') |
| 530 | +
|
| 531 | + if [ -z "$test_all" ] |
| 532 | + then { |
| 533 | + echo "Last commit message forces to test everything." |
| 534 | + echo "Using TESTING_DEPTH=$td" |
| 535 | + echo "TESTING_DEPTH=$td" >> "$GITHUB_ENV" |
| 536 | + exit 0 |
| 537 | + } fi |
| 538 | +
|
| 539 | + test_dir="tests/testthat/" |
| 540 | +
|
| 541 | + if [ -z "$ALL_CHANGED_FILES" ] |
| 542 | + then { |
| 543 | + echo "No R files affected: test everything." |
| 544 | + echo Using "TESTING_DEPTH=$td" |
| 545 | + echo "TESTING_DEPTH=$td" >> "$GITHUB_ENV" |
| 546 | + exit 0 |
| 547 | + } fi |
| 548 | +
|
| 549 | + # Loop through each modified file and determine which tests to run |
| 550 | + for file in $ALL_CHANGED_FILES; do |
| 551 | +
|
| 552 | + echo "Check for $file." |
| 553 | +
|
| 554 | + # Extract the base name of the file, examples: |
| 555 | + # tests/testthat/test-shinytest2-foo.R -> foo |
| 556 | + # R/foo.R -> foo |
| 557 | + base_name=$(basename "$file" .R | sed s/test-shinytest2-//g) |
| 558 | + # Find matching test files (parenthesis to not match arguments) |
| 559 | + test_files=$(grep -l "$base_name(" "$test_dir"test-shinytest2-*.R || echo "") |
| 560 | + # Modify in place so that only modified modules are tested. |
| 561 | + if [ -z "$test_files" ]; |
| 562 | + then { |
| 563 | + git restore $test_dir |
| 564 | + echo "Run all tests: Helpers modifications detected." |
| 565 | + TESTING_DEPTH="$td"; |
| 566 | + break; |
| 567 | + } else { |
| 568 | + sed -i 's/skip_if_too_deep(5)/skip_if_too_deep(3)/g' "$test_files" |
| 569 | + TESTING_DEPTH=3 |
| 570 | + echo "TESTING_DEPTH=3" >> "$GITHUB_ENV" |
| 571 | + echo "Testing with shinytest2 only for $test_files"; |
| 572 | + } fi |
| 573 | + done |
| 574 | +
|
| 575 | + echo "At the end, using TESTING_DEPTH=${TESTING_DEPTH}" |
| 576 | + echo "TESTING_DEPTH=${TESTING_DEPTH}" >> "$GITHUB_ENV" |
| 577 | + shell: bash |
487 | 578 |
|
488 | 579 | - name: Build R package 🏗 |
489 | 580 | run: | |
|
0 commit comments