Skip to content

Commit 36e7424

Browse files
committed
Merge pull request #323 from guard/e2-fix_322_spec_when_no_spec_lib
find spec/foo_spec.rb when no spec/lib/foo_spec.rb
2 parents 7e8c806 + 4800246 commit 36e7424

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

lib/guard/rspec/dsl.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ def watch_spec_files_for(expr)
1313
@dsl.send(:watch, expr) { |m| rspec.spec.(m[1]) }
1414
end
1515

16+
def self.detect_spec_file_for(rspec, file)
17+
# TODO: when spec not found ... run specs in topmost found path?
18+
# Or show warning?
19+
20+
path = "#{rspec.spec_dir}/#{file}_spec.rb"
21+
return path unless file.start_with?("lib/")
22+
return path if Dir.exist?("#{rspec.spec_dir}/lib")
23+
24+
without_lib = file.sub(/^lib\//, "")
25+
"#{rspec.spec_dir}/#{without_lib}_spec.rb"
26+
end
27+
1628
def rspec
1729
@rspec ||= OpenStruct.new(to_s: "spec").tap do |rspec|
1830
rspec.spec_dir = "spec"
19-
rspec.spec = ->(m) { "#{rspec.spec_dir}/#{m}_spec.rb" }
31+
rspec.spec = ->(m) { Dsl.detect_spec_file_for(rspec, m) }
2032
rspec.spec_helper = "#{rspec.spec_dir}/spec_helper.rb"
2133
rspec.spec_files = %r{^#{rspec.spec_dir}/.+_spec\.rb$}
2234
rspec.spec_support = %r{^#{rspec.spec_dir}/support/(.+)\.rb$}

spec/lib/guard/rspec/template_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,24 @@
1414
expect(subject.changed("spec/spec_helper.rb")).to eq(%w(spec))
1515
end
1616

17-
it "matches Ruby files by default" do
18-
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/lib/foo_spec.rb))
17+
describe "mapping files to specs" do
18+
before do
19+
allow(Dir).to receive(:exist?).with("spec/lib").and_return(has_spec_lib)
20+
end
21+
22+
context "when spec/lib exists" do
23+
let(:has_spec_lib) { true }
24+
it "matches Ruby files with files in spec/lib" do
25+
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/lib/foo_spec.rb))
26+
end
27+
end
28+
29+
context "when spec/lib does not exist" do
30+
let(:has_spec_lib) { false }
31+
it "matches Ruby files with files in spec/lib" do
32+
expect(subject.changed("lib/foo.rb")).to eq(%w(spec/foo_spec.rb))
33+
end
34+
end
1935
end
2036

2137
it "matches Rails files by default" do

spec/spec_helper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def instance_double(*args)
9797
config.raise_errors_for_deprecations!
9898

9999
config.before do
100+
%w(exist?).each do |meth|
101+
allow(Dir).to receive(meth.to_sym) do |*args|
102+
abort "stub me: Dir.#{meth}(#{args.map(&:inspect) * ','})!"
103+
end
104+
end
105+
100106
allow(Dir).to receive(:[]) do |*args|
101107
abort "stub me: Dir[#{args.first}]!"
102108
end
@@ -107,7 +113,7 @@ def instance_double(*args)
107113
end
108114
end
109115

110-
%w(mkdir).each do |meth|
116+
%w(mkdir mkdir_p).each do |meth|
111117
allow(FileUtils).to receive(meth.to_sym) do |*args|
112118
abort "stub me: FileUtils.#{meth}(#{args.map(&:inspect) * ','})!"
113119
end

0 commit comments

Comments
 (0)