Module: SimpleCov::Deprecation

Defined in:
lib/simplecov/deprecation.rb

Overview

Emits legacy-API deprecation warnings, deduplicated by the source location that triggered them. A deprecated method called in a loop —or a config block re-evaluated once per parallel worker / spec file —otherwise repeats the same notice until stderr is unreadable. Keying on the caller location collapses those repeats to a single line while still warning separately about each distinct call site the user needs to fix. See issue #1204.

Class Method Summary collapse

Class Method Details

.emittedObject

Already-emitted dedup keys for this process. Parallel workers are separate processes with their own set, so each warns at most once.



38
39
40
# File 'lib/simplecov/deprecation.rb', line 38

def emitted
  @emitted ||= Set.new
end

.reset!Object



43
44
45
# File 'lib/simplecov/deprecation.rb', line 43

def reset!
  @emitted = Set.new
end

.warn(message, location: Array(Kernel.caller(2..2)).first) ⇒ Object

Warn about a deprecated API. ‘message` is the notice without the `[DEPRECATION]` tag or location prefix (both are added here).

‘location` defaults to the caller of the deprecated method that called us — every shipped call site is a one-level alias such as `track_files`, so the frame two up is the user code. Pass `location:` explicitly when the relevant site isn’t that frame (e.g. a source file and line discovered while parsing). ‘Array(…)` coerces a missing backtrace (nil) to `[]` so `.first` yields nil rather than raising — and, unlike `&.`, adds no branch for the unreachable no-caller case to the project’s 100% coverage target.



27
28
29
30
31
32
33
34
# File 'lib/simplecov/deprecation.rb', line 27

def warn(message, location: Array(Kernel.caller(2..2)).first)
  # Key on location when we have one (collapses a deprecated call in a
  # loop to a single warning); fall back to the message so a missing
  # backtrace never silently swallows every notice.
  return unless emitted.add?(location || message)

  Kernel.warn "#{"#{location}: " if location}[DEPRECATION] #{message}"
end