Skip to content

Commit 9bff376

Browse files
authored
Run shinytests2 only if the modules are affected (#273)
# Pull Request Fixes: insightsengineering/coredev-tasks#580 This should make testing on PR to main a lot faster by just running test of the modules affected. TODO: - [x] Check if modifying a general files trigger all tests - [ ] Be sensible to the branch name and merging strategy: 125_feat@dev@main should test and compare vs `@dev` ? - [x] Check it works on a package without modules (teal.code for example) - [x] Check testing depth change doesn't affect other other steps.
1 parent 551beb0 commit 9bff376

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

.github/workflows/build-check-install.yaml

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ on:
247247
required: false
248248
type: boolean
249249
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
250259

251260
concurrency:
252261
group: r-cmd-${{ inputs.concurrency-group }}-${{ github.event.pull_request.number || github.ref }}
@@ -295,7 +304,6 @@ jobs:
295304
uses: actions/[email protected]
296305
if: github.event_name == 'pull_request'
297306
with:
298-
ref: ${{ steps.branch-name.outputs.head_ref_branch }}
299307
path: ${{ github.event.repository.name }}
300308
repository: ${{ github.event.pull_request.head.repo.full_name }}
301309
fetch-depth: 0
@@ -515,6 +523,88 @@ jobs:
515523
with:
516524
path: "${{ inputs.additional-caches }}"
517525
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
518608

519609
- name: Build R package 🏗
520610
run: |

0 commit comments

Comments
 (0)