Module: Fsr

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

Overview

Run RSpec fast by avoiding full app boot

“‘rb

listener = Fsr.listen(
  ['spec/controllers/task_controller_spec.rb'],
  load: ['app/controllers/task_controller.rb']
)

listener.start
listener.stop

“‘

Defined Under Namespace

Classes: Runner

Constant Summary collapse

LISTEN_DIRS =
[
  "#{`pwd`.strip}/app",
  "#{`pwd`.strip}/api",
  "#{`pwd`.strip}/components",
  "#{`pwd`.strip}/lib",
  "#{`pwd`.strip}/spec",
  "#{`pwd`.strip}/db"
].select { |dir| Dir.exist?(dir) }
VERSION =
'0.1.7'

Class Method Summary collapse

Class Method Details

.build_listener(run, load: [], listen: []) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fsr.rb', line 39

def self.build_listener(run, load: [], listen: [])
  listen = (listen.empty? ? LISTEN_DIRS : listen).select { |dir| Dir.exist?(dir) }
  @listener = Listen.to(*listen) do |modified, added|
    begin
      files = [modified, added, load].compact.flatten
      Fsr::Runner.new(run, load: files).run
    rescue => e
      puts "Something went wrong while loading the files or running the spec, #{e.message}, #{e.backtrace}"
    end
  end
end

.listen(run, load: [], listen: []) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/fsr.rb', line 31

def self.listen(run, load: [], listen: [])
  require 'rspec/core'
  ActiveRecord::Base.connection.reconnect!
  @listener.stop rescue 'error'
  @listener = build_listener(run, load: load, listen: listen)
  @listener.start
end

.run(run, load: []) ⇒ Object



51
52
53
54
# File 'lib/fsr.rb', line 51

def self.run(run, load: [])
  ActiveRecord::Base.connection.reconnect!
  Fsr::Runner.new(run, load: load).run
end

.sandboxedObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fsr.rb', line 56

def self.sandboxed
  orig_world   = RSpec.world
  orig_example = RSpec.current_example
  RSpec.world  = RSpec::Core::World.new(RSpec.configuration)

  yield
ensure
  RSpec.world           = orig_world
  RSpec.current_example = orig_example
  RSpec.clear_examples
end