Class: Bolt::Runners::RSpec

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt/runners/rspec.rb

Constant Summary collapse

MAPPINGS =

mappings define which folders hold the files that the listener should listen to

/(\.\/app\/|\.\/lib\/|\.\/spec\/controllers|\.\/spec\/models|\.\/spec)/
CLASS_MAP =

class map specifies the folders holding classes that can be reloaded

/(app\/controllers\/|app\/models\/|lib\/)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#clear_class, #file_verified?, #handle, #initialized, #load_file, #reload, #resolve_class, #resolve_classname

Instance Attribute Details

#notifierObject

accesors



21
22
23
# File 'lib/bolt/runners/rspec.rb', line 21

def notifier
  @notifier
end

#test_ioObject

accesors



21
22
23
# File 'lib/bolt/runners/rspec.rb', line 21

def test_io
  @test_io
end

Instance Method Details

#run(files) ⇒ Object

run the appropriate test



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
102
103
104
105
106
# File 'lib/bolt/runners/rspec.rb', line 61

def run(files)
  file = files.first
  
  require 'spec'
  
  # redirect spec output to StringIO
  io = StringIO.new
  ::Spec::Runner.use(::Spec::Runner::OptionParser.new($stderr, io).options)

  # refresh the loaded test file
  $".delete(file)
  begin
    require file
  rescue LoadError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  rescue ArgumentError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  rescue SyntaxError
    notifier.error("Error in #{file}", $!)
    puts $!
    return
  end
          
  # run the tests in the Spec::Runner
  ::Spec::Runner::CommandLine.run
  
  # recreate the reporter to refresh the example count
  ::Spec::Runner::Reporter.new(::Spec::Runner.options)
  
  # remove all examples up to date
  ::Spec::Runner.options.example_groups.each { |g| ::Spec::Runner.options.remove_example_group(g) }
          
  # read the buffer
  result = io.string.to_s.dup
  
  # send buffer to stdout
  puts result
  
  # sent result to notifier
  notifier.result(file, result.split("\n").compact.last)
  
end

#translate(file) ⇒ Object

mapping is a copied and modified version of mislav/rspactor Inspector#translate



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
# File 'lib/bolt/runners/rspec.rb', line 24

def translate(file)
  
  basename = File.basename(file)
  candidates = []
  test_filename = nil
  case file
    when %r:^app/controllers/:
      test_filename = file.sub('.rb', '_spec.rb').sub('app/controllers', 'spec/controllers')
    when %r:^app/models/:
      test_filename = "spec/models/#{basename.sub('.rb', '_spec.rb')}"
    when %r:^app/views/:
      file = file.sub('app/views/', '')
      directory = file.split('/')[0..-2].compact.join('/')
      test_filename = "spec/controllers/#{directory}_controller_spec.rb"
    when %r:^spec/:
      test_filename = file
    when %r:^lib/:
      # map libs to straight specs
      test_filename = "spec/#{file.sub('lib/', '').sub('.rb', '_spec.rb')}"
    when 'config/routes.rb'
      test_filename = "spec/controllers/#{basename.sub('.rb', '_spec.rb')}"
    when 'config/database.yml', 'db/schema.rb'
      #candidates << 'models'
    else
      #
  end
  if test_filename and file_verified?(test_filename)
    candidates << test_filename
  end
  if candidates == []
    puts "=> NOTICE: could not find spec file for: #{file}"
  end

  candidates
end