diff --git a/lib/sunspot_test.rb b/lib/sunspot_test.rb index b89f3cc..00bcb41 100644 --- a/lib/sunspot_test.rb +++ b/lib/sunspot_test.rb @@ -1,4 +1,5 @@ require 'sunspot/rails' +require 'sunspot/version' require 'net/http' module SunspotTest @@ -23,13 +24,19 @@ def server def start_sunspot_server unless solr_running? - pid = fork do - STDERR.reopen("/dev/null") - STDOUT.reopen("/dev/null") - server.run + if Sunspot::VERSION > '2.2.7' + server.start + else + suppress_output($stderr) do + server.start + end end - at_exit { Process.kill("TERM", pid) } + at_exit do + suppress_output($stdout) do + server.stop + end + end wait_until_solr_starts end @@ -53,6 +60,14 @@ def unstub private + def suppress_output(output) + original_output = output.clone + output.reopen('/dev/null') + yield + ensure + output.reopen(original_output) + end + def original_sunspot_session @original_sunspot_session ||= Sunspot.session end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 330b338..b9fdda3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,3 +3,7 @@ require 'sunspot' require 'sunspot_test' + +RSpec.configure do |c| + c.raise_errors_for_deprecations! +end diff --git a/spec/sunspot_test_spec.rb b/spec/sunspot_test_spec.rb index 2dc2986..0223700 100644 --- a/spec/sunspot_test_spec.rb +++ b/spec/sunspot_test_spec.rb @@ -54,17 +54,17 @@ end # was getting funky issues when the test was broken up - it "forks the process, redirects stdout/stderr, runs server, sets exit hook, waits for server" do - - expect(SunspotTest).to receive(:fork) do |&block| - expect(STDERR).to receive(:reopen).with("/dev/null") - expect(STDOUT).to receive(:reopen).with("/dev/null") - expect(SunspotTest.server).to receive(:run) - block.call + it 'forks the process, redirects stdout/stderr, runs server, sets exit hook, waits for server' do + expect(SunspotTest).to receive(:suppress_output).with($stderr) do |&block| + expect(SunspotTest.server).to receive(:start) + block.call($stderr) end expect(SunspotTest).to receive(:at_exit) do |&block| - expect(Process).to receive(:kill) + expect(SunspotTest).to receive(:suppress_output).with($stdout) do |&block| + expect(SunspotTest.server).to receive(:stop) + block.call($stdout) + end block.call end @@ -136,7 +136,7 @@ describe ".solr_running" do context "if solr is running" do before do - Net::HTTP.stub(get_response: double(code: '200')) + allow(Net::HTTP).to receive_messages(get_response: double(code: '200')) end it "returns true" do @@ -152,7 +152,7 @@ context "if solr is starting up" do before do - Net::HTTP.stub(get_response:double(code: '503')) + allow(Net::HTTP).to receive_messages(get_response: double(code: '503')) end it "returns false" do @@ -162,6 +162,14 @@ end + describe ".suppress_output" do + it "restors default" do + original_class = $stdout.class + SunspotTest.send(:suppress_output, $stdout) { puts('this should not show up') } + expect($stdout.class).to eq(original_class) + end + end + describe ".wait_until_solr_starts" do context "if solr never starts" do before do