From 274475870b27df262b0bd73d5f1e4017cb37aa70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Sat, 17 Jun 2017 19:22:26 +0200 Subject: [PATCH] Change Guard::RSpec to be a module and create Plugin which includes Compat::API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- Gemfile | 5 ++- guard-rspec.gemspec | 2 +- lib/guard/rspec.rb | 43 +------------------ lib/guard/rspec/command.rb | 2 +- lib/guard/rspec/deprecator.rb | 2 +- lib/guard/rspec/dsl.rb | 2 +- lib/guard/rspec/inspectors/base_inspector.rb | 2 +- lib/guard/rspec/inspectors/factory.rb | 2 +- .../rspec/inspectors/focused_inspector.rb | 2 +- .../rspec/inspectors/keeping_inspector.rb | 4 +- .../rspec/inspectors/simple_inspector.rb | 2 +- lib/guard/rspec/notifier.rb | 2 +- lib/guard/rspec/options.rb | 2 +- lib/guard/rspec/plugin.rb | 40 +++++++++++++++++ lib/guard/rspec/results.rb | 2 +- lib/guard/rspec/rspec_process.rb | 2 +- lib/guard/rspec/runner.rb | 2 +- lib/guard/rspec/version.rb | 2 +- .../{rspec_spec.rb => rspec/plugin_spec.rb} | 20 +++++---- 19 files changed, 72 insertions(+), 68 deletions(-) create mode 100644 lib/guard/rspec/plugin.rb rename spec/lib/guard/{rspec_spec.rb => rspec/plugin_spec.rb} (85%) diff --git a/Gemfile b/Gemfile index 6743958a..dd8d3417 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ filename = "gemfiles/common" instance_eval(IO.read(filename), filename, 1) +gem 'guard', github: 'guard/guard', branch: 'guard-api' + group :test do gem "rspec", "~> 3.4" end @@ -21,5 +23,6 @@ end group :development do gem "rubocop", require: false gem "guard-rubocop", require: false - gem "guard-compat", ">= 0.0.2", require: false + # gem "guard-compat", ">= 0.0.2", require: false + gem 'guard-compat', github: 'guard/guard-compat', branch: 'guard-api', require: false end diff --git a/guard-rspec.gemspec b/guard-rspec.gemspec index 4b7aef7f..44f75f00 100644 --- a/guard-rspec.gemspec +++ b/guard-rspec.gemspec @@ -5,7 +5,7 @@ require "guard/rspec/version" Gem::Specification.new do |s| s.name = "guard-rspec" - s.version = Guard::RSpecVersion::VERSION + s.version = Guard::RSpec::VERSION s.author = "Thibaud Guillaume-Gentil" s.email = "thibaud@thibaud.gg" s.summary = "Guard gem for RSpec" diff --git a/lib/guard/rspec.rb b/lib/guard/rspec.rb index 39c4c8ad..84f5ea6e 100644 --- a/lib/guard/rspec.rb +++ b/lib/guard/rspec.rb @@ -4,48 +4,7 @@ require "guard/rspec/deprecator" require "guard/rspec/runner" -# NOTE: To avoid 'superclass mismatch for class RSpec' errors, -# every file has to have -# -# class RSpec < Plugin -# -# and not just -# -# class RSpec - module Guard - class RSpec < Plugin - attr_accessor :options, :runner - - def initialize(options = {}) - super - @options = Options.with_defaults(options) - Deprecator.warns_about_deprecated_options(@options) - @runner = Runner.new(@options) - end - - def start - Compat::UI.info "Guard::RSpec is running" - run_all if options[:all_on_start] - end - - def run_all - _throw_if_failed { runner.run_all } - end - - def reload - runner.reload - end - - def run_on_modifications(paths) - return false if paths.empty? - _throw_if_failed { runner.run(paths) } - end - - private - - def _throw_if_failed - throw :task_has_failed unless yield - end + module RSpec end end diff --git a/lib/guard/rspec/command.rb b/lib/guard/rspec/command.rb index 6a3b53b3..4576fd6a 100644 --- a/lib/guard/rspec/command.rb +++ b/lib/guard/rspec/command.rb @@ -4,7 +4,7 @@ require "guard/rspec" module Guard - class RSpec < Plugin + module RSpec class Command < String FAILURE_EXIT_CODE = 2 diff --git a/lib/guard/rspec/deprecator.rb b/lib/guard/rspec/deprecator.rb index f3e9da7d..d66624b5 100644 --- a/lib/guard/rspec/deprecator.rb +++ b/lib/guard/rspec/deprecator.rb @@ -1,5 +1,5 @@ module Guard - class RSpec < Plugin + module RSpec class Deprecator attr_accessor :options diff --git a/lib/guard/rspec/dsl.rb b/lib/guard/rspec/dsl.rb index 6b1fffa1..82af8140 100644 --- a/lib/guard/rspec/dsl.rb +++ b/lib/guard/rspec/dsl.rb @@ -3,7 +3,7 @@ require "guard/rspec" module Guard - class RSpec < Plugin + module RSpec class Dsl def initialize(dsl) @dsl = dsl diff --git a/lib/guard/rspec/inspectors/base_inspector.rb b/lib/guard/rspec/inspectors/base_inspector.rb index d3a8aa35..8776dc30 100644 --- a/lib/guard/rspec/inspectors/base_inspector.rb +++ b/lib/guard/rspec/inspectors/base_inspector.rb @@ -1,5 +1,5 @@ module Guard - class RSpec < Plugin + module RSpec module Inspectors class BaseInspector attr_accessor :options, :spec_paths diff --git a/lib/guard/rspec/inspectors/factory.rb b/lib/guard/rspec/inspectors/factory.rb index 6db474d0..ad41e571 100644 --- a/lib/guard/rspec/inspectors/factory.rb +++ b/lib/guard/rspec/inspectors/factory.rb @@ -3,7 +3,7 @@ require "guard/rspec/inspectors/focused_inspector.rb" module Guard - class RSpec < Plugin + module RSpec module Inspectors class Factory class << self diff --git a/lib/guard/rspec/inspectors/focused_inspector.rb b/lib/guard/rspec/inspectors/focused_inspector.rb index acdbad43..613e5e6a 100644 --- a/lib/guard/rspec/inspectors/focused_inspector.rb +++ b/lib/guard/rspec/inspectors/focused_inspector.rb @@ -1,7 +1,7 @@ require "guard/rspec/inspectors/base_inspector.rb" module Guard - class RSpec < Plugin + module RSpec module Inspectors # Inspector that focuses on set of paths if any of them is failing. # Returns only that set of paths on all future calls to #paths diff --git a/lib/guard/rspec/inspectors/keeping_inspector.rb b/lib/guard/rspec/inspectors/keeping_inspector.rb index 7518d58b..7387b346 100644 --- a/lib/guard/rspec/inspectors/keeping_inspector.rb +++ b/lib/guard/rspec/inspectors/keeping_inspector.rb @@ -1,7 +1,7 @@ require "guard/rspec/inspectors/base_inspector.rb" module Guard - class RSpec < Plugin + module RSpec module Inspectors # Inspector that remembers all failed paths and # returns that paths in future calls to #paths method @@ -52,7 +52,7 @@ def _location_path(location) # https://github.com/rspec/rspec-core/issues/952 # # module Guard -# class RSpec < Plugin +# module RSpec # module Inspectors # # Inspector that remembers all failed paths and # # returns that paths in future calls to #paths method diff --git a/lib/guard/rspec/inspectors/simple_inspector.rb b/lib/guard/rspec/inspectors/simple_inspector.rb index d92ac8b0..090ebf2b 100644 --- a/lib/guard/rspec/inspectors/simple_inspector.rb +++ b/lib/guard/rspec/inspectors/simple_inspector.rb @@ -1,7 +1,7 @@ require "guard/rspec/inspectors/base_inspector" module Guard - class RSpec < Plugin + module RSpec module Inspectors class SimpleInspector < BaseInspector def paths(paths) diff --git a/lib/guard/rspec/notifier.rb b/lib/guard/rspec/notifier.rb index 3154efc7..0af058f6 100644 --- a/lib/guard/rspec/notifier.rb +++ b/lib/guard/rspec/notifier.rb @@ -1,5 +1,5 @@ module Guard - class RSpec < Plugin + module RSpec class Notifier attr_accessor :options diff --git a/lib/guard/rspec/options.rb b/lib/guard/rspec/options.rb index dbeeab43..c49fbe61 100644 --- a/lib/guard/rspec/options.rb +++ b/lib/guard/rspec/options.rb @@ -1,5 +1,5 @@ module Guard - class RSpec < Plugin + module RSpec module Options DEFAULTS = { all_on_start: false, diff --git a/lib/guard/rspec/plugin.rb b/lib/guard/rspec/plugin.rb new file mode 100644 index 00000000..7921e6b8 --- /dev/null +++ b/lib/guard/rspec/plugin.rb @@ -0,0 +1,40 @@ +module Guard + module RSpec + class Plugin + include Compat::API + + attr_accessor :runner + + def initialize(opts = {}) + super + @options = Options.with_defaults(opts) + Deprecator.warns_about_deprecated_options(options) + @runner = Runner.new(options) + end + + def start + Compat::UI.info "Guard::RSpec is running" + run_all if options[:all_on_start] + end + + def run_all + _throw_if_failed { runner.run_all } + end + + def reload + runner.reload + end + + def run_on_modifications(paths) + return false if paths.empty? + _throw_if_failed { runner.run(paths) } + end + + private + + def _throw_if_failed + throw :task_has_failed unless yield + end + end + end +end diff --git a/lib/guard/rspec/results.rb b/lib/guard/rspec/results.rb index b2100d7b..54f1e858 100644 --- a/lib/guard/rspec/results.rb +++ b/lib/guard/rspec/results.rb @@ -1,5 +1,5 @@ module Guard - class RSpec < Plugin + module RSpec class Results class InvalidData < RuntimeError end diff --git a/lib/guard/rspec/rspec_process.rb b/lib/guard/rspec/rspec_process.rb index d6772c45..0d8b43b6 100644 --- a/lib/guard/rspec/rspec_process.rb +++ b/lib/guard/rspec/rspec_process.rb @@ -1,7 +1,7 @@ require "guard/rspec/command" module Guard - class RSpec < Plugin + module RSpec class RSpecProcess class Failure < RuntimeError end diff --git a/lib/guard/rspec/runner.rb b/lib/guard/rspec/runner.rb index 6a3e04ef..0c15650c 100644 --- a/lib/guard/rspec/runner.rb +++ b/lib/guard/rspec/runner.rb @@ -7,7 +7,7 @@ require "guard/rspec/rspec_process" module Guard - class RSpec < Plugin + module RSpec class Runner class NoCmdOptionError < RuntimeError def initialize diff --git a/lib/guard/rspec/version.rb b/lib/guard/rspec/version.rb index b9e2315c..7f3d547c 100644 --- a/lib/guard/rspec/version.rb +++ b/lib/guard/rspec/version.rb @@ -1,5 +1,5 @@ module Guard - module RSpecVersion + module RSpec VERSION = "4.7.3".freeze end end diff --git a/spec/lib/guard/rspec_spec.rb b/spec/lib/guard/rspec/plugin_spec.rb similarity index 85% rename from spec/lib/guard/rspec_spec.rb rename to spec/lib/guard/rspec/plugin_spec.rb index 58435654..ed9c55a1 100644 --- a/spec/lib/guard/rspec_spec.rb +++ b/spec/lib/guard/rspec/plugin_spec.rb @@ -1,10 +1,10 @@ require "guard/compat/test/helper" -require "guard/rspec" +require "guard/rspec/plugin" -RSpec.describe Guard::RSpec do +RSpec.describe Guard::RSpec::Plugin do let(:default_options) { Guard::RSpec::Options::DEFAULTS } let(:options) { {} } - let(:plugin) { Guard::RSpec.new(options) } + let(:plugin) { described_class.new(options) } let(:runner) { instance_double(Guard::RSpec::Runner) } before do @@ -14,23 +14,25 @@ end describe ".initialize" do + let(:options) { { foo: :bar } } + it "instanciates with default and custom options" do - guard_rspec = Guard::RSpec.new(foo: :bar) - expect(guard_rspec.options).to eq(default_options.merge(foo: :bar)) + expect(plugin.options).to eq(default_options.merge(options)) end it "instanciates Runner with all default and custom options" do expect(Guard::RSpec::Runner).to receive(:new). - with(default_options.merge(foo: :bar)) - Guard::RSpec.new(foo: :bar) + with(default_options.merge(options)) + + plugin end it "warns deprecated options" do expect(Guard::RSpec::Deprecator). to receive(:warns_about_deprecated_options). - with(default_options.merge(foo: :bar)) + with(default_options.merge(options)) - Guard::RSpec.new(foo: :bar) + plugin end end