Skip to content

test: skip image snapshots when raster is not installed (fixes the nosuggests leg) - #46

Merged
eliotmcintire merged 1 commit into
developmentfrom
fix/snapshot-tests-need-raster
Sep 4, 2026
Merged

test: skip image snapshots when raster is not installed (fixes the nosuggests leg)#46
eliotmcintire merged 1 commit into
developmentfrom
fix/snapshot-tests-need-raster

Conversation

@eliotmcintire

@eliotmcintire eliotmcintire commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

What

skip_if_not_installed("raster") in the nine test_that() blocks of tests/testthat/test-Plot-snapshots.R that take image snapshots, plus a header comment explaining why.

Why

The reference images in _snaps/Plot-snapshots/ were recorded with the full Suggests set installed, and they are not portable to a run without it — not because the rendering differs, but because the names do.

Several tests build a rasts list and then double its length:

if (requireNamespace("raster", quietly = TRUE)) {
  rasterVec <- seq_along(rasts) + length(rasts)
  rasts[rasterVec] <- Map(...)          # SpatRaster -> RasterLayer twins
}

Snapshot names come from fn() (helper-testInit.R:85), which reads a running counter incremented once per oses entry per plot. Drop raster and every one of those lists halves, so the counter runs shorter and every snapshot name after the first requireNamespace("raster") block lines up against a different plot.

test_that("internal functions in Plot") has a second, sharper version of the same assumption:

val <- (testNum - 1) %% (length(rasts) / 2) + 1

The / 2 hard-codes the doubling. Without raster, length(rasts) is 4, so val cycles 1, 2, 1, 2 — plots 3 and 4 are never rendered, and their reference images are compared against plots 1 and 2.

Effect

This is precisely the _R_CHECK_DEPENDS_ONLY_ (nosuggests) leg that the shared PredictiveEcology/actions R-CMD-check matrix runs. It reports FAIL 11, and all eleven are in this one file:

Location Count Cause
:189 "internal functions in Plot" 2 the length(rasts) / 2 assumption
:327 "Plot 2 is not error-free" 6 counter shift
:396 "Plot 2 is not error-free" 1 counter shift
:703 "Plot lists" 1 missing Linux fixture (fixed in #45)
:612 "rePlot doesn't work" 1 duplicate snapshot name (fixed in #45)

Maintaining a second full set of reference images for a Suggests-free configuration would be a lot of pixels for no extra signal, so the image comparisons are skipped there instead. The nosuggests leg keeps its actual purpose: the package loads and its non-image tests pass without Suggests.

Verification

The nosuggests leg does not exist on development's current workflow — it only appears once #44 lands — so this PR's own CI cannot exercise it. What it can show is that the guard is a no-op where raster is installed, and it is: run 33897721026 reports FAIL 2 | SKIP 3 | PASS 173 on ubuntu-latest (release) — byte for byte the same counts, the same two failures and the same three skips as the pre-change baseline. No test was silently skipped.

Those two remaining failures are the ones #45 fixes; this branch is deliberately independent of it.

Note

This does not fix the length(rasts) / 2 bug itself — with the guard in place raster is always present in these tests, so the expression is always correct. Its sibling at :302/:321 already uses the safe (1 + hasRasterLayer) idiom if you would rather make them match; left alone here to keep the change surgical.

🤖 Generated with Claude Code

https://claude.ai/code/session_011kpoQV6nNqcknk16R3RzBB

The image snapshots in test-Plot-snapshots.R were recorded with the full
Suggests set installed, and they are not portable to a run without it --
not because the rendering differs, but because the *names* do.

Several tests build a `rasts` list and then double its length inside
`if (requireNamespace("raster", quietly = TRUE))`. Snapshot file names come
from `fn()` (helper-testInit.R), which pulls from a running `counter` over
those lists. Drop `raster` and every list shortens, so each name after the
first raster block lines up against a different plot. "internal functions
in Plot" additionally computes `val <- (testNum - 1) %% (length(rasts) / 2) + 1`,
hard-coding the doubling, so without raster it renders plots 1 and 2 twice
and never renders 3 or 4.

This is exactly the `_R_CHECK_DEPENDS_ONLY_` leg that PredictiveEcology/actions'
shared R-CMD-check matrix runs: 11 failures there, all in this file, all of
them a reference image compared against the wrong plot.

Maintaining a second set of reference images for that configuration would be
a lot of pixels for no extra signal, so skip the image comparisons when
raster is absent. The nosuggests leg keeps its real purpose -- the package
loads and its non-image tests pass without Suggests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011kpoQV6nNqcknk16R3RzBB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant