Sdlc/Minitest/RequireInTestMethod: carve out tests whose subject is the require; fix count #1382

Open
opened 2026-10-25 21:23:40 +00:00 by jared · 0 comments
Owner

Problem

Sdlc/Minitest/RequireInTestMethod flags a require that is the subject of the test. Moving it to the top of the file changes what the test asserts. The message also counts calls as "method bodies".

Found by the #1325 zero-false-positive audit (docs/plans/class-length-family/sdlc-cop-audit.md).

Observed

Counterexample (file ends _test.rb): the test asserts the return value of require_relative.

class LoadTest < Minitest::Test
  def test_requiring_the_entry_file_twice_loads_it_once
    assert require_relative("../lib/entry")
    refute require_relative("../lib/entry")
  end
end

One method with 2 requires reports "inside 2 test method bodies".

Reproduce

n/a: run rubocop --only Sdlc/Minitest/RequireInTestMethod on the snippet above saved as load_test.rb.

Approach

Fix by shape. Put the carve-out in the cop matcher, never in a config Exclude list or a test-path exclusion.

Required work:

  1. Carve-out: a require whose return value is asserted, or that sits inside an assert, refute, or assert_raises argument or block, is the subject under test. Do not flag it.
  2. The message counts methods, not calls: one method with 2 requires reports "1 test method body".

Acceptance criteria

  1. Given a test that passes a require_relative call to assert, when the cop runs, then it reports no offense.
  2. Given one test method with 2 bare requires, when the cop reports, then the message counts 1 method body.

Origin

  • Trigger: the #1325 zero-false-positive audit of the Sdlc cops (docs/plans/class-length-family/sdlc-cop-audit.md)
  • Improvised this session: none
  • Chain: false positive ← matcher flags every require in a test def ← DESIGN (plugins/os-sdlc/lib/os_sdlc/cops/require_in_test_method.rb:15-19)
  • Root candidate: none found; searched RequireInTestMethod, require_in_test_method, "test method bod", RequireIn
  • Where: plugins/os-sdlc/lib/os_sdlc/cops/require_in_test_method.rb
  • Session: eb98cdcd-6783-46a0-b7df-cf18a8fe2a56
  • Transcript: /home/jared/.claude/projects/-home-jared-dev-cc-os--claude-worktrees-ticket-1325/eb98cdcd-6783-46a0-b7df-cf18a8fe2a56.jsonl

VERDICT: CREATE

## Problem `Sdlc/Minitest/RequireInTestMethod` flags a `require` that is the subject of the test. Moving it to the top of the file changes what the test asserts. The message also counts calls as "method bodies". Found by the #1325 zero-false-positive audit (docs/plans/class-length-family/sdlc-cop-audit.md). ## Observed Counterexample (file ends `_test.rb`): the test asserts the return value of `require_relative`. ```ruby class LoadTest < Minitest::Test def test_requiring_the_entry_file_twice_loads_it_once assert require_relative("../lib/entry") refute require_relative("../lib/entry") end end ``` One method with 2 requires reports "inside 2 test method bodies". ## Reproduce n/a: run `rubocop --only Sdlc/Minitest/RequireInTestMethod` on the snippet above saved as `load_test.rb`. ## Approach Fix by shape. Put the carve-out in the cop matcher, never in a config Exclude list or a test-path exclusion. Required work: 1. Carve-out: a require whose return value is asserted, or that sits inside an `assert`, `refute`, or `assert_raises` argument or block, is the subject under test. Do not flag it. 2. The message counts methods, not calls: one method with 2 requires reports "1 test method body". ## Acceptance criteria 1. Given a test that passes a `require_relative` call to `assert`, when the cop runs, then it reports no offense. 2. Given one test method with 2 bare requires, when the cop reports, then the message counts 1 method body. ## Origin - Trigger: the #1325 zero-false-positive audit of the Sdlc cops (docs/plans/class-length-family/sdlc-cop-audit.md) - Improvised this session: none - Chain: false positive ← matcher flags every require in a test def ← DESIGN (plugins/os-sdlc/lib/os_sdlc/cops/require_in_test_method.rb:15-19) - Root candidate: none found; searched RequireInTestMethod, require_in_test_method, "test method bod", RequireIn - Where: plugins/os-sdlc/lib/os_sdlc/cops/require_in_test_method.rb - Session: eb98cdcd-6783-46a0-b7df-cf18a8fe2a56 - Transcript: /home/jared/.claude/projects/-home-jared-dev-cc-os--claude-worktrees-ticket-1325/eb98cdcd-6783-46a0-b7df-cf18a8fe2a56.jsonl VERDICT: CREATE
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jared/cc-os#1382
No description provided.