guard-jruby-rspec icon indicating copy to clipboard operation
guard-jruby-rspec copied to clipboard

Ability to add custom reloaders to run before running all specs.

Open sumitmah opened this issue 9 years ago • 5 comments
trafficstars

This ability will give us resolve issues related to missing constants, loading/requiring files.

sumitmah avatar Apr 19 '16 06:04 sumitmah

Hi @sumitmah, thanks for this! Before we get this ready to merge, could you help us understand the scenario in which you want to have special reloaders associated with run_all but not normal runs?

nilbus avatar Apr 19 '16 13:04 nilbus

@nilbus As it is mentioned in readme,

Since JRuby cannot fork, guard-jruby-rspec reloads code with load on each changed path, which can potentially cause weird side side effects or errors.

I was also facing similar issues with my Rails project. e.g. for shared specs(I saw similar issue already posted for this gem) "context not found". This was happening because of following code.

def unload_previous_examples
  ::RSpec.configuration.reset
  ::RSpec.world.reset
end

::RSpec.world.reset will remove shared example groups. Following is definition of reset method. I had to reload these examples explicitly in the custom reloader for run all on rerun. Since following method clears the example groups.

def reset
  example_groups.clear
  @shared_example_group_registry = nil
end

Here is example from my guardfile

    reload_shared_specs = lambda do 
    if Object.const_defined?('Rails')
      $LOADED_FEATURES.reject! { |path| path =~ /\/spec\/support\//}
      ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /\/spec\/support\//}.inspect

      ActiveSupport::Dependencies.autoloaded_constants.each {|c|   ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /#{c.to_s.underscore}/} }
      ActiveSupport::Dependencies.autoloaded_constants.each {|c| $LOADED_FEATURES.reject! { |path| path =~ /#{c.to_s.underscore}/} }

      ActiveSupport::Dependencies.explicitly_unloadable_constants.each {|c| ActiveSupport::Dependencies.loaded.reject! { |path| path =~ /#{c.to_s.underscore}/} }
      ActiveSupport::Dependencies.explicitly_unloadable_constants.each {|c| $LOADED_FEATURES.reject! { |path| path =~ /#{c.to_s.underscore}/} }

      Dir[::Rails.root.join('spec/support/**/*.rb')].each { |f| ::RSpec.world.shared_example_group_registry.require_or_load(f) }
      end
    end

sumitmah avatar Apr 19 '16 18:04 sumitmah

@nilbus @jkutner any thoughts on this?

sumitmah avatar Apr 20 '16 18:04 sumitmah

Thanks for the background information, and sorry for the delay @sumitmah. I'm trying to connect the code changes with that background story. Is the Guardfile code you provided what you're running inside a custom_reloaders_for_run_all block?

nilbus avatar May 08 '16 23:05 nilbus

@nilbus Yes

sumitmah avatar May 09 '16 04:05 sumitmah