Module: InteractiveRspec

Defined in:
lib/interactive_rspec.rb,
lib/interactive_rspec.rb,
lib/interactive_rspec/version.rb

Constant Summary collapse

VERSION =
'0.0.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.rspec_modeObject

Returns the value of attribute rspec_mode.



8
9
10
# File 'lib/interactive_rspec.rb', line 8

def rspec_mode
  @rspec_mode
end

Class Method Details

.configureObject



26
27
28
29
30
31
32
# File 'lib/interactive_rspec.rb', line 26

def self.configure
  #TODO load spec_helper.rb?
  RSpec.configure do |c|
    c.output_stream = STDOUT
    c.color_enabled = true
  end
end

.fuzzy_match(specs) ⇒ Object



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

def self.fuzzy_match(specs)
  return Dir.glob '**/*_spec.rb' if specs == :all
  [specs, "spec/#{specs}", "#{specs}.rb", "#{specs}_spec.rb", "spec/#{specs}.rb", "spec/#{specs}_spec.rb", "#{specs}/**/*_spec.rb", "spec/#{specs}/**/*_spec.rb"].each do |pattern|
    files = Dir.glob pattern
    return files if files.any?
  end
  specs
end

.new_extended_example_groupObject



34
35
36
37
38
39
40
# File 'lib/interactive_rspec.rb', line 34

def self.new_extended_example_group
  eg = describe
  RSpec.configuration.expectation_frameworks.each do |framework|
    eg.extend framework
  end
  eg.extend RSpec.configuration.mock_framework
end

.reconnect_active_recordObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/interactive_rspec.rb', line 115

def self.reconnect_active_record
  if defined? ActiveRecord::Base
    if ActiveRecord::Base.respond_to? :clear_cache
      ActiveRecord::Base.clear_cache!
    else
    end
    ActiveRecord::Base.clear_all_connections!
    ActiveRecord::Base.establish_connection
  end
end

.report(result) ⇒ Object

Parameters:

  • (Exception or true)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/interactive_rspec.rb', line 43

def self.report(result)
  e = describe.example

  ret = RSpec.configuration.reporter.report(1) do |r|
    r.instance_variable_set '@example_count', 1
    if result.is_a? Exception
      result.extend(RSpec::Core::Example::NotPendingExampleFixed) if defined?(RSpec::Core::Example::NotPendingExampleFixed) && !result.respond_to?(:pending_fixed?)
      e.send :record, :status => 'failed', :finished_at => Time.now, :run_time => 0, :exception => result
      r.example_failed e
      false
    else
      r.example_passed e
      true
    end
  end
  RSpec.reset
  ret
end

.run_specs(specs) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/interactive_rspec.rb', line 62

def self.run_specs(specs)
  # to avoid auto_run at_exit
  RSpec::Core::Runner.instance_variable_set '@autorun_disabled', true
  config_options = RSpec::Core::ConfigurationOptions.new ['--color', fuzzy_match(specs)]
  config_options.parse_options

  RSpec::Core::CommandLine.new(config_options, RSpec.configuration, RSpec.world).run(STDERR, STDOUT)
end

.start(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/interactive_rspec.rb', line 16

def self.start(options = {})
  configure if {:configure => true}.merge(options)

  switch_rspec_mode do
    switch_rails_env do
      IRB.start_with_context new_extended_example_group
    end
  end
end

.switch_rails_env(&block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/interactive_rspec.rb', line 89

def self.switch_rails_env(&block)
  begin
    original_env_rails_env, original_rails_rails_env = nil, nil
    if defined? Rails
      unless Rails.env.test?
        original_env_rails_env = ENV['RAILS_ENV']
        ENV['RAILS_ENV'] = 'test'
        original_rails_rails_env = Rails.env
        load Rails.root.join 'config/environments/test.rb'
        Rails.env = 'test'
        reconnect_active_record
        Bundler.require :test if defined? Bundler
      end
    end

    block.call
  ensure
    if original_env_rails_env || original_rails_rails_env
      ENV['RAILS_ENV'] = original_env_rails_env
      Rails.env = original_rails_rails_env
      load Rails.root.join "config/environments/#{Rails.env}.rb"
      reconnect_active_record
    end
  end
end

.switch_rspec_mode(&block) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/interactive_rspec.rb', line 80

def self.switch_rspec_mode(&block)
  begin
    InteractiveRspec.rspec_mode = true
    block.call
  ensure
    InteractiveRspec.rspec_mode = false
  end
end