Skip to content

Commit

Permalink
detect specs in spec/ when no spec/lib [fix 322]
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Jun 3, 2015
1 parent ba3386b commit 4800246
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/guard/rspec/dsl.rb
Expand Up @@ -13,10 +13,22 @@ def watch_spec_files_for(expr)
@dsl.send(:watch, expr) { |m| rspec.spec.(m[1]) }
end

def self.detect_spec_file_for(rspec, file)
# TODO: when spec not found ... run specs in topmost found path?
# Or show warning?

path = "#{rspec.spec_dir}/#{file}_spec.rb"
return path unless file.start_with?("lib/")
return path if Dir.exist?("#{rspec.spec_dir}/lib")

without_lib = file.sub(/^lib\//, "")
"#{rspec.spec_dir}/#{without_lib}_spec.rb"
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 = ->(m) { Dsl.detect_spec_file_for(rspec, m) }
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$}
Expand Down
20 changes: 18 additions & 2 deletions spec/lib/guard/rspec/template_spec.rb
Expand Up @@ -14,8 +14,24 @@
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))
describe "mapping files to specs" do
before do
allow(Dir).to receive(:exist?).with("spec/lib").and_return(has_spec_lib)
end

context "when spec/lib exists" do
let(:has_spec_lib) { true }
it "matches Ruby files with files in spec/lib" do
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/lib/foo_spec.rb))
end
end

context "when spec/lib does not exist" do
let(:has_spec_lib) { false }
it "matches Ruby files with files in spec/lib" do
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/foo_spec.rb))
end
end
end

it "matches Rails files by default" do
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -97,6 +97,12 @@ def instance_double(*args)
config.raise_errors_for_deprecations!

config.before do
%w(exist?).each do |meth|
allow(Dir).to receive(meth.to_sym) do |*args|
abort "stub me: Dir.#{meth}(#{args.map(&:inspect) * ','})!"
end
end

allow(Dir).to receive(:[]) do |*args|
abort "stub me: Dir[#{args.first}]!"
end
Expand Down

0 comments on commit 4800246

Please sign in to comment.