Skip to content

Commit

Permalink
replace rr with rspec-mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ranmocy committed Jul 20, 2016
1 parent 6787619 commit 765c454
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 70 deletions.
1 change: 0 additions & 1 deletion guard-rails.gemspec
Expand Up @@ -22,7 +22,6 @@ Gem::Specification.new do |s|
s.add_dependency('guard-compat', '~> 1.0')

s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rr', '~> 1.0'
s.add_development_dependency 'fakefs', '~> 0.5'
s.add_development_dependency 'version', '>= 1.0.0'
end
82 changes: 40 additions & 42 deletions spec/lib/guard/rails/runner_spec.rb
Expand Up @@ -220,7 +220,7 @@
describe '#run_rails_command' do
before do
@bundler_env = ENV['BUNDLE_GEMFILE']
stub(runner).build_command.returns("printenv BUNDLE_GEMFILE > /dev/null")
allow(runner).to receive(:build_command).and_return("printenv BUNDLE_GEMFILE > /dev/null")
end
after do
ENV['BUNDLE_GEMFILE'] = @bundler_env
Expand Down Expand Up @@ -288,20 +288,18 @@
end

describe '#start' do
let(:kill_expectation) { mock(runner).kill_unmanaged_pid! }
let(:pid_stub) { stub(runner).has_pid? }

context 'without options[:zeus]' do
before do
mock(runner).wait_for_zeus.never
mock(runner).run_rails_command!.once
expect(runner).to receive(:wait_for_zeus).never
expect(runner).to receive(:run_rails_command!).once
end

context 'without options[:force_run]' do
before do
pid_stub.returns(true)
kill_expectation.never
mock(runner).wait_for_pid_action.never
allow(runner).to receive(:has_pid?).and_return(true)
expect(runner).to receive(:kill_unmanaged_pid!).never
expect(runner).to receive(:wait_for_pid_action).never
end

it "starts as normal" do
Expand All @@ -313,9 +311,9 @@
let(:options) { default_options.merge(force_run: true) }

before do
pid_stub.returns(true)
kill_expectation.once
mock(runner).wait_for_pid_action.never
allow(runner).to receive(:has_pid?).and_return(true)
expect(runner).to receive(:kill_unmanaged_pid!).once
expect(runner).to receive(:wait_for_pid_action).never
end

it "starts as normal" do
Expand All @@ -325,9 +323,9 @@

context "doesn't write the pid" do
before do
pid_stub.returns(false)
kill_expectation.never
mock(runner).wait_for_pid_action.times(Guard::Rails::Runner::MAX_WAIT_COUNT)
allow(runner).to receive(:has_pid?).and_return(false)
expect(runner).to receive(:kill_unmanaged_pid!).never
expect(runner).to receive(:wait_for_pid_action).exactly(Guard::Rails::Runner::MAX_WAIT_COUNT).times
end

it "doesn't start" do
Expand All @@ -341,36 +339,36 @@

context 'when zeus socket file is absent' do
before do
stub(runner).sleep { 1 }
mock(File).exist?(File.join(Dir.pwd, '.zeus.sock')) { false }.at_least(1)
allow(runner).to receive(:sleep).and_return(1)
expect(File).to receive(:exist?).with(File.join(Dir.pwd, '.zeus.sock')).and_return(false).at_least(1)
end

it "waits for zeus" do
expect(runner.wait_for_zeus).to be false
end

it 'returns false' do
mock(Guard::UI).info("[Guard::Rails::Error] Could not find zeus socket file.")
mock(runner).run_rails_command!.never
mock(runner).wait_for_pid.never
expect(Guard::UI).to receive(:info).with("[Guard::Rails::Error] Could not find zeus socket file.")
expect(runner).to receive(:run_rails_command!).never
expect(runner).to receive(:wait_for_pid).never
expect(runner.start).to be false
end
end

context 'when zeus socket file is present' do
before do
mock(runner).sleep(1).never
mock(File).exist?(File.join(Dir.pwd, '.zeus.sock')) { true }.once
expect(runner).to receive(:sleep).never
expect(File).to receive(:exist?).with(File.join(Dir.pwd, '.zeus.sock')).and_return(true).once
end

it "doesn't wait for zeus" do
expect(runner.wait_for_zeus).to be true
end

it 'returns true' do
# mock(runner).wait_for_zeus { true }
mock(runner).run_rails_command!.once
mock(runner).wait_for_pid.once { true }
# expect(runner).to receive(:wait_for_zeus).and_return(true)
expect(runner).to receive(:run_rails_command!).once
expect(runner).to receive(:wait_for_pid).and_return(true).once
expect(runner.start).to be true
end
end
Expand Down Expand Up @@ -399,23 +397,23 @@
end

it 'kills the process with INT' do
mock(runner).kill_process.with("INT", pid).returns { true }
stub(runner).sleep
mock(runner).kill_process.with("KILL", pid).once
expect(runner).to receive(:kill_process).with("INT", pid).and_return(true)
allow(runner).to receive(:sleep)
expect(runner).to receive(:kill_process).with("KILL", pid).once
runner.stop
end

it 'kills the process with KILL when INT not work' do
mock(runner).kill_process.with("INT", pid).returns { false }
mock(runner).kill_process.with("KILL", pid).once
expect(runner).to receive(:kill_process).with("INT", pid).and_return(false)
expect(runner).to receive(:kill_process).with("KILL", pid).once
runner.stop
end
end

context "when pid file doesn't exist" do
it 'does nothing' do
expect(runner).to receive(:kill_process).never
runner.stop
mock(runner).kill_process.never
end
end

Expand All @@ -426,7 +424,7 @@
end

it 'does nothing' do
mock(runner).kill_process.never
expect(runner).to receive(:kill_process).never
runner.stop
end

Expand All @@ -440,8 +438,8 @@

describe '#restart' do
it 'calls stop and start' do
mock(runner).stop.once
mock(runner).start.once
expect(runner).to receive(:stop).once
expect(runner).to receive(:start).once
runner.restart
end
end
Expand All @@ -450,14 +448,14 @@
let(:pid) { 12345 }

it 'kill processes if any' do
mock(runner).unmanaged_pid.returns { pid }
mock(runner).kill_process.with("KILL", pid).once
expect(runner).to receive(:unmanaged_pid).and_return(pid)
expect(runner).to receive(:kill_process).with("KILL", pid).once
runner.send(:kill_unmanaged_pid!)
end

it 'does nothing if none' do
mock(runner).unmanaged_pid.returns { nil }
mock(runner).kill_process.with("KILL", pid).never
expect(runner).to receive(:unmanaged_pid).and_return(nil)
expect(runner).to receive(:kill_process).with("KILL", pid).never
runner.send(:kill_unmanaged_pid!)
end
end
Expand All @@ -466,14 +464,14 @@
let(:pid) { 12345 }

it 'returns pid if any' do
mock(runner, :'`').with(anything).returns {
expect(runner).to receive(:'`').with(anything) {
"ruby #{pid} ranmocy 12u IPv4 0x9c30720e04d31a0f 0t0 TCP *:#{default_port} (LISTEN)"
}
expect(runner.send(:unmanaged_pid)).to eq pid
end

it 'returns nil if none' do
mock(runner, :'`').with(anything).returns { "\n" }
expect(runner).to receive(:'`').with(anything).and_return("\n")
expect(runner.send(:unmanaged_pid)).to be nil
end
end
Expand All @@ -484,7 +482,7 @@
let(:pid) { 12345 }

it 'returns true if killed' do
mock(Process).kill(signal, pid) { 0 }
expect(Process).to receive(:kill).with(signal, pid).and_return(0)
expect(runner.send(:kill_process, signal, pid)).to be true
end

Expand All @@ -493,8 +491,8 @@
end

it 'returns false if no permission to kill' do
mock(Process).kill(signal, pid) { raise Errno::EPERM }
mock(Guard::UI).info("[Guard::Rails::Error] Don't have permission to KILL!")
expect(Process).to receive(:kill).with(signal, pid).and_raise(Errno::EPERM)
expect(Guard::UI).to receive(:info).with("[Guard::Rails::Error] Don't have permission to KILL!")
expect(runner.send(:kill_process, signal, pid)).to be false
end
end
Expand Down
48 changes: 25 additions & 23 deletions spec/lib/guard/rails_spec.rb
Expand Up @@ -14,12 +14,14 @@
end

describe "#start" do
let(:ui_expectation) { mock(Guard::UI).info.with(/#{Guard::Rails::DEFAULT_OPTIONS[:port]}/) }
let(:expect_ui_update) {
expect(Guard::UI).to receive(:info).with(/#{Guard::Rails::DEFAULT_OPTIONS[:port]}/)
}

context "starts when Guard starts" do
it "shows the right message and runs startup" do
mock(guard).reload.with("start").once
ui_expectation
expect(guard).to receive(:reload).with("start").once
expect_ui_update
guard.start
end
end
Expand All @@ -28,8 +30,8 @@
let(:options) { { start_on_start: false } }

it "shows the right message and doesn't run startup" do
mock(guard).reload.never
ui_expectation
expect(guard).to receive(:reload).never
expect_ui_update
guard.start
end
end
Expand All @@ -39,53 +41,53 @@
let(:pid) { '12345' }

before do
any_instance_of(Guard::Rails::Runner, pid: pid)
allow_any_instance_of(Guard::Rails::Runner).to receive(:pid).and_return(pid)
end

context 'at start' do
before do
mock(Guard::UI).info.with('Starting Rails...')
mock(Guard::Notifier).notify.with(/Rails starting/, hash_including(image: :pending))
any_instance_of(Guard::Rails::Runner, restart: true)
expect(Guard::UI).to receive(:info).with('Starting Rails...')
expect(Guard::Notifier).to receive(:notify).with(/Rails starting/, hash_including(image: :pending))
allow_any_instance_of(Guard::Rails::Runner).to receive(:restart).and_return(true)
end

it "starts and shows the pid file" do
mock(Guard::UI).info.with(/#{pid}/)
mock(Guard::Notifier).notify.with(/Rails started/, hash_including(image: :success))
expect(Guard::UI).to receive(:info).with(/#{pid}/)
expect(Guard::Notifier).to receive(:notify).with(/Rails started/, hash_including(image: :success))

guard.reload("start")
end
end

context "after start" do
before do
any_instance_of(Guard::Rails::Runner, pid: pid)
mock(Guard::UI).info.with('Restarting Rails...')
mock(Guard::Notifier).notify.with(/Rails restarting/, hash_including(image: :pending))
allow_any_instance_of(Guard::Rails::Runner).to receive(:pid).and_return(pid)
expect(Guard::UI).to receive(:info).with('Restarting Rails...')
expect(Guard::Notifier).to receive(:notify).with(/Rails restarting/, hash_including(image: :pending))
end

context "with pid file" do
before do
any_instance_of(Guard::Rails::Runner, restart: true)
allow_any_instance_of(Guard::Rails::Runner).to receive(:restart).and_return(true)
end

it "restarts and shows the pid file" do
mock(Guard::UI).info.with(/#{pid}/)
mock(Guard::Notifier).notify.with(/Rails restarted/, hash_including(image: :success))
expect(Guard::UI).to receive(:info).with(/#{pid}/)
expect(Guard::Notifier).to receive(:notify).with(/Rails restarted/, hash_including(image: :success))

guard.reload
end
end

context "without pid file" do
before do
any_instance_of(Guard::Rails::Runner, restart: false)
allow_any_instance_of(Guard::Rails::Runner).to receive(:restart).and_return(false)
end

it "restarts and shows the pid file" do
mock(Guard::UI).info.with(/#{pid}/).never
mock(Guard::UI).info.with(/Rails NOT restarted/)
mock(Guard::Notifier).notify.with(/Rails NOT restarted/, hash_including(image: :failed))
expect(Guard::UI).to receive(:info).with(/#{pid}/).never
expect(Guard::UI).to receive(:info).with(/Rails NOT restarted/)
expect(Guard::Notifier).to receive(:notify).with(/Rails NOT restarted/, hash_including(image: :failed))

guard.reload
end
Expand All @@ -95,14 +97,14 @@

describe "#stop" do
it "stops with correct message" do
mock(Guard::Notifier).notify.with('Until next time...', anything)
expect(Guard::Notifier).to receive(:notify).with('Until next time...', anything)
guard.stop
end
end

describe '#run_on_change' do
it "reloads on change" do
mock(guard).reload.once
expect(guard).to receive(:reload).once
guard.run_on_change([])
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Expand Up @@ -8,7 +8,3 @@

require 'guard/compat/test/helper' # test helper for Guard plugin
require 'guard/rails'

RSpec.configure do |c|
c.mock_with :rr
end

0 comments on commit 765c454

Please sign in to comment.