From 61b06e86d1adc26cf727ad63d73ada5e2146d79a Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sun, 11 May 2014 16:01:53 +0000 Subject: [PATCH] Add specs for Mutant.isolate --- lib/mutant.rb | 2 +- spec/unit/mutant_spec.rb | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/mutant.rb b/lib/mutant.rb index 50b4a40f..4a33a5a3 100644 --- a/lib/mutant.rb +++ b/lib/mutant.rb @@ -62,7 +62,7 @@ module Mutant begin data = Marshal.load(reader.read) rescue ArgumentError - raise IsolationError, 'Childprocess wrote unmarshallable data' + raise IsolationError, 'Childprocess wrote un-unmarshallable data' end status = Process.waitpid2(pid).last diff --git a/spec/unit/mutant_spec.rb b/spec/unit/mutant_spec.rb index d5779655..4c9a3394 100644 --- a/spec/unit/mutant_spec.rb +++ b/spec/unit/mutant_spec.rb @@ -38,4 +38,55 @@ describe Mutant do expect(inspect).to be_frozen end end + + describe '.isolate' do + let(:object) { described_class } + + let(:expected_return) { :foo } + + subject { object.isolate(&block) } + + def redirect_stderr + $stderr = File.open('/dev/null') + end + + context 'when block returns mashallable data, and process exists zero' do + let(:block) do + lambda do + :data_from_child_process + end + end + + it { should eql(:data_from_child_process) } + end + + context 'when block does return marshallable data' do + let(:block) do + lambda do + redirect_stderr + $stderr # not mashallable, nothing written to pipe and raised exceptions in child + end + end + + it 'raises an exception' do + expect { subject }.to raise_error(Mutant::IsolationError, 'Childprocess wrote un-unmarshallable data') + end + end + + context 'when block does return marshallable data, but process exits with nonzero exitstatus' do + let(:block) do + lambda do + redirect_stderr + at_exit do + raise + end + :foo + end + end + + it 'raises an exception' do + expect { subject }.to raise_error(Mutant::IsolationError, 'Childprocess exited with nonzero exit status: 1') + end + end + end end