Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
more better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbintz committed Jun 1, 2011
1 parent bf2eaf9 commit 99d8f28
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ source "http://rubygems.org"
gemspec
gem 'rake', '0.8.7'
gem 'growl'
gem 'fakefs', :require => nil
12 changes: 8 additions & 4 deletions lib/guard/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ def start
end

def run_all
UI.info "Restarting Rails"
UI.info "Restarting Rails..."
Notifier.notify("Rails restarting on port #{options[:port]} in #{options[:environment]} environment...", :title => "Restarting Rails...", :image => :pending)
runner.restart
UI.info "Rails restarted, pid #{File.read(pid_file)}"
Notifier.notify("Rails restarted on port #{options[:port]}.", :title => "Rails restarted!", :image => :success)
if runner.restart
UI.info "Rails restarted, pid #{runner.pid}"
Notifier.notify("Rails restarted on port #{options[:port]}.", :title => "Rails restarted!", :image => :success)
else
UI.info "Rails NOT restarted, check your log files."
Notifier.notify("Rails NOT restarted, check your log files.", :title => "Rails NOT restarted!", :image => :failure)
end
end

def stop
Expand Down
13 changes: 9 additions & 4 deletions lib/guard/rails/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def start
wait_for_pid_action
count += 1
end
!(count == 10)
end

def stop
Expand All @@ -39,6 +40,14 @@ def build_rails_command
%{sh -c 'cd #{Dir.pwd} && rails s #{rails_options.join(' ')} &'}
end

def pid_file
File.expand_path("tmp/pids/#{options[:environment]}.pid")
end

def pid
File.file?(pid_file) ? File.read(pid_file).to_i : nil
end

private
def run_rails_command!
system build_rails_command
Expand All @@ -52,10 +61,6 @@ def wait_for_pid_action
0.5
end

def pid_file
File.expand_path("tmp/pids/#{options[:environment]}.pid")
end

def kill_unmanaged_pid!
if pid = unmanaged_pid
system %{kill -INT #{pid}}
Expand Down
28 changes: 25 additions & 3 deletions spec/lib/guard/rails/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'guard/rails/runner'
require 'fakefs/spec_helpers'

describe Guard::RailsRunner do
let(:runner) { Guard::RailsRunner.new(options) }
Expand All @@ -9,6 +10,27 @@
let(:default_options) { { :environment => environment, :port => port } }
let(:options) { default_options }

describe '#pid' do
include FakeFS::SpecHelpers

context 'pid file exists' do
let(:pid) { 12345 }

before do
File.open(runner.pid_file, 'w') { |fh| fh.print pid }
end

it "should read the pid" do
runner.pid.should == pid
end
end

context 'pid file does not exist' do
it "should return nil" do
runner.pid.should be_nil
end
end
end

describe '#build_rails_command' do
context 'no daemon' do
Expand Down Expand Up @@ -42,7 +64,7 @@
end

it "should act properly" do
runner.start
runner.start.should be_true
end
end

Expand All @@ -56,7 +78,7 @@
end

it "should act properly" do
runner.start
runner.start.should be_true
end
end

Expand All @@ -68,7 +90,7 @@
end

it "should act properly" do
runner.start
runner.start.should be_false
end
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/guard/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,44 @@
guard
end
end

describe '#run_all' do
let(:pid) { '12345' }

before do
Guard::UI.expects(:info).with('Restarting Rails...')
Guard::Notifier.expects(:notify).with(regexp_matches(/Rails restarting/), anything)
Guard::RailsRunner.any_instance.stubs(:pid).returns(pid)
end

let(:runner_stub) { Guard::RailsRunner.any_instance.stubs(:restart) }

context 'with pid file' do
before do
runner_stub.returns(true)
end

it "should restart and show the pid file" do
Guard::UI.expects(:info).with(regexp_matches(/#{pid}/))
Guard::Notifier.expects(:notify).with(regexp_matches(/Rails restarted/), anything)

guard.run_all
end
end

context 'no pid file' do
before do
runner_stub.returns(false)
end

it "should restart and show the pid file" do
Guard::UI.expects(:info).with(regexp_matches(/#{pid}/)).never
Guard::UI.expects(:info).with(regexp_matches(/Rails NOT restarted/))
Guard::Notifier.expects(:notify).with(regexp_matches(/Rails NOT restarted/), anything)

guard.run_all
end
end
end
end

0 comments on commit 99d8f28

Please sign in to comment.