Class: RailsMcpInsight::Analyzers::TestAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_mcp_insight/analyzers/test_analyzer.rb

Overview

Maps source files to their corresponding test/spec files.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ TestAnalyzer

Returns a new instance of TestAnalyzer.



7
8
9
# File 'lib/rails_mcp_insight/analyzers/test_analyzer.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#coverage_statsObject

Get test coverage statistics



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_mcp_insight/analyzers/test_analyzer.rb', line 51

def coverage_stats
  spec_files = Dir.exist?(@config.spec_path) ? Dir.glob(File.join(@config.spec_path, "**", "*_spec.rb")) : []
  test_files = Dir.exist?(@config.test_path) ? Dir.glob(File.join(@config.test_path, "**", "*_test.rb")) : []

  app_files = Dir.glob(File.join(@config.app_path, "**", "*.rb"))

  tested_files = app_files.count do |app_file|
    result = find_tests(app_file)
    result[:has_tests]
  end

  {
    app_files: app_files.length,
    spec_files: spec_files.length,
    test_files: test_files.length,
    total_test_files: spec_files.length + test_files.length,
    tested_app_files: tested_files,
    untested_app_files: app_files.length - tested_files,
    coverage_ratio: app_files.empty? ? 0 : (tested_files.to_f / app_files.length).round(2)
  }
end

#find_tests(source_file) ⇒ Object

Find tests for a given source file



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_mcp_insight/analyzers/test_analyzer.rb', line 12

def find_tests(source_file)
  relative = source_file.sub("#{@config.project_path}/", "")

  test_files = []

  # Check RSpec (spec/)
  if Dir.exist?(@config.spec_path)
    spec_file = relative.sub("app/", "spec/").sub(".rb", "_spec.rb")
    full_spec = File.join(@config.project_path, spec_file)
    test_files << { file: spec_file, exists: File.exist?(full_spec), framework: "rspec" }

    # Also check for request/integration specs
    if relative.start_with?("app/controllers/")
      request_spec = relative.sub("app/controllers/", "spec/requests/")
                             .sub("_controller.rb", "_spec.rb")
      full_request = File.join(@config.project_path, request_spec)
      test_files << { file: request_spec, exists: File.exist?(full_request), framework: "rspec" }
    end
  end

  # Check Minitest (test/)
  if Dir.exist?(@config.test_path)
    test_file = relative.sub("app/", "test/").sub(".rb", "_test.rb")
    full_test = File.join(@config.project_path, test_file)
    test_files << { file: test_file, exists: File.exist?(full_test), framework: "minitest" }
  end

  # Find related factories
  factory_file = find_factory(relative)

  {
    source_file: relative,
    test_files: test_files,
    has_tests: test_files.any? { |t| t[:exists] },
    factory: factory_file
  }
end