Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Make sure an application is started if it's a new app and restarted i…
Browse files Browse the repository at this point in the history
…f the app already exists.
  • Loading branch information
alloy committed Jun 9, 2008
1 parent 8cfb1e9 commit 86f33e9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
13 changes: 11 additions & 2 deletions PassengerApplication.rb
Expand Up @@ -9,13 +9,16 @@ class PassengerApplication < NSObject

def init
if super_init
@new_app = true
@dirty = false
@host, @path = '', ''
self
end
end

def initWithFile(file)
if init
@new_app = false
data = File.read(file)
@host = data.match(/ServerName\s+(.+)\n/)[1]
@path = data.match(/DocumentRoot\s+"(.+)\/public"\n/)[1]
Expand All @@ -27,11 +30,11 @@ def initWithFile(file)
# p "Restarting Rails application: #{@path}"
# save_config!
# end
#

# def remove!
# p "remove #{self}"
# end
#

def save_config!
execute "/usr/bin/env ruby '#{CONFIG_INSTALLER}' '#{config_path}' '/etc/hosts' '#{@host}' '#{@path}'"
end
Expand All @@ -40,6 +43,12 @@ def config_path
@config_path ||= "#{CONFIG_PATH}/#{@host}.vhost.conf"
end

def rbSetValue_forKey(value, key)
super
@dirty = true
(@new_app ? start : restart) unless @host.empty? or @path.empty?
end

private

def execute(command)
Expand Down
59 changes: 53 additions & 6 deletions test/passenger_application_test.rb
@@ -1,25 +1,52 @@
require File.expand_path('../test_helper', __FILE__)
require 'PassengerApplication'

describe "PassengerApplication" do
describe "PassengerApplication, with a new application" do
tests PassengerApplication

def after_setup
@vhost = File.expand_path('../fixtures/blog.vhost.conf', __FILE__)
@instance_to_be_tested = PassengerApplication.alloc.initWithFile(@vhost)
@instance_to_be_tested = PassengerApplication.alloc.init
end

it "should initialize with empty path & host" do
new_app = PassengerApplication.alloc.init
new_app.path.should == ''
new_app.host.should == ''
passenger_app.path.should == ''
passenger_app.host.should == ''
assigns(:dirty).should.be false
assigns(:new_app).should.be true
end

it "should not start the application if only one of host or path is entered" do
passenger_app.expects(:start).times(0)

passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
passenger_app.setValue_forKey('', 'host')
passenger_app.setValue_forKey('/Users/het-manfred/rails code/blog', 'path')
end

it "should start the application for the first time once a valid host and path are entered" do
passenger_app.expects(:start).times(1)
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
passenger_app.setValue_forKey('/Users/het-manfred/rails code/blog', 'path')
end
end

describe "PassengerApplication, in general" do
tests PassengerApplication

def after_setup
@vhost = File.expand_path('../fixtures/blog.vhost.conf', __FILE__)
@instance_to_be_tested = PassengerApplication.alloc.initWithFile(@vhost)
end

it "should parse the correct host & path from a vhost file" do
passenger_app.host.should == "het-manfreds-blog.local"
passenger_app.path.should == "/Users/het-manfred/rails code/blog"
end

it "should set @new_app to false" do
assigns(:new_app).should.be false
end

it "should be able to execute shell with administrator permissions" do
osa = mock('NSAppleScript')
OSX::NSAppleScript.any_instance.expects(:initWithSource).with('do shell script "/requires/admin/privileges" with administrator privileges').returns(osa)
Expand All @@ -36,4 +63,24 @@ def after_setup
passenger_app.expects(:execute).with("/usr/bin/env ruby '#{PassengerApplication::CONFIG_INSTALLER}' '#{passenger_app.config_path}' '/etc/hosts' 'het-manfreds-blog.local' '/Users/het-manfred/rails code/blog'")
passenger_app.save_config!
end

it "should mark the application as dirty if a value has changed" do
passenger_app.stubs(:restart)

assigns(:dirty).should.be false
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
assigns(:dirty).should.be true
end

it "should not start the application if only one of host or path is entered" do
passenger_app.expects(:restart).times(0)

passenger_app.setValue_forKey('', 'host')
passenger_app.setValue_forKey('/Users/het-manfred/rails code/blog', 'path')
end

it "should restart the application if a valid host and path are entered" do
passenger_app.expects(:restart).times(1)
passenger_app.setValue_forKey('het-manfreds-blog.local', 'host')
end
end

0 comments on commit 86f33e9

Please sign in to comment.