diff --git a/lib/moonshine/manifest.rb b/lib/moonshine/manifest.rb index ee0d5962..55f1fd9c 100644 --- a/lib/moonshine/manifest.rb +++ b/lib/moonshine/manifest.rb @@ -53,6 +53,11 @@ def self.rails_env ENV["RAILS_ENV"] || 'production' end + # HAX for cases where we evaluate ERB that refers to Rails.env + def self.env + rails_env + end + # The current environment's database configuration def database_environment configuration[:database][rails_env.to_sym] @@ -121,11 +126,11 @@ def on_stage(*args) end end - def local_template_dir + def self.local_template_dir @local_template_dir ||= rails_root.join('app/manifests/templates') end - def local_template(pathname) + def self.local_template(pathname) (local_template_dir + pathname.basename).expand_path end @@ -133,7 +138,7 @@ def local_template(pathname) # with the same basename at RAILS_ROOT/app/manifests/templates, it # is used instead. This is useful to override templates provided by plugins # to customize application configuration files. - def template(pathname, b = binding) + def self.template(pathname, b = binding) pathname = Pathname.new(pathname) unless pathname.kind_of?(Pathname) template_contents = if local_template(pathname).exist? @@ -146,6 +151,10 @@ def template(pathname, b = binding) ERB.new(template_contents).result(b) end + def template(pathname, b = binding) + self.class.template(pathname, b) + end + # config/moonshine.yml if moonshine_yml.exist? configure(YAML::load(ERB.new(moonshine_yml.read).result)) diff --git a/test/moonshine/test_manifest.rb b/test/moonshine/test_manifest.rb index 7c5735fc..4104b680 100644 --- a/test/moonshine/test_manifest.rb +++ b/test/moonshine/test_manifest.rb @@ -5,6 +5,12 @@ module Moonshine::Iptables class Moonshine::ManifestTest < Test::Unit::TestCase + def teardown + if @manifest && application_template && application_template.exist? + application_template.delete + end + end + def test_loads_configuration assert_not_nil Moonshine::Manifest.configuration assert_not_nil Moonshine::Manifest.configuration[:application] @@ -15,36 +21,33 @@ def test_loads_environment_specific_configuration end def test_moonshine_templates - @manifest = Moonshine::Manifest.new + @manifest = Moonshine::Manifest::Rails.new @manifest.configure(:application => 'bar') - moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'templates', 'passenger.conf.erb') + moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'manifest', 'rails', 'templates', 'passenger.vhost.erb') template_contents = 'moonshine template: <%= configuration[:application] %>' - moonshine_template.expects(:exist?).returns(true) - moonshine_template.expects(:read).returns(template_contents) - - application_template = @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb') - application_template.expects(:exist?).returns(false) - @manifest.stubs(:local_template).returns(application_template) - assert_equal 'moonshine template: bar', @manifest.template(moonshine_template) + assert_match 'ServerName yourapp.com', @manifest.template(moonshine_template) + end + + def application_template + @application_template ||= @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb') end def test_app_templates_override_moonshine_templates @manifest = Moonshine::Manifest.new @manifest.configure(:application => 'bar') - moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'templates', 'passenger.conf.erb') + application_template.open('w') {|f| f.write "application template: <%= configuration[:application] %>" } + moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'manifest', 'rails', 'templates', 'passenger.conf.erb') application_template = @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb') - template_contents = 'application template: <%= configuration[:application] %>' - application_template.expects(:exist?).returns(true) - application_template.expects(:read).returns(template_contents) - - @manifest.stubs(:local_template).returns(application_template) + assert application_template.exist?, "#{application_template} should exist, but didn't" + assert moonshine_template.exist?, "#{moonshine_template} should exist, but didn't" - assert_equal 'application template: bar', @manifest.template(moonshine_template) + # should return the output from that existing thing + assert_match 'application template: bar', @manifest.template(moonshine_template) end def test_loads_plugins