Skip to content

Commit

Permalink
rework template using helpers + add template spec
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Dec 12, 2014
1 parent e762a2b commit de90922
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 30 deletions.
52 changes: 52 additions & 0 deletions lib/guard/rspec/dsl.rb
@@ -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
54 changes: 24 additions & 30 deletions lib/guard/rspec/templates/Guardfile
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
56 changes: 56 additions & 0 deletions spec/lib/guard/rspec/template_spec.rb
@@ -0,0 +1,56 @@
require "guard/compat/test/template"

# Do not require to simulate Guardfile loading more accurately
# require 'guard/rspec'

RSpec.describe "Guard::RSpec" do
describe "template" do
subject { Guard::Compat::Test::Template.new("Guard::RSpec") }

it "matches spec files by default" do
expect(subject.changed("spec/lib/foo_spec.rb")).
to eq(%w(spec/lib/foo_spec.rb))

expect(subject.changed("spec/spec_helper.rb")).to eq(%w(spec))
end

it "matches Ruby files by default" do
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/lib/foo_spec.rb))
end

it "matches Rails files by default" do
expect(subject.changed("spec/rails_helper.rb")).to eq(%w(spec))

expect(subject.changed("app/models/foo.rb")).
to eq(%w(spec/models/foo_spec.rb))

expect(subject.changed("app/views/foo/bar.slim")).to eq(
%w(
spec/views/foo/bar.slim_spec.rb
spec/features/foo_spec.rb
)
)

expect(subject.changed("app/controllers/application_controller.rb")).
to eq(
%w(
spec/controllers/application_controller_spec.rb
spec/routing/application_routing_spec.rb
spec/acceptance/application_spec.rb
spec/controllers
)
)

expect(subject.changed("app/controllers/foo_controller.rb")).
to match_array(
%w(
spec/controllers/foo_controller_spec.rb
spec/routing/foo_routing_spec.rb
spec/acceptance/foo_spec.rb
)
)

expect(subject.changed("config/routes.rb")).to eq(%w(spec/routing))
end
end
end

0 comments on commit de90922

Please sign in to comment.