Class: Guard::RSpectacle

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/rspectacle.rb,
lib/guard/rspectacle/runner.rb,
lib/guard/rspectacle/humanity.rb,
lib/guard/rspectacle/notifier.rb,
lib/guard/rspectacle/reloader.rb,
lib/guard/rspectacle/formatter.rb,
lib/guard/rspectacle/inspector.rb

Overview

The RSpecRails guard that gets notifications about the following Guard events: ‘start`, `stop`, `reload`, `run_all` and `run_on_change`.

Defined Under Namespace

Modules: Formatter, Inspector, Runner Classes: Humanity, Notifier, Reloader

Constant Summary collapse

DEFAULT_OPTIONS =
{
    :cli => '',
    :notification   => true,
    :hide_success   => false,
    :all_on_start   => true,
    :keep_failed    => true,
    :all_after_pass => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ RSpectacle

Initialize Guard::RSpecRails.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the watchers in the Guard block

  • options (Hash) (defaults to: {})

    the options for the Guard

Options Hash (options):

  • :cli (String)

    the RSpec CLI options

  • :notification (Boolean)

    show notifications

  • :hide_success (Boolean)

    hide success message notification

  • :all_on_start (Boolean)

    Run all specs on start

  • :keep_failed (Boolean)

    keep failed examples and add them to the next run again

  • :all_after_pass (Boolean)

    run all specs after all examples have passed again after failing



41
42
43
44
45
46
47
48
# File 'lib/guard/rspectacle.rb', line 41

def initialize(watchers = [], options = {})
  options = DEFAULT_OPTIONS.merge(options)

  super(watchers, options)

  self.last_run_passed = true
  self.rerun_specs = []
end

Instance Attribute Details

#last_run_passedObject

Returns the value of attribute last_run_passed.



19
20
21
# File 'lib/guard/rspectacle.rb', line 19

def last_run_passed
  @last_run_passed
end

#rerun_specsObject

Returns the value of attribute rerun_specs.



19
20
21
# File 'lib/guard/rspectacle.rb', line 19

def rerun_specs
  @rerun_specs
end

Instance Method Details

#reloadObject

Gets called when the Guard should reload itself.

Raises:

  • (:task_has_failed)

    when run_on_change has failed



66
67
68
69
70
71
# File 'lib/guard/rspectacle.rb', line 66

def reload
  Dir.glob('**/*.rb').each { |file| Reloader.reload_file(file) }

  self.last_run_passed = true
  self.rerun_specs = []
end

#run_allObject

Gets called when all specs should be run.

Raises:

  • (:task_has_failed)

    when run_on_change has failed



77
78
79
80
81
82
83
84
# File 'lib/guard/rspectacle.rb', line 77

def run_all
  passed, failed_examples, passed_examples = Runner.run(['spec'], options.merge({ :message => 'Run all specs'}))

  self.rerun_specs = failed_examples
  self.last_run_passed = passed

  throw :task_has_failed unless passed
end

#run_on_changes(paths) ⇒ Object

Gets called when watched paths and files have changes.

Parameters:

  • paths (Array<String>)

    the changed paths and files

Raises:

  • (:task_has_failed)

    when run_on_change has failed



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/guard/rspectacle.rb', line 91

def run_on_changes(paths)
  specs = Inspector.clean(paths)
  return false if specs.empty?

  specs += self.rerun_specs if options[:keep_failed]

  # RSpec reloads the files, so reload only non spec files
  (paths - specs).each { |path| Reloader.reload_file(path) }

  passed, failed_examples, passed_examples = Runner.run(specs, options)

  self.rerun_specs += failed_examples
  self.rerun_specs -= passed_examples

  run_all if passed && !self.last_run_passed && options[:all_after_pass]

  self.last_run_passed = passed

  throw :task_has_failed unless passed
end

#startObject

Gets called once when Guard starts.

Raises:

  • (:task_has_failed)

    when run_on_change has failed



54
55
56
57
58
59
60
# File 'lib/guard/rspectacle.rb', line 54

def start
  ENV['RAILS_ENV'] ||= 'test'
  require './spec/spec_helper'

  Formatter.info "RSpectacle is ready in #{ ENV['RAILS_ENV'] } environment."
  run_all if options[:all_on_start]
end