Class: Inspector

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

Overview

Maps the changed filenames to list of specs to run in the next go. Assumes Rails-like directory structure

Constant Summary collapse

EXTENSIONS =
%w(rb erb builder haml rhtml rxml yml conf opts)

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Inspector

Returns a new instance of Inspector.



7
8
9
# File 'lib/inspector.rb', line 7

def initialize(dir)
  @root = dir
end

Instance Method Details

#append_spec_file_extension(file) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/inspector.rb', line 72

def append_spec_file_extension(file)
  if File.extname(file) == ".rb"
    file.sub(/.rb$/, "_spec.rb")
  else
    file + "_spec.rb"
  end
end

#determine_spec_files(file) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/inspector.rb', line 11

def determine_spec_files(file)
  candidates = translate(file)
  spec_files = candidates.select { |candidate| File.exists? candidate }
  
  if spec_files.empty?
    $stderr.puts "doesn't exist: #{candidates.inspect}"
  end
  spec_files
end

#spec_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/inspector.rb', line 80

def spec_file?(file)
  file =~ /^spec\/.+_spec.rb$/
end

#translate(file) ⇒ Object

mappings for Rails are inspired by autotest mappings in rspec-rails



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inspector.rb', line 22

def translate(file)
  file = file.sub(%r:^#{Regexp.escape(@root)}/:, '')
  candidates = []
  
  if spec_file?(file)
    candidates << file
  else
    spec_file = append_spec_file_extension(file)
    
    case file
    when %r:^app/:
      if file =~ %r:^app/controllers/application(_controller)?.rb$:
        candidates << 'controllers'
      elsif file == 'app/helpers/application_helper.rb'
        candidates << 'helpers' << 'views'
      else
        candidates << spec_file.sub('app/', '')
        
        if file =~ %r:^app/(views/.+\.[a-z]+)\.[a-z]+$:
          candidates << append_spec_file_extension($1)
        elsif file =~ %r:app/helpers/(\w+)_helper.rb:
          candidates << "views/#{$1}"
        end
      end
    when %r:^lib/:
      candidates << spec_file
      # lib/foo/bar_spec.rb -> lib/bar_spec.rb
      candidates << candidates.last.sub($&, '')
      # lib/bar_spec.rb -> bar_spec.rb
      candidates << candidates.last.sub(%r:\w+/:, '') if candidates.last.index('/')
    when 'config/routes.rb'
      candidates << 'controllers' << 'helpers' << 'views'
    when 'config/database.yml', 'db/schema.rb'
      candidates << 'models'
    when %r:^(spec/(spec_helper|shared/.*)|config/(boot|environment(s/test)?))\.rb$:, 'spec/spec.opts'
      candidates << 'spec'
    else
      candidates << spec_file
    end
  end
  
  candidates.map do |candidate|
    if candidate.index('spec') == 0
      candidate
    else
      'spec/' + candidate
    end
  end
end