Class: RSpecConsole::ConfigCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-console/config_cache.rb

Defined Under Namespace

Classes: RecordingProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_proxyObject

We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what’s running, what are the different test results, etc).

RSpec.configuration gets also loaded with a bunch of stuff from the ‘spec/spec_helper.rb’ file. Often that instance is extended with other modules (FactoryGirl, Mocha,…) and we don’t want to replace requires with load all around the place.

Instead, we proxy and record whatever is done to RSpec.configuration during the first invocation of require(‘spec_helper’). This is done by interposing the RecordingProxy class on of RSpec.configuration.


14
15
16
# File 'lib/rspec-console/config_cache.rb', line 14

def config_proxy
  @config_proxy
end

#shared_examples_copyObject

We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what’s running, what are the different test results, etc).

RSpec.configuration gets also loaded with a bunch of stuff from the ‘spec/spec_helper.rb’ file. Often that instance is extended with other modules (FactoryGirl, Mocha,…) and we don’t want to replace requires with load all around the place.

Instead, we proxy and record whatever is done to RSpec.configuration during the first invocation of require(‘spec_helper’). This is done by interposing the RecordingProxy class on of RSpec.configuration.


14
15
16
# File 'lib/rspec-console/config_cache.rb', line 14

def shared_examples_copy
  @shared_examples_copy
end

Instance Method Details

#ensure_configuration_setter!Object

[View source]

98
99
100
101
102
103
104
105
106
# File 'lib/rspec-console/config_cache.rb', line 98

def ensure_configuration_setter!
  return if RSpec.respond_to?(:configuration=)

  ::RSpec.instance_eval do
    def self.configuration=(value)
      @configuration = value
    end
  end
end

#forward_rspec_config_singleton_to(config_proxy) ⇒ Object

[View source]

64
65
66
67
68
69
# File 'lib/rspec-console/config_cache.rb', line 64

def forward_rspec_config_singleton_to(config_proxy)
  # an old version of rspec-rails/lib/rspec/rails/view_rendering.rb adds
  # methods on the configuration singleton. This takes care of that.
  ::RSpec.configuration.singleton_class
    .send(:define_method, :method_missing, &config_proxy.method(:send))
end

#has_recorded_config?Boolean

Returns:

  • (Boolean)
[View source]

60
61
62
# File 'lib/rspec-console/config_cache.rb', line 60

def has_recorded_config?
  !!self.config_proxy
end

#record_configuration(&configuration_block) ⇒ Object

[View source]

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec-console/config_cache.rb', line 29

def record_configuration(&configuration_block)
  ensure_configuration_setter!

  original_config = ::RSpec.configuration
  ::RSpec.configuration = RecordingProxy.new(original_config, [])

  configuration_block.call # spec helper is called during this yield, see #reset

  self.config_proxy = ::RSpec.configuration
  ::RSpec.configuration = original_config

  stash_shared_examples

  forward_rspec_config_singleton_to(self.config_proxy)
end

#replay_configurationObject

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec-console/config_cache.rb', line 45

def replay_configuration
  ::RSpec.configure do |config|
    self.config_proxy.recorded_messages.each do |method, args, block|
      # reporter caches config.output_stream which is not good as it
      # prevents the runner to use a custom stdout.
      next if method == :reporter
      config.send(method, *args, &block)
    end
  end

  restore_shared_examples

  forward_rspec_config_singleton_to(self.config_proxy)
end

#restore_shared_examplesObject

[View source]

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rspec-console/config_cache.rb', line 82

def restore_shared_examples
  return if self.shared_examples_copy.nil?

  if rspec2?
    ::RSpec.world.shared_example_groups.merge!(self.shared_examples_copy)
  else
    self.shared_examples_copy.each do |context, name_blocks|
      name_blocks.each do |name, block|
        ::RSpec.world.shared_example_group_registry.add(context, name, &block)
      end
    end
  end
rescue Exception => e
  STDERR.puts "[rspec-console] WARN: #{e.class} #{e}"
end

#rspec2?Boolean

Returns:

  • (Boolean)
[View source]

108
109
110
# File 'lib/rspec-console/config_cache.rb', line 108

def rspec2?
  Gem.loaded_specs['rspec-core'].version < Gem::Version.new('3')
end

#stash_shared_examplesObject

[View source]

71
72
73
74
75
76
77
78
79
80
# File 'lib/rspec-console/config_cache.rb', line 71

def stash_shared_examples
  if rspec2?
    self.shared_examples_copy = ::RSpec.world.shared_example_groups.dup
  else
    self.shared_examples_copy = ::RSpec.world.shared_example_group_registry
                                  .send(:shared_example_groups).dup
  end
rescue Exception => e
  STDERR.puts "[rspec-console] WARN: #{e.class} #{e}"
end