Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find spec/foo_spec.rb when no spec/lib/foo_spec.rb #323

Merged
merged 2 commits into from
Jun 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/guard/rspec/dsl.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
8 changes: 7 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
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 All @@ -107,7 +113,7 @@ def instance_double(*args)
end
end

%w(mkdir).each do |meth|
%w(mkdir mkdir_p).each do |meth|
allow(FileUtils).to receive(meth.to_sym) do |*args|
abort "stub me: FileUtils.#{meth}(#{args.map(&:inspect) * ','})!"
end
Expand Down