Skip to content

Commit 7e8c806

Browse files
committed
Merge pull request #321 from guard/e2-fix_results_file_handling
improve results file handling
2 parents 8667958 + a91c13c commit 7e8c806

File tree

7 files changed

+122
-38
lines changed

7 files changed

+122
-38
lines changed

lib/guard/rspec/results.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Guard
2+
class RSpec < Plugin
3+
class Results
4+
class InvalidData < RuntimeError
5+
end
6+
7+
attr_reader :summary
8+
attr_reader :failed_paths
9+
10+
def initialize(filename)
11+
lines = File.readlines(filename)
12+
if lines.empty? || lines.first.empty?
13+
dump = lines.inspect
14+
fail InvalidData, "Invalid results in: #{filename},"\
15+
" lines:\n#{dump}\n"
16+
end
17+
18+
@summary = lines.first.chomp
19+
@failed_paths = lines[1..11].map(&:chomp).compact
20+
end
21+
end
22+
end
23+
end

lib/guard/rspec/runner.rb

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "guard/rspec/inspectors/factory"
22
require "guard/rspec/command"
33
require "guard/rspec/notifier"
4+
require "guard/rspec/results"
45

56
module Guard
67
class RSpec < Plugin
@@ -41,8 +42,12 @@ def _run(all, paths, options)
4142
return unless _cmd_option_present(options)
4243
command = Command.new(paths, options)
4344

44-
_without_bundler_env { Kernel.system(command) }.tap do |success|
45-
_process_run_result(success, all)
45+
_without_bundler_env { Kernel.system(command) }.tap do |result|
46+
if _command_success?(result)
47+
_process_run_result(result, all)
48+
else
49+
notifier.notify_failure
50+
end
4651
end
4752
end
4853

@@ -68,13 +73,7 @@ def _command_success?(success)
6873

6974
def _command_output
7075
formatter_tmp_file = _tmp_file(options[:chdir])
71-
lines = File.readlines(formatter_tmp_file)
72-
summary = lines.first.strip
73-
failed_paths = lines[1..11].map(&:strip).compact
74-
75-
[summary, failed_paths]
76-
rescue
77-
[nil, nil]
76+
Results.new(formatter_tmp_file)
7877
ensure
7978
File.delete(formatter_tmp_file) if File.exist?(formatter_tmp_file)
8079
end
@@ -92,19 +91,9 @@ def _run_all_after_pass
9291
end
9392

9493
def _process_run_result(result, all)
95-
unless _command_success?(result)
96-
notifier.notify_failure
97-
return
98-
end
99-
100-
summary, failed_paths = _command_output
101-
unless summary && failed_paths
102-
notifier.notify_failure
103-
return
104-
end
105-
106-
inspector.failed(failed_paths)
107-
notifier.notify(summary)
94+
results = _command_output
95+
inspector.failed(results.failed_paths)
96+
notifier.notify(results.summary)
10897
_open_launchy
10998

11099
_run_all_after_pass if !all && result

spec/lib/guard/rspec/command_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
before do
3737
allow(RSpec::Core::ConfigurationOptions).to receive(:new) do
38-
double(options: { formatters: formatters })
38+
instance_double(RSpec::Core::ConfigurationOptions,
39+
options: { formatters: formatters })
3940
end
4041
end
4142

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "guard/compat/test/helper"
2+
3+
require "guard/rspec/results"
4+
5+
RSpec.describe Guard::RSpec::Results do
6+
subject do
7+
described_class.new("foo/bar.txt")
8+
end
9+
10+
before do
11+
allow(File).to receive(:readlines).with("foo/bar.txt").and_return(data)
12+
end
13+
14+
context "with valid data" do
15+
let(:data) do
16+
[
17+
"5 examples, 2 failures (3 pending)\n",
18+
"foo1/bar1_spec.rb\n",
19+
"foo1/bar2_spec.rb\n",
20+
]
21+
end
22+
23+
describe "#summary" do
24+
it "sets a summary" do
25+
expect(subject.summary).to eq("5 examples, 2 failures (3 pending)")
26+
end
27+
end
28+
29+
describe "#failures" do
30+
it "sets a list of failures" do
31+
expect(subject.failed_paths).
32+
to eq(%w(foo1/bar1_spec.rb foo1/bar2_spec.rb))
33+
end
34+
end
35+
end
36+
37+
context "with no data" do
38+
let(:data) do
39+
[]
40+
end
41+
42+
it "crashes" do
43+
expect do
44+
subject.load
45+
end.to raise_error(
46+
Guard::RSpec::Results::InvalidData,
47+
"Invalid results in: foo/bar.txt, lines:\n[]\n")
48+
end
49+
end
50+
51+
context "with invalid data" do
52+
let(:data) do
53+
[""]
54+
end
55+
56+
it "crashes" do
57+
expect do
58+
subject.load
59+
end.to raise_error(
60+
Guard::RSpec::Results::InvalidData,
61+
"Invalid results in: foo/bar.txt, lines:\n[\"\"]\n")
62+
end
63+
end
64+
end

spec/lib/guard/rspec/runner_spec.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
RSpec.describe Guard::RSpec::Runner do
88
let(:options) { { cmd: "rspec" } }
99
let(:runner) { Guard::RSpec::Runner.new(options) }
10-
let(:inspector) { double(Guard::RSpec::Inspectors::SimpleInspector) }
11-
let(:notifier) { double(Guard::RSpec::Notifier) }
10+
let(:inspector) { instance_double(Guard::RSpec::Inspectors::SimpleInspector) }
11+
let(:notifier) { instance_double(Guard::RSpec::Notifier) }
12+
let(:results) { instance_double(Guard::RSpec::Results) }
1213

1314
before do
1415
allow(Guard::Compat::UI).to receive(:info)
@@ -19,6 +20,11 @@
1920
allow(notifier).to receive(:notify)
2021
allow(notifier).to receive(:notify_failure)
2122

23+
allow(Guard::RSpec::Results).to receive(:new).
24+
with("tmp/rspec_guard_result").and_return(results)
25+
allow(results).to receive(:summary).and_return("Summary")
26+
allow(results).to receive(:failed_paths).and_return([])
27+
2228
$CHILD_STATUS = double("exitstatus", exitstatus: 0)
2329
end
2430

@@ -68,7 +74,6 @@
6874

6975
before do
7076
allow(inspector).to receive(:failed)
71-
allow(File).to receive(:readlines).and_return([])
7277
end
7378

7479
it "builds commands with spec paths" do
@@ -136,10 +141,7 @@
136141
describe "#run" do
137142
let(:paths) { %w(spec_path1 spec_path2) }
138143
before do
139-
tmp_file = "tmp/rspec_guard_result"
140-
allow(File).to receive(:readlines).with(tmp_file) { ["Summary\n"] }
141144
allow(inspector).to receive(:paths) { paths }
142-
allow(inspector).to receive(:clear_paths) { true }
143145
allow(inspector).to receive(:failed)
144146
end
145147

@@ -195,15 +197,12 @@
195197

196198
context "with failed paths" do
197199
before do
198-
allow(File).to receive(:readlines).
199-
with("tmp/rspec_guard_result") do
200-
[
201-
"Summary\n",
202-
"./failed_spec.rb:123\n",
203-
"./other/failed_spec.rb:77\n"
204-
]
205-
end
200+
allow(results).to receive(:failed_paths).and_return([
201+
"./failed_spec.rb:123",
202+
"./other/failed_spec.rb:77"
203+
])
206204
end
205+
207206
it "notifies inspector about failed paths" do
208207
expect(inspector).to receive(:failed).
209208
with(["./failed_spec.rb:123", "./other/failed_spec.rb:77"])

spec/lib/guard/rspec_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let(:default_options) { Guard::RSpec::Options::DEFAULTS }
66
let(:options) { {} }
77
let(:plugin) { Guard::RSpec.new(options) }
8-
let(:runner) { double(Guard::RSpec::Runner) }
8+
let(:runner) { instance_double(Guard::RSpec::Runner) }
99

1010
before do
1111
allow(Guard::Compat::UI).to receive(:info)

spec/spec_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
rspec_version = ::RSpec::Version::STRING.to_f
99
old_rspec = (rspec_version < 3)
1010

11+
if old_rspec
12+
class RSpec::Core::ExampleGroup
13+
def instance_double(*args)
14+
double(*args)
15+
end
16+
end
17+
end
18+
1119
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1220
RSpec.configure do |config|
1321
config.expect_with :rspec do |expectations|

0 commit comments

Comments
 (0)