tools/audit-mutations.R

# Targeted fault injection, each tested in a fresh R process.
# Usage: Rscript tools/audit-mutations.R package_directory scratch_directory
# Requires development dependencies testthat and pkgload. Never edits the source.
args <- commandArgs(trailingOnly = TRUE)
stopifnot(length(args) == 2L)
root <- normalizePath(args[1], mustWork = TRUE)
dir.create(args[2], recursive = TRUE, showWarnings = FALSE)
output <- normalizePath(args[2], mustWork = TRUE)
stopifnot(!startsWith(paste0(output, "/"), paste0(root, "/")))
mutations <- list(
  control = NULL,
  covariance_denominator = c("R/core.R", "  if (any(!is.finite(c_bl))) {",
    "  c_bl <- c_bl * (nrow(state$draws) - 1) / nrow(state$draws)\n  if (any(!is.finite(c_bl))) {"),
  finite_sample_factor = c("R/core.R", "v <- (k / (k - 1L)) * scatter", "v <- scatter"),
  cluster_centering = c("R/core.R",
    'tcrossprod(sweep(state$unit_cov, 1L, rowMeans(state$unit_cov), "-"))',
    'tcrossprod(state$unit_cov)'),
  chain_order = c("R/core.R", "chain_rows <- lapply(ids, function(id) which(chain_id == id))",
    "chain_rows <- lapply(seq_along(ids), function(j) seq.int(j, s, by = length(ids)))"),
  vector_row_labels = c("R/core.R", 'dimnames = list(names(transformed), "quantity")',
    'dimnames = list(NULL, "quantity")'),
  covariance_type = c("R/methods.R", 'if (type == "ij") object$vcov else object$posterior_vcov',
    'object$posterior_vcov'),
  block_row_reference = c("R/core.R", "reference = row_ids", "reference = rownames(draws)"),
  factor_NA_level = c("R/core.R", "      (is.factor(x) && anyNA(as.character(x))) ||\n", ""),
  first_scatter_merge = c("R/core.R", "if (state$count > 0L) {", "if (TRUE) {")
)
runner <- file.path(output, "runner.R")
writeLines(c('args <- commandArgs(trailingOnly = TRUE)',
  'testthat::set_max_fails(10000)',
  'result <- testthat::test_local(args[1], filter = "core|bugs|methods-diagnostics|v03|validation",',
  '  reporter = "silent", stop_on_failure = FALSE)',
  'table <- as.data.frame(result)',
  'table <- table[, !vapply(table, is.list, logical(1)), drop = FALSE]',
  'write.csv(table, args[2], row.names = FALSE)',
  'cat("failed assertions:", sum(table$failed), "; test errors:", sum(table$error), "\\n")'), runner)
results <- list()
for (name in names(mutations)) {
  dest <- file.path(output, name)
  if (dir.exists(dest)) stop("Use a fresh scratch directory: ", dest)
  dir.create(dest)
  stopifnot(all(file.copy(list.files(root, full.names = TRUE, all.files = TRUE, no.. = TRUE),
                         dest, recursive = TRUE)))
  mutation <- mutations[[name]]
  if (!is.null(mutation)) {
    file <- file.path(dest, mutation[1])
    original <- paste(readLines(file, warn = FALSE), collapse = "\n")
    locations <- gregexpr(mutation[2], original, fixed = TRUE)[[1]]
    stopifnot(length(locations) == 1L, locations[1] > 0)
    writeLines(sub(mutation[2], mutation[3], original, fixed = TRUE), file)
  }
  csv <- file.path(output, paste0(name, ".csv"))
  log <- file.path(output, paste0(name, ".log"))
  status <- system2(file.path(R.home("bin"), "Rscript"),
    c(shQuote(runner), shQuote(dest), shQuote(csv)),
    stdout = log, stderr = log)
  if (status != 0L || !file.exists(csv)) stop("Mutation runner failed: ", name)
  tab <- read.csv(csv)
  failures <- sum(tab$failed) + sum(tab$error)
  detected <- failures > 0
  if (name == "control") stopifnot(!detected) else stopifnot(detected)
  results[[length(results) + 1L]] <- data.frame(mutation = name, failed_assertions = sum(tab$failed),
    test_errors = sum(tab$error), detected = detected, expected_failure = name != "control")
  cat(name, if (name == "control") "passed cleanly" else "detected", "\n")
}
out <- do.call(rbind, results)
write.csv(out, file.path(output, "mutation-summary.csv"), row.names = FALSE)
cat("Clean control passed; all nine prespecified faults were detected.\n")

Try the IJSE package in your browser

Any scripts or data that you put into this service are public.

IJSE documentation built on Oct. 22, 2026, 5:18 p.m.