Class: SpecGuardian::FilePathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_guardian/file_path_resolver.rb

Class Method Summary collapse

Class Method Details

.resolve(file_path, framework) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spec_guardian/file_path_resolver.rb', line 5

def self.resolve(file_path, framework)
  # Remove leading ./ if present
  file_path = file_path.sub(%r{^\./}, '')

  # Extract the relative path from app/
  relative_path = if file_path.start_with?('app/')
                    file_path.sub(%r{^app/}, '')
                  else
                    file_path
                  end

  # Determine test directory based on framework
  test_dir = framework == :rspec ? 'spec' : 'test'

  # Handle different file types
  if relative_path.start_with?('models/')
    "#{test_dir}/#{relative_path.sub(/\.rb$/, '_spec.rb')}"
  elsif relative_path.start_with?('controllers/')
    "#{test_dir}/#{relative_path.sub(/\.rb$/, '_spec.rb')}"
  elsif relative_path.start_with?('views/')
    view_path = relative_path.sub(%r{^views/}, '')
    "#{test_dir}/views/#{view_path.sub(/\.\w+\.erb$/, '.html_spec.rb')}"
  else
    # For other files, maintain the same structure
    "#{test_dir}/#{relative_path.sub(/\.rb$/, '_spec.rb')}"
  end
end