Class: Woods::Extractors::TestMappingExtractor

Inherits:
Object
  • Object
show all
Includes:
SharedDependencyScanner, SharedUtilityMethods
Defined in:
lib/woods/extractors/test_mapping_extractor.rb

Overview

TestMappingExtractor maps test files to the units they exercise.

Scans spec/*/_spec.rb (RSpec) and test/*/_test.rb (Minitest) to produce one ExtractedUnit per test file. Extracts subject class, test count, shared example usage, and test framework type.

Units are linked to the code under test via :test_coverage dependencies, inferred from the subject class name and file directory structure.

Examples:

extractor = TestMappingExtractor.new
units = extractor.extract_all
spec = units.find { |u| u.identifier == "spec/models/user_spec.rb" }
spec.[:subject_class]  # => "User"
spec.[:test_count]     # => 12

Constant Summary collapse

RSPEC_GLOB =
'spec/**/*_spec.rb'
MINITEST_GLOB =
'test/**/*_test.rb'

Constants included from SharedDependencyScanner

SharedDependencyScanner::FORM_ACTION_HELPER, SharedDependencyScanner::ROUTE_HELPER_PATTERN

Instance Method Summary collapse

Methods included from SharedDependencyScanner

#extract_constantize_targets, #scan_common_dependencies, #scan_form_dependencies, #scan_job_dependencies, #scan_mailer_dependencies, #scan_model_dependencies, #scan_navigation_dependencies, #scan_service_dependencies

Methods included from SharedUtilityMethods

#app_source?, #condition_label, #count_loc, #detect_entry_points, #extract_action_filter_actions, #extract_callback_conditions, #extract_class_methods, #extract_class_name, #extract_custom_errors, #extract_initialize_params, #extract_namespace, #extract_parent_class, #extract_public_methods, #resolve_source_location, #skip_file?

Constructor Details

#initializeTestMappingExtractor

Returns a new instance of TestMappingExtractor.



31
32
33
# File 'lib/woods/extractors/test_mapping_extractor.rb', line 31

def initialize
  @rails_root = Rails.root
end

Instance Method Details

#extract_allArray<ExtractedUnit>

Extract all test mapping units from spec/ and test/ directories.

Returns:



38
39
40
# File 'lib/woods/extractors/test_mapping_extractor.rb', line 38

def extract_all
  rspec_units + minitest_units
end

#extract_test_file(file_path) ⇒ ExtractedUnit?

Extract a single test file into a test mapping unit.

Parameters:

  • file_path (String)

    Absolute path to the spec or test file

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/woods/extractors/test_mapping_extractor.rb', line 46

def extract_test_file(file_path)
  source = File.read(file_path)
  framework = detect_framework(file_path)
  relative_path = file_path.sub("#{@rails_root}/", '')

  unit = ExtractedUnit.new(
    type: :test_mapping,
    identifier: relative_path,
    file_path: file_path
  )

  unit.source_code = source
  unit. = (source, file_path, framework)
  unit.dependencies = extract_dependencies(unit.[:subject_class], unit.[:test_type])

  unit
rescue StandardError => e
  Rails.logger.error("Failed to extract test mapping from #{file_path}: #{e.message}")
  nil
end