Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Runner + allow stubbing on JRuby
- Loading branch information
Showing
4 changed files
with
179 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "guard/rspec/command" | ||
|
||
module Guard | ||
class RSpec < Plugin | ||
class RSpecProcess | ||
class Failure < RuntimeError | ||
end | ||
|
||
attr_reader :results | ||
|
||
def initialize(command, formatter_tmp_file) | ||
@command = command | ||
@formatter_tmp_file = formatter_tmp_file | ||
@results = nil | ||
|
||
@exit_code = _run | ||
@results = _read_results | ||
end | ||
|
||
def all_green? | ||
exit_code.zero? | ||
end | ||
|
||
private | ||
|
||
def _run | ||
_without_bundler_env do | ||
exit_code = _really_run | ||
unless [0, Command::FAILURE_EXIT_CODE].include?(exit_code) | ||
fail Failure, "Failed: #{command.inspect} (exit code: #{exit_code})" | ||
end | ||
exit_code | ||
end | ||
end | ||
|
||
def _really_run | ||
pid = Kernel.spawn(command) # use spawn to stub in JRuby | ||
result = Process.wait2(pid) | ||
result.last.exitstatus | ||
rescue Errno::ENOENT => ex | ||
fail Failure, "Failed: #{command.inspect} (#{ex})" | ||
end | ||
|
||
def _read_results | ||
Results.new(formatter_tmp_file) | ||
ensure | ||
File.delete(formatter_tmp_file) if File.exist?(formatter_tmp_file) | ||
end | ||
|
||
def _without_bundler_env | ||
if defined?(::Bundler) | ||
::Bundler.with_clean_env { yield } | ||
else | ||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :command | ||
attr_reader :exit_code | ||
attr_reader :formatter_tmp_file | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require "guard/compat/test/helper" | ||
|
||
require "guard/rspec/rspec_process" | ||
|
||
RSpec.describe Guard::RSpec::RSpecProcess do | ||
before do | ||
allow(Kernel).to receive(:spawn) do |*args| | ||
fail "Not stubbed: Kernel.spawn(#{args.map(&:inspect) * ','})" | ||
end | ||
end | ||
|
||
let(:results) { instance_double(Guard::RSpec::Results) } | ||
|
||
let(:cmd) { "foo" } | ||
let(:file) { "foobar.txt" } | ||
let(:pid) { 1234 } | ||
let(:exit_code) { 0 } | ||
let(:status) { instance_double(Process::Status, exitstatus: exit_code) } | ||
let(:wait_result) { [pid, status] } | ||
|
||
subject do | ||
described_class.new(cmd, file) | ||
end | ||
|
||
before do | ||
allow(Guard::RSpec::Results).to receive(:new). | ||
with("foobar.txt").and_return(results) | ||
end | ||
|
||
context "with an non-existing command" do | ||
before do | ||
allow(Kernel).to receive(:spawn). | ||
and_raise(Errno::ENOENT, "No such file or directory - foo") | ||
end | ||
|
||
it "fails" do | ||
expect { subject }. | ||
to raise_error(Guard::RSpec::RSpecProcess::Failure, /Failed: /) | ||
end | ||
end | ||
|
||
context "with an existing command" do | ||
before do | ||
allow(Kernel).to receive(:spawn).with(cmd).and_return(pid) | ||
allow(Process).to receive(:wait2).with(pid).and_return(wait_result) | ||
end | ||
|
||
context "with an unknown failure" do | ||
let(:exit_code) { 100 } | ||
|
||
it "fails" do | ||
expect { subject }. | ||
to raise_error(Guard::RSpec::RSpecProcess::Failure, /Failed: /) | ||
end | ||
end | ||
|
||
context "with the failure code for normal test failures" do | ||
let(:exit_code) { Guard::RSpec::Command::FAILURE_EXIT_CODE } | ||
|
||
it "fails" do | ||
expect { subject }.to_not raise_error | ||
end | ||
|
||
it { is_expected.to_not be_all_green } | ||
end | ||
|
||
context "with no failures" do | ||
it "spawns and waits" do | ||
expect(Kernel).to receive(:spawn).with(cmd).and_return(pid) | ||
expect(Process).to receive(:wait2).with(pid).and_return(wait_result) | ||
subject | ||
end | ||
|
||
it { is_expected.to be_all_green } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters