diff --git a/.gitea/workflows/r-test.yml b/.gitea/workflows/r-test.yml index ddbeccb..91bbb20 100644 --- a/.gitea/workflows/r-test.yml +++ b/.gitea/workflows/r-test.yml @@ -1,24 +1,77 @@ -name: R tests +name: R test on: [push, pull_request] jobs: - test: + r-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + # Detect an R package (DESCRIPTION with Package:) or a tests tree + - name: Detect R tests + id: detect + shell: bash + run: | + set -euo pipefail + # Look for an R package + PKG_DIR=$(git ls-files '**/DESCRIPTION' | while read -r f; do + if grep -q '^Package:' "$f"; then dirname "$f"; break; fi + done || true) + + if [ -n "${PKG_DIR:-}" ]; then + echo "found=true" >> "$GITHUB_OUTPUT" + echo "kind=package" >> "$GITHUB_OUTPUT" + echo "dir=$PKG_DIR" >> "$GITHUB_OUTPUT" + echo "Detected R package at $PKG_DIR" + exit 0 + fi + + # Otherwise, look for a tests/testthat directory + CANDIDATE=$(find . -type d -path '*/tests/testthat' | head -n1 || true) + if [ -n "${CANDIDATE:-}" ]; then + # project dir = parent of 'tests' + PROJ_DIR="$(dirname "$(dirname "$CANDIDATE")")" + echo "found=true" >> "$GITHUB_OUTPUT" + echo "kind=tests" >> "$GITHUB_OUTPUT" + echo "dir=$PROJ_DIR" >> "$GITHUB_OUTPUT" + echo "Detected testthat directory at $CANDIDATE (project: $PROJ_DIR)" + exit 0 + fi + + echo "found=false" >> "$GITHUB_OUTPUT" + echo "No R package or tests/testthat found; skipping." + - name: Set up R + if: steps.detect.outputs.found == 'true' uses: r-lib/actions/setup-r@v2 with: use-public-rspm: true # fast Linux binaries - name: Install dependencies + if: steps.detect.outputs.found == 'true' uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: any::testthat # or list others you need + extra-packages: any::testthat cache-version: 1 - - name: Run tests + - name: Run tests (R package) + if: steps.detect.outputs.found == 'true' && steps.detect.outputs.kind == 'package' + working-directory: ${{ steps.detect.outputs.dir }} shell: Rscript {0} run: | - testthat::test_local() \ No newline at end of file + testthat::test_local() + + - name: Run tests (non-package testthat dir) + if: steps.detect.outputs.found == 'true' && steps.detect.outputs.kind == 'tests' + working-directory: ${{ steps.detect.outputs.dir }} + shell: Rscript {0} + run: | + # Optionally source code from an R/ folder if you have one: + if (dir.exists("R")) { + testthat::source_dir("R", recurse = TRUE) + } + testthat::test_dir("tests/testthat") + + - name: Skip (no R tests found) + if: steps.detect.outputs.found != 'true' + run: echo "No R package or tests/testthat directory found; skipping R tests."