Class: RSpactor::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/rspactor/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 feature)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ Inspector

Returns a new instance of Inspector.



11
12
13
14
# File 'lib/rspactor/inspector.rb', line 11

def initialize(runner)
  @runner = runner
  @root = runner.dir
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/rspactor/inspector.rb', line 9

def root
  @root
end

#runnerObject (readonly)

Returns the value of attribute runner.



9
10
11
# File 'lib/rspactor/inspector.rb', line 9

def runner
  @runner
end

Instance Method Details

#append_spec_file_extension(file) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/rspactor/inspector.rb', line 103

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

#cucumber_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rspactor/inspector.rb', line 114

def cucumber_file?(file)
  file =~ /^features\/.+$/
end

#determine_files(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspactor/inspector.rb', line 16

def determine_files(file)
  candidates = translate(file)
  cucumberable = candidates.delete('cucumber')
  runner.spork.reload if candidates.delete('spork') && runner.spork?
  
  candidates.reject { |candidate| candidate.index('.') }.each do |dir|
    candidates.reject! { |candidate| candidate.index("#{dir}/") == 0 }
  end
  files = candidates.select { |candidate| File.exists? candidate }
  
  if files.empty? && !candidates.empty? && !cucumberable 
    $stderr.puts "doesn't exist: #{candidates.inspect}"
  end
  
  files << 'cucumber' if cucumberable
  files
end

#spec_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rspactor/inspector.rb', line 111

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

#translate(file) ⇒ Object

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



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rspactor/inspector.rb', line 35

def translate(file)
  file = file.sub(%r:^#{Regexp.escape(root)}/:, '')
  candidates = []
  
  if spec_file?(file)
    candidates << file
  elsif cucumber_file?(file)
    candidates << 'cucumber'
    candidates << 'spork' if file =~ /^features\/support\//
  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'
        candidates << 'views' if runner.options[:view]
      elsif file.include?("app/views/")
        if runner.options[:view]
          candidates << spec_file.sub('app/', '')
          if file =~ %r:^app/(views/.+\.[a-z]+)\.[a-z]+$:
            candidates << append_spec_file_extension($1)
          end
        end
      else
        candidates << spec_file.sub('app/', '')
        if file =~ %r:app/helpers/(\w+)_helper.rb:
          candidates << "views/#{$1}"
        elsif file =~ /_observer.rb$/
          candidates << candidates.last.sub('_observer', '')
        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' << 'routing'
      candidates << 'views' if runner.options[:view]
    when 'config/database.yml', 'db/schema.rb', 'spec/factories.rb'
      candidates << 'models'
    when 'config/boot.rb', 'config/environment.rb', %r:^config/environments/:, %r:^config/initializers/:, %r:^vendor/:, 'spec/spec_helper.rb'
      candidates << 'spork'
      candidates << 'spec'
    when %r:^config/:
      # nothing
    when %r:^(spec/(spec_helper|shared/.*)|config/(boot|environment(s/test)?))\.rb$:, 'spec/spec.opts', 'spec/fakewebs.rb'
      candidates << 'spec'
    else
      candidates << spec_file
    end
  end
  
  candidates.map do |candidate|
    if candidate == 'cucumber' || candidate == 'spork'
      candidate
    elsif candidate.index('spec') == 0
      File.join(root, candidate)
    else
      File.join(root, 'spec', candidate)
    end
  end
end