Skip to content

Add :load hook fired on cached fixture access#198

Merged
ahaverbuch merged 1 commit into
mainfrom
add-load-hook
Apr 29, 2026
Merged

Add :load hook fired on cached fixture access#198
ahaverbuch merged 1 commit into
mainfrom
add-load-hook

Conversation

@aaronrosenthal
Copy link
Copy Markdown
Contributor

@aaronrosenthal aaronrosenthal commented Apr 29, 2026

Motivation

Cached fixtures can hold values whose validity depends on real-world time (e.g., a start_date aligned to a calendar payroll boundary). Today fixtury exposes :execution and :time_access triggers, which only fire on cache miss / time access — there is no callback when a cached fixture is restored via the locator on each test run, so downstream consumers have no clean place to refresh stale per-test state.

This adds a :load trigger that fires inside Fixtury::Store#get when a cached reference is successfully resolved by the locator, passing (definition, value) to hook callbacks. It deliberately does not fire on the cache-miss build path (:execution already covers that) or on the recovery rebuild path when the locator returns nil.

Intended consumer usage

::Fixtury.hooks.on(:load) do |dfn, value|
  ProspectStartDateBuffer.apply!(value) if value.is_a?(::Prospect)
end

Implementation notes

Store#get calls maybe_load_isolation_dependencies before the cache-hit check, and that path can recursively call get for the same fixture (when the fixture's isolation_key defaults to its own pathname). That recursive call builds and stores the reference, so the outer call would otherwise observe a synthetic cache hit on the very first get. To distinguish a true cache hit from this build-via-isolation-recursion case, we capture the reference state before isolation-dependency loading and only fire :load when a real reference existed beforehand.

Bumps version to 2.4.0 (additive, no breaking changes).

Test plan

  • bundle exec rake test — 74 runs, 171 assertions, 0 failures
  • :load fires on cache hit
  • :load does not fire on initial cache-miss build
  • :load does not fire when locator returns nil (recovery rebuild)
  • Multiple :load hooks fire in registration order

Note

Medium Risk
Touches the core fixture retrieval path (Store#get) and introduces a new callback that can run user code on cache hits, which could affect test behavior/performance if misused.

Overview
Adds a new :load hook that fires when Fixtury::Store#get successfully resolves a cached fixture via the locator, passing (definition, value) for consumers to refresh per-test state.

Updates Store#get to avoid false positives caused by isolation-group dependency loading (only firing when a real reference existed before dependency loading) and to skip the hook when the locator returns nil and a rebuild occurs; adds tests covering these cases and bumps the gem version to 2.4.0.

Reviewed by Cursor Bugbot for commit 0c9168f. Bugbot is set up for automated code reviews on this repo. Configure here.

Cached fixtures can hold values whose validity depends on real-world
time, and downstream consumers currently have no clean place to refresh
them per test run. The existing :execution and :time_access triggers
only fire on cache miss / time access, leaving cache-hit reads
unobservable. This adds a :load hook that fires inside Store#get when a
cached reference is successfully resolved by the locator, passing
(definition, value) so consumers can perform per-test refresh work
(e.g. updating a stale calendar-derived attribute on a cached record).

The hook only fires on a true cache hit — not on the initial build (the
recursive isolation-load path can repopulate the reference within the
same get call, so we capture pre-existing reference state before
isolation-dependency loading) and not on the recovery rebuild path when
the locator returns nil.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@socket-security
Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedfixtury@​2.3.0 ⏵ 2.4.0100 +310090 -10100100

View full report

@ahaverbuch ahaverbuch merged commit 05dba5d into main Apr 29, 2026
9 checks passed
@ahaverbuch ahaverbuch deleted the add-load-hook branch April 29, 2026 13:35
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.

3 participants