|
247 | 247 | required: false |
248 | 248 | type: boolean |
249 | 249 | default: false |
| 250 | + selected-shinytests: |
| 251 | + description: | |
| 252 | + Should shinytests2 tests only run per modified corresponding R file in R/ folder? |
| 253 | + If enabled and there is a module modificated only that shinytest2 file will be tested. |
| 254 | + Might not apply to most packages! Because it replaces skip_if_too_deep(5) to skip_if_too_deep(3). |
| 255 | + Will be ignored if the commit message contains [run-all-tests]. |
| 256 | + required: false |
| 257 | + type: boolean |
| 258 | + default: false |
250 | 259 |
|
251 | 260 | concurrency: |
252 | 261 | group: r-cmd-${{ inputs.concurrency-group }}-${{ github.event.pull_request.number || github.ref }} |
@@ -295,7 +304,6 @@ jobs: |
295 | 304 | |
296 | 305 | if: github.event_name == 'pull_request' |
297 | 306 | with: |
298 | | - ref: ${{ steps.branch-name.outputs.head_ref_branch }} |
299 | 307 | path: ${{ github.event.repository.name }} |
300 | 308 | repository: ${{ github.event.pull_request.head.repo.full_name }} |
301 | 309 | fetch-depth: 0 |
@@ -515,6 +523,88 @@ jobs: |
515 | 523 | with: |
516 | 524 | path: "${{ inputs.additional-caches }}" |
517 | 525 | key: additional-caches-${{ runner.os }} |
| 526 | + steps: |
| 527 | + - name: Get changed files 📃 |
| 528 | + id: changed-files |
| 529 | + if: inputs.selected-shinytests == true |
| 530 | + uses: tj-actions/changed-files@v45 |
| 531 | + with: |
| 532 | + path: ${{ github.event.repository.name }}/${{ inputs.package-subdirectory }} |
| 533 | + base_sha: "main" |
| 534 | + files: | |
| 535 | + tests/testthat/*.R |
| 536 | + R/*.R |
| 537 | +
|
| 538 | + - name: Check only affected modules 🎯 |
| 539 | + if: inputs.selected-shinytests == true |
| 540 | + working-directory: ${{ github.event.repository.name }}/${{ inputs.package-subdirectory }} |
| 541 | + env: |
| 542 | + ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} |
| 543 | + run: | |
| 544 | + # Bash script run |
| 545 | + commit_msg=$( git log -1 --pretty=%B ) |
| 546 | +
|
| 547 | + # Set default TESTING_DEPTH |
| 548 | + td=$TESTING_DEPTH |
| 549 | + if [ -z "$td" ] |
| 550 | + then { |
| 551 | + echo "No TESTING_DEPTH default." |
| 552 | + echo "Setting TESTING_DEPTH=5" |
| 553 | + echo "TESTING_DEPTH=5" >> "$GITHUB_ENV" |
| 554 | + td=5 |
| 555 | + } fi |
| 556 | +
|
| 557 | + echo "Commit msg is: ${commit_msg}" |
| 558 | + # Exit early if tag is on commit message even if it set to true |
| 559 | + test_all=$( echo "${commit_msg}" | grep -zvF "[run-all-tests]" | tr -d '\0') |
| 560 | +
|
| 561 | + if [ -z "$test_all" ] |
| 562 | + then { |
| 563 | + echo "Last commit message forces to test everything." |
| 564 | + echo "Using TESTING_DEPTH=$td" |
| 565 | + echo "TESTING_DEPTH=$td" >> "$GITHUB_ENV" |
| 566 | + exit 0 |
| 567 | + } fi |
| 568 | +
|
| 569 | + test_dir="tests/testthat/" |
| 570 | +
|
| 571 | + if [ -z "$ALL_CHANGED_FILES" ] |
| 572 | + then { |
| 573 | + echo "No R files affected: test everything." |
| 574 | + echo Using "TESTING_DEPTH=$td" |
| 575 | + echo "TESTING_DEPTH=$td" >> "$GITHUB_ENV" |
| 576 | + exit 0 |
| 577 | + } fi |
| 578 | +
|
| 579 | + # Loop through each modified file and determine which tests to run |
| 580 | + for file in $ALL_CHANGED_FILES; do |
| 581 | +
|
| 582 | + echo "Check for $file." |
| 583 | +
|
| 584 | + # Extract the base name of the file, examples: |
| 585 | + # tests/testthat/test-shinytest2-foo.R -> foo |
| 586 | + # R/foo.R -> foo |
| 587 | + base_name=$(basename "$file" .R | sed s/test-shinytest2-//g) |
| 588 | + # Find matching test files (parenthesis to not match arguments) |
| 589 | + test_files=$(grep -l "$base_name(" "$test_dir"test-shinytest2-*.R || echo "") |
| 590 | + # Modify in place so that only modified modules are tested. |
| 591 | + if [ -z "$test_files" ]; |
| 592 | + then { |
| 593 | + git restore $test_dir |
| 594 | + echo "Run all tests: Helpers modifications detected." |
| 595 | + TESTING_DEPTH="$td"; |
| 596 | + break; |
| 597 | + } else { |
| 598 | + sed -i 's/skip_if_too_deep(5)/skip_if_too_deep(3)/g' "$test_files" |
| 599 | + TESTING_DEPTH=3 |
| 600 | + echo "TESTING_DEPTH=3" >> "$GITHUB_ENV" |
| 601 | + echo "Testing with shinytest2 only for $test_files"; |
| 602 | + } fi |
| 603 | + done |
| 604 | +
|
| 605 | + echo "At the end, using TESTING_DEPTH=${TESTING_DEPTH}" |
| 606 | + echo "TESTING_DEPTH=${TESTING_DEPTH}" >> "$GITHUB_ENV" |
| 607 | + shell: bash |
518 | 608 |
|
519 | 609 | - name: Build R package 🏗 |
520 | 610 | run: | |
|
0 commit comments