Skip to content

Commit

Permalink
Merge pull request #304 from guard/update_guard_compat_n_rework_specs
Browse files Browse the repository at this point in the history
Update guard compat n rework specs
  • Loading branch information
e2 committed Dec 13, 2014
2 parents 73a631a + de90922 commit 1d2839a
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 62 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ matrix:
allow_failures:
- rvm: jruby-19mode
- rvm: rbx

24 changes: 20 additions & 4 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
group :specs, halt_on_fail: true do
guard :rspec, cmd: "bundle exec rspec" do
watch(%r{^spec/.+_spec\.rb})
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch("spec/spec_helper.rb") { "spec" }
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_files)
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }

# Ruby files
dsl.watch_spec_files_for(dsl.ruby.lib_files)

watch(%r{^(lib/guard/rspec/template)s/Guardfile$}) do
rspec.spec.("lib/guard/rspec/template")
end

watch(%r{^lib/guard/rspec/dsl.rb$}) do
rspec.spec.("lib/guard/rspec/template")
end
end

guard :rubocop do
guard :rubocop, all_on_start: false do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end
Expand Down
2 changes: 1 addition & 1 deletion guard-rspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.require_path = "lib"

s.add_dependency "guard", "~> 2.1"
s.add_dependency "guard-compat", "~> 0.1"
s.add_dependency "guard-compat", "~> 1.0"
s.add_dependency "rspec", ">= 2.99.0", "< 4.0"

s.add_development_dependency "bundler", ">= 1.3.5", "< 2.0"
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(options = {})
end

def start
::Guard::UI.info "Guard::RSpec is running"
Guard::Compat::UI.info "Guard::RSpec is running"
run_all if options[:all_on_start]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/guard/rspec/deprecator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def warns_about_deprecated_options

def _spec_opts_env
return if ENV["SPEC_OPTS"].nil?
UI.warning(
Compat::UI.warning(
"The SPEC_OPTS environment variable is present." +
" This can conflict with guard-rspec."
)
Expand Down Expand Up @@ -77,7 +77,7 @@ def _focus_on_failed_option
end

def _deprecated(message)
UI.warning %(Guard::RSpec DEPRECATION WARNING: #{message})
Compat::UI.warning %(Guard::RSpec DEPRECATION WARNING: #{message})
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions lib/guard/rspec/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "ostruct"

require "guard/rspec"

module Guard
class RSpec < Plugin
class Dsl
def initialize(dsl)
@dsl = dsl
end

def watch_spec_files_for(expr)
@dsl.send(:watch, expr) { |m| rspec.spec.(m[1]) }
end

def rspec
@rspec ||= OpenStruct.new(to_s: "spec").tap do |rspec|
rspec.spec_dir = "spec"
rspec.spec = ->(m) { "#{rspec.spec_dir}/#{m}_spec.rb" }
rspec.spec_helper = "#{rspec.spec_dir}/spec_helper.rb"
rspec.spec_files = %r{^#{rspec.spec_dir}/.+_spec\.rb$}
rspec.spec_support = %r{^#{rspec.spec_dir}/support/(.+)\.rb$}
end
end

def ruby
# Ruby apps
@ruby || OpenStruct.new.tap do |ruby|
ruby.lib_files = %r{^(lib/.+)\.rb$}
end
end

def rails(options = {})
# Rails example
@rails ||= OpenStruct.new.tap do |rails|
exts = options.dup.delete(:view_extensions) || %w(erb haml slim)

rails.app_files = %r{^app/(.+)\.rb$}

rails.views = %r{^app/(views/.+/[^/]*\.(?:#{exts * "|"}))$}
rails.view_dirs = %r{^app/views/(.+)/[^/]*\.(?:#{exts * "|"})$}
rails.layouts = %r{^app/layouts/(.+)/.*\.("#{exts * "|"}")$}

rails.controllers = %r{^app/controllers/(.+)_controller\.rb$}
rails.routes = "config/routes.rb"
rails.app_controller = "app/controllers/application_controller.rb"
rails.spec_helper = "#{rspec.spec_dir}/rails_helper.rb"
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/guard/rspec/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def notify(summary)
failure_count, pending_count = _parse_summary(summary)
image = _image(failure_count, pending_count)
priority = _priority(image)
::Guard::Notifier.notify(summary,
Guard::Compat::UI.notify(summary,
title: @options[:title],
image: image,
priority: priority)
end

def notify_failure
return unless options[:notification]
::Guard::Notifier.notify("Failed",
Guard::Compat::UI.notify("Failed",
title: @options[:title],
image: :failed,
priority: 2)
Expand Down
6 changes: 3 additions & 3 deletions lib/guard/rspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def run_all
paths = options[:spec_paths]
options = @options.merge(@options[:run_all])
return true if paths.empty?
::Guard::UI.info(options[:message], reset: true)
Compat::UI.info(options[:message], reset: true)
_run(true, paths, options)
end

def run(paths)
paths = inspector.paths(paths)
return true if paths.empty?
::Guard::UI.info("Running: #{paths.join(" ")}", reset: true)
Compat::UI.info("Running: #{paths.join(" ")}", reset: true)
_run(false, paths, options)
end

Expand Down Expand Up @@ -56,7 +56,7 @@ def _without_bundler_env

def _cmd_option_present(options)
return true if options[:cmd]
::Guard::UI.error("No cmd option specified, unable to run specs!")
Compat::UI.error("No cmd option specified, unable to run specs!")
notifier.notify_failure
false
end
Expand Down
54 changes: 24 additions & 30 deletions lib/guard/rspec/templates/Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "ostruct"

# Generic Ruby apps
rspec = OpenStruct.new
rspec.spec = ->(m) { "spec/#{m}_spec.rb" }
rspec.spec_dir = "spec"
rspec.spec_helper = "spec/spec_helper.rb"

watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| rspec.spec.("lib/#{m[1]}") }
watch(rspec.spec_helper) { rspec.spec_dir }

# Rails example
rails = OpenStruct.new
rails.app = %r{^app/(.+)\.rb$}
rails.views_n_layouts = %r{^app/(.*)(\.erb|\.haml|\.slim)$}
rails.controllers = %r{^app/controllers/(.+)_controller\.rb$}
rails.routes = "config/routes.rb"
rails.app_controller = "app/controllers/application_controller.rb"
rails.spec_helper = "spec/rails_helper.rb"
rails.spec_support = %r{^spec/support/(.+)\.rb$}
rails.views = %r{^app/views/(.+)/.*\.(erb|haml|slim)$}

watch(rails.app) { |m| rspec.spec.(m[1]) }
watch(rails.views_n_layouts) { |m| rspec.spec.("#{m[1]}#{m[2]}") }
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.("routing/#{m[1]}_routing"),
Expand All @@ -41,18 +36,17 @@ guard :rspec, cmd: "bundle exec rspec" do
]
end

watch(rails.spec_support) { rspec.spec_dir }
# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "spec/routing" }
watch(rails.app_controller) { "spec/controllers" }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.views) { |m| rspec.spec.("features/#{m[1]}") }
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end

end
14 changes: 7 additions & 7 deletions spec/lib/guard/rspec/deprecator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
describe "handling of environment variable SPEC_OPTS" do
it "shows warning if SPEC_OPTS is set" do
ENV["SPEC_OPTS"] = "-f p"
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"The SPEC_OPTS environment variable is present." +
" This can conflict with guard-rspec.")
deprecator.warns_about_deprecated_options
ENV["SPEC_OPTS"] = nil # otherwise other specs pick it up and fail
end
it "does not show warning if SPEC_OPTS is unset" do
expect(Guard::UI).to_not receive(:warning).with(
expect(Guard::Compat::UI).to_not receive(:warning).with(
"The SPEC_OPTS environment variable is present." +
" This can conflict with guard-rspec.")
deprecator.warns_about_deprecated_options
Expand All @@ -28,7 +28,7 @@
let(:options) { { version: 1 } }

it "shows deprecation warning" do
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"Guard::RSpec DEPRECATION WARNING:" +
" The :version option is deprecated." +
" Only RSpec ~> 2.14 is now supported.")
Expand All @@ -40,7 +40,7 @@
let(:options) { { exclude: "**" } }

it "shows deprecation warning" do
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"Guard::RSpec DEPRECATION WARNING:" +
" The :exclude option is deprecated." +
" Please Guard ignore method instead." +
Expand All @@ -55,7 +55,7 @@
let(:options) { { option.to_sym => 1 } }

it "shows deprecation warning" do
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"Guard::RSpec DEPRECATION WARNING: The :#{option} option is" +
" deprecated. Please customize the new :cmd option to" +
" fit your need.")
Expand All @@ -68,7 +68,7 @@
let(:options) { { keep_failed: true } }

it "shows deprecation warning" do
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"Guard::RSpec DEPRECATION WARNING:" +
" The :keep_failed option is deprecated." +
" Please set new :failed_mode option value to" +
Expand All @@ -82,7 +82,7 @@
let(:options) { { focus_on_failed: true } }

it "shows deprecation warning" do
expect(Guard::UI).to receive(:warning).with(
expect(Guard::Compat::UI).to receive(:warning).with(
"Guard::RSpec DEPRECATION WARNING:" +
" The :focus_on_failed option is deprecated." +
" Focus mode is the default and can be changed using new" +
Expand Down
7 changes: 3 additions & 4 deletions spec/lib/guard/rspec/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
let(:notifier) { Guard::RSpec::Notifier.new(options) }

def expect_notification(title = "RSpec results", message, image, priority)
expect(Guard::Notifier).
to receive(:notify).
expect(Guard::Compat::UI).to receive(:notify).
with(message, title: title, image: image, priority: priority)
end

Expand Down Expand Up @@ -76,14 +75,14 @@ def expect_notification(title = "RSpec results", message, image, priority)

describe "#notify_failure" do
it "keeps quiet" do
expect(Guard::Notifier).not_to receive(:notify)
expect(Guard::Compat::UI).not_to receive(:notify)
notifier.notify_failure
end
end

describe "#notify" do
it "keeps quiet" do
expect(Guard::Notifier).not_to receive(:notify)
expect(Guard::Compat::UI).not_to receive(:notify)
notifier.notify("Summary")
end
end
Expand Down
15 changes: 9 additions & 6 deletions spec/lib/guard/rspec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
let(:notifier) { double(Guard::RSpec::Notifier) }

before do
allow(Guard::UI).to receive(:info)
allow(Guard::Compat::UI).to receive(:info)
allow(Kernel).to receive(:system) { true }
allow(Guard::RSpec::Inspectors::Factory).to receive(:create) { inspector }
allow(Guard::RSpec::Notifier).to receive(:new) { notifier }
Expand Down Expand Up @@ -48,7 +48,7 @@

shared_examples "abort" do
it "aborts" do
expect(Guard::UI).to_not receive(:info)
expect(Guard::Compat::UI).to_not receive(:info)
subject
end

Expand Down Expand Up @@ -78,7 +78,9 @@
end

it "prints message" do
expect(Guard::UI).to receive(:info).with("Custom message", reset: true)
expect(Guard::Compat::UI).to receive(:info).
with("Custom message", reset: true)

runner.run_all
end

Expand Down Expand Up @@ -111,7 +113,7 @@
before do
options[:cmd] = nil
allow(Guard::RSpec::Command).to receive(:new)
allow(Guard::UI).to receive(:error).with(an_instance_of(String))
allow(Guard::Compat::UI).to receive(:error).with(an_instance_of(String))
allow(notifier).to receive(:notify_failure)
runner.run_all
end
Expand All @@ -121,7 +123,8 @@
end

it "issues a warning to the user" do
expect(Guard::UI).to have_received(:error).with(an_instance_of(String))
expect(Guard::Compat::UI).to have_received(:error).
with(an_instance_of(String))
end

it "notifies the notifer of failure" do
Expand All @@ -141,7 +144,7 @@
end

it "prints running message" do
expect(Guard::UI).to receive(:info).
expect(Guard::Compat::UI).to receive(:info).
with("Running: spec_path1 spec_path2", reset: true)
runner.run(paths)
end
Expand Down
Loading

0 comments on commit 1d2839a

Please sign in to comment.