create coverage cache folder before running cache saving and loading #10
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
push: | |
branches-ignore: | |
- updateREADME | |
workflow_dispatch: # Allow manual triggers | |
schedule: | |
- cron: '0 8 * * 4' | |
name: Coverage testing | |
permissions: read-all | |
jobs: | |
test-coverage: | |
runs-on: ubuntu-latest | |
env: | |
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: r-lib/actions/setup-r@v2 | |
with: | |
use-public-rspm: true | |
- uses: r-lib/actions/setup-r-dependencies@v2 | |
with: | |
extra-packages: any::covr, any::xml2 | |
needs: coverage | |
# Create cache directory if it doesn't exist | |
- name: Create coverage cache directory | |
run: | | |
mkdir -p coverage_cache | |
# Restore cached coverage (if available) | |
- name: Restore cached coverage | |
id: cache-coverage | |
uses: actions/cache@v3 | |
with: | |
path: coverage_cache | |
key: coverage-${{ runner.os }}-${{ hashFiles('**/*') }} | |
- name: Test coverage | |
run: | | |
# Set covr verbose mode for detailed debugging | |
options(covr.verbose = TRUE) | |
# Try-catch to capture errors and print traceback | |
tryCatch({ | |
# Check if cache exists | |
if (!file.exists('coverage_cache/covr_cache.rds')) { | |
# No cache found, so calculate coverage | |
cov <- covr::package_coverage( | |
quiet = FALSE, | |
clean = TRUE, # Ensures fresh environment for coverage | |
type = "tests", | |
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") | |
) | |
print("Coverage calculated!") | |
# Save coverage to cache | |
saveRDS(cov, file = 'coverage_cache/covr_cache.rds') | |
print("Coverage saved to cache.") | |
} else { | |
# Load cached coverage | |
cov <- readRDS('coverage_cache/covr_cache.rds') | |
print("Cached coverage loaded successfully!") | |
} | |
# Save coverage results to Cobertura format | |
covr::to_cobertura(cov) | |
# Generate an HTML report for local viewing | |
covr::coveralls(cov, output = "coverage_report") | |
}, error = function(e) { | |
print("Error during package_coverage:") | |
print(e) | |
traceback() | |
stop(e) # Re-raise the error to stop the process | |
}) | |
shell: Rscript {0} | |
# Save cache after the coverage is calculated | |
- name: Save coverage to cache | |
uses: actions/cache@v3 | |
with: | |
path: coverage_cache | |
key: coverage-${{ runner.os }}-${{ hashFiles('**/*') }} | |
# Ensure that the cache is only updated if there were no errors | |
if: success() | |
- uses: codecov/codecov-action@v4 | |
with: | |
file: ./cobertura.xml | |
plugin: noop | |
disable_search: true | |
token: ${{ secrets.CODECOV_TOKEN }} | |
commit_parent: "false" | |
# Optionally, display coverage HTML report if the upload fails | |
- name: Display Coverage Report | |
run: | | |
if [ -f coverage_report/index.html ]; then | |
echo "Coverage report generated. You can view it at:" | |
echo "$(pwd)/coverage_report/index.html" | |
else | |
echo "No coverage report found." | |
fi |