|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Patch Catch2 to be compatible with R CMD check requirements |
| 4 | +# Fixes warnings about abort, sprintf, and srand |
| 5 | + |
| 6 | +cd ~/Documents/testthat/ |
| 7 | + |
| 8 | +CATCH_FILE="inst/include/testthat/vendor/catch.hpp" |
| 9 | + |
| 10 | +if [ ! -f "$CATCH_FILE" ]; then |
| 11 | + echo "ERROR: $CATCH_FILE not found. Run 02-install.sh first." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +echo "Creating backup of catch.hpp..." |
| 16 | +cp "$CATCH_FILE" "$CATCH_FILE.backup" |
| 17 | + |
| 18 | +echo "Patching Catch2 for R compatibility..." |
| 19 | + |
| 20 | +# First, add R header includes after the initial header guard |
| 21 | +echo " - Adding R headers..." |
| 22 | +sed -i '/#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED/a\ |
| 23 | +#ifdef __cplusplus\ |
| 24 | +extern "C" {\ |
| 25 | +#endif\ |
| 26 | +#include <R_ext/Error.h>\ |
| 27 | +#ifdef __cplusplus\ |
| 28 | +}\ |
| 29 | +#endif' "$CATCH_FILE" |
| 30 | + |
| 31 | +# Fix 1: Replace std::abort() with Rf_error() |
| 32 | +# This uses R's native error mechanism instead of terminating the process |
| 33 | +echo " - Replacing abort() with Rf_error()..." |
| 34 | +sed -i 's/std::abort();/Rf_error("Fatal error in test framework");/g' "$CATCH_FILE" |
| 35 | + |
| 36 | +# Fix 2: Replace std::sprintf() with std::snprintf() for safety |
| 37 | +# snprintf is safer and avoids R CMD check warnings |
| 38 | +echo " - Replacing sprintf() with snprintf()..." |
| 39 | +sed -i 's/std::sprintf(buffer, "%.3f", duration);/std::snprintf(buffer, sizeof(buffer), "%.3f", duration);/g' "$CATCH_FILE" |
| 40 | + |
| 41 | +# Fix 3: Comment out std::srand() - R uses its own RNG |
| 42 | +# R packages should use R's RNG via GetRNGstate()/PutRNGstate() |
| 43 | +echo " - Commenting out srand()..." |
| 44 | +sed -i 's/std::srand(config.rngSeed());/\/\/ std::srand(config.rngSeed()); \/\/ Disabled for R compatibility/g' "$CATCH_FILE" |
| 45 | + |
| 46 | +echo "" |
| 47 | +echo "Verifying patches..." |
| 48 | +echo "" |
| 49 | + |
| 50 | +echo "1. Checking abort() was replaced:" |
| 51 | +if grep -q "std::abort()" "$CATCH_FILE"; then |
| 52 | + echo " WARNING: std::abort() still found!" |
| 53 | +else |
| 54 | + echo " No abort() calls found" |
| 55 | +fi |
| 56 | + |
| 57 | +echo "" |
| 58 | +echo "2. Checking sprintf() was replaced:" |
| 59 | +if grep -q "std::sprintf(buffer" "$CATCH_FILE"; then |
| 60 | + echo " WARNING: std::sprintf() still found!" |
| 61 | +else |
| 62 | + echo " No sprintf() calls found" |
| 63 | +fi |
| 64 | + |
| 65 | +echo "" |
| 66 | +echo "3. Checking srand() was commented out:" |
| 67 | +if grep -q "^[[:space:]]*std::srand(config.rngSeed());" "$CATCH_FILE"; then |
| 68 | + echo " WARNING: Uncommented std::srand() still found!" |
| 69 | +else |
| 70 | + echo " srand() has been disabled" |
| 71 | +fi |
| 72 | + |
| 73 | +echo "" |
| 74 | +echo "Patch complete!" |
| 75 | +echo "" |
| 76 | +echo "To test: R CMD INSTALL --preclean . && R CMD check --as-cran ." |
| 77 | +echo "" |
| 78 | +echo "If you need to restore the original, run:" |
| 79 | +echo " mv $CATCH_FILE.backup $CATCH_FILE" |
0 commit comments