Module: SimpleCov::RSpec::DescribedSourceFiles Private

Defined in:
lib/simplecov-rspec/described_source_files.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Finds the source files that define the classes an RSpec run described.

This is the scope most people want from list_uncovered_files: on a focused run: "report on the code I am actually testing". Deriving it means reaching into RSpec.world, which is not part of RSpec's public API, so it lives here rather than in each project's spec_helper.rb, where it could not be fixed centrally.

Class Method Summary collapse

Class Method Details

.call(example_groups: ::RSpec.world.example_groups) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The source files defining the classes the run described

A group that describes something other than a Module contributes nothing, as does one whose described class is anonymous, defined in C, or no longer reachable by name. Such a group has no source file to report on, and saying so is not useful, so it is skipped silently.

Examples:

DescribedSourceFiles.call # => ['/project/lib/parser.rb']

Parameters:

  • example_groups (Array<Class>) (defaults to: ::RSpec.world.example_groups)

    the run's top-level example groups

Returns:

  • (Array<String>)

    absolute paths, without duplicates



33
34
35
# File 'lib/simplecov-rspec/described_source_files.rb', line 33

def call(example_groups: ::RSpec.world.example_groups)
  with_nested(example_groups).filter_map { |group| source_file(group) }.uniq
end

.source_file(group) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The source file defining one group's described class, if it has one

Examples:

DescribedSourceFiles.source_file(group) # => '/project/lib/parser.rb'

Parameters:

  • group (Class)

    the example group

Returns:

  • (String, nil)

    an absolute path, or nil if there is nothing to report on



66
67
68
69
70
71
72
73
# File 'lib/simplecov-rspec/described_source_files.rb', line 66

def source_file(group)
  described_class = group.described_class
  return nil unless described_class.is_a?(Module)

  # A class defined in C answers [], one whose constant is gone answers nil
  name = described_class.name
  name && Object.const_source_location(name)&.first
end

.with_nested(groups) ⇒ Array<Class>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The given example groups, plus every group nested within them

A nested group can describe a different class than the group containing it, so the whole tree is walked rather than just the top level.

Examples:

DescribedSourceFiles.with_nested(::RSpec.world.example_groups)

Parameters:

  • groups (Array<Class>)

    the example groups to walk

Returns:

  • (Array<Class>)


51
52
53
# File 'lib/simplecov-rspec/described_source_files.rb', line 51

def with_nested(groups)
  groups.flat_map { |group| [group, *with_nested(group.children)] }
end