Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DRY up code, fix fail/pending test cases

  • Loading branch information...
commit bc95ac572bf5365744f8e9d579ce05fd755638c0 1 parent fd6f545
@chtrinh authored
View
3  Gemfile
@@ -1,4 +1,7 @@
source :rubygems
gemspec
+gem "ruby-debug", :platforms => :mri_18
+gem "ruby-debug19", :platforms => :mri_19
+
gem "autotest", ">= 0"
View
24 Gemfile.lock
@@ -12,8 +12,10 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.2.8)
+ archive-tar-minitar (0.5.2)
autotest (4.4.6)
ZenTest (>= 4.4.1)
+ columnize (0.3.6)
diff-lcs (1.1.3)
heroku (2.25.0)
launchy (>= 0.3.2)
@@ -23,9 +25,14 @@ GEM
i18n (0.6.0)
launchy (2.1.0)
addressable (~> 2.2.6)
+ linecache (0.46)
+ rbx-require-relative (> 0.0.4)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
mime-types (1.18)
multi_json (1.3.4)
netrc (0.7.1)
+ rbx-require-relative (0.0.9)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.10.0)
@@ -36,6 +43,21 @@ GEM
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
+ ruby-debug (0.10.4)
+ columnize (>= 0.1)
+ ruby-debug-base (~> 0.10.4.0)
+ ruby-debug-base (0.10.4)
+ linecache (>= 0.3)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
rubyzip (0.9.8)
PLATFORMS
@@ -46,3 +68,5 @@ DEPENDENCIES
autotest
heroku-rails-saas!
rspec (~> 2.0)
+ ruby-debug
+ ruby-debug19
View
69 lib/heroku-rails-saas/config.rb
@@ -45,6 +45,14 @@ def app_names
apps.keys
end
+ def cmd(app_env)
+ if self.stack(app_env) =~ /cedar/i
+ 'heroku run '
+ else
+ 'heroku '
+ end
+ end
+
# Returns the app name on heroku froma string format like so: `app:env`
# Allows for `rake <app:env> [<app:env>] <command>`
def app_name_on_heroku(string)
@@ -74,63 +82,38 @@ def stack(app_env)
stacks[name].try("[]", env) || stacks['all']
end
- def cmd(app_env)
- if self.stack(app_env) =~ /cedar/i
- 'heroku run '
- else
- 'heroku '
- end
+ # return a list of domains for a particular app environment
+ def domains(app_env)
+ name, env = app_env.split(SEPERATOR)
+ domains = self.settings['domains'] || {}
+ domains[name].try("[]", env) || []
end
# pull out the config setting hash for a particular app environment
def config(app_env)
- name, env = app_env.split(SEPERATOR)
- config = self.settings['config'] || {}
- all = config['all'] || {}
-
- app_configs = (config[name] && config[name].reject { |k,v| v.class == Hash }) || {}
- # overwrite app configs with the environment specific ones
- merged_environment_configs = app_configs.merge((config[name] && config[name][env]) || {})
-
- # overwrite all configs with the environment specific ones
- all.merge(merged_environment_configs)
+ app_setting_hash("config", app_env)
end
# pull out the scaling setting hash for a particular app environment
def scale(app_env)
- name, env = app_env.split(SEPERATOR)
- scaling = self.settings['scale'] || {}
- all = scaling['all'] || {}
-
- app_scaling = (scaling[name] && scaling[name].reject { |k,v| v.class == Hash }) || {}
- # overwrite app scaling with the environment specific ones
- merged_environment_scaling = app_scaling.merge((scaling[name] && scaling[name][env]) || {})
-
- # overwrite all scaling with the environment specific ones
- all.merge(merged_environment_scaling)
+ app_setting_hash("scale", app_env)
end
- # return a list of domains for a particular app environment
- def domains(app_env)
- name, env = app_env.split(SEPERATOR)
- domains = self.settings['domains'] || {}
- domains[name].try("[]", env) || []
- end
# return a list of collaborators for a particular app environment
def collaborators(app_env)
- app_setting_list('collaborators', app_env)
+ app_setting_array('collaborators', app_env)
end
# return a list of addons for a particular app environment
def addons(app_env)
- app_setting_list('addons', app_env)
+ app_setting_array('addons', app_env)
end
private
- # Add app specific settings to the default ones defined in all
- def app_setting_list(setting_key, app_env)
+ # Add app specific settings to the default ones defined in all for an array listing
+ def app_setting_array(setting_key, app_env)
name, env = app_env.split(SEPERATOR)
setting = self.settings[setting_key] || {}
default = setting['all'] || []
@@ -139,6 +122,20 @@ def app_setting_list(setting_key, app_env)
(default + app_settings).uniq
end
+ # Add app specific settings to the default ones defined in all for a hash listing
+ def app_setting_hash(setting_key, app_env)
+ name, env = app_env.split(SEPERATOR)
+ config = self.settings[setting_key] || {}
+ all = config['all'] || {}
+
+ app_configs = (config[name] && config[name].reject { |k,v| v.class == Hash }) || {}
+ # overwrite app settings with the environment specific ones
+ merged_environment_configs = app_configs.merge((config[name] && config[name][env]) || {})
+
+ # overwrite all settings with the environment specific ones
+ all.merge(merged_environment_configs)
+ end
+
def parse_yml(config_filepath, options)
if File.exists?(config_filepath)
config_hash = YAML.load(ERB.new(File.read(config_filepath)).result)
View
2  lib/heroku-rails-saas/runner.rb
@@ -1,3 +1,4 @@
+require 'active_support/core_ext/object/blank'
require 'heroku/client'
module HerokuRailsSaas
@@ -223,7 +224,6 @@ def scale
# cycles through each configured heroku app
# yields the environment name, the app name, and the repo url
def each_heroku_app
-
if @config.apps.size == 0
puts "\nNo heroku apps are configured. Run:
rails generate heroku:config\n\n"
View
5 spec/fixtures/mediocreapp.yml
@@ -12,3 +12,8 @@ collaborators:
domains:
development:
- "mediocreapp.com"
+
+scale:
+ development:
+ web: 1
+ worker: 0
View
2  spec/heroku/rails/saas/heroku_config_spec.rb
@@ -156,7 +156,7 @@ module HerokuRailsSaas
describe "#scale" do
context "mediocrapp" do
it "should include the scaling settings defined in 'all'" do
- @scale = @config.scale('mediocreapp')
+ @scale = @config.scale('mediocreapp:development')
@scale['web'].should_not be_nil
@scale['worker'].should_not be_nil
@scale['web'].should eql 1
View
16 spec/heroku/rails/saas/heroku_runner_spec.rb
@@ -2,13 +2,21 @@
module HerokuRailsSaas
describe Runner do
- describe "each_heroku_app" do
- it "should return my apps" do
- pending
+ describe "#each_heroku_app" do
+ it "should return all apps in all environments" do
+ config_files = {:default => config_path("heroku-config.yml"), :apps => [config_path("awesomeapp.yml"), config_path("mediocreapp.yml")]}
+ config = Config.new(config_files)
+ runner = Runner.new(config)
+ runner.all_environments
+ runner.each_heroku_app {}.should == ["awesomeapp:production", "awesomeapp:staging", "mediocreapp:development"]
end
it "should not return a production app if @environment is not specified and there's only one app" do
- pending
+ config_files = {:default => config_path("heroku-config.yml"), :apps => [config_path("mediocreapp.yml")]}
+ config = Config.new(config_files)
+ runner = Runner.new(config)
+ runner.instance_variable_set("@environments", [])
+ lambda { runner.each_heroku_app }.should raise_error(SystemExit)
end
end
end
View
28 spec/spec_helper.rb
@@ -2,16 +2,38 @@
require 'bundler/setup'
-RSpec.configure do |c|
- # setup fixtures path
- c.before(:all) do
+RSpec.configure do |config|
+ config.before(:all) do
@fixture_path = Pathname.new(File.join(File.dirname(__FILE__), "/fixtures"))
raise "Fixture folder not found: #{@fixture_path}" unless @fixture_path.directory?
+ silence_output
+ end
+
+ config.after(:all) do
+ enable_output
end
# returns the file path of a fixture setting file
def config_path(filename)
@fixture_path.join(filename)
end
+
+ # Redirects stderr and stdout to /dev/null.
+ def silence_output
+ @orig_stderr = $stderr
+ @orig_stdout = $stdout
+
+ # redirect stderr and stdout to /dev/null
+ $stderr = File.new('/dev/null', 'w')
+ $stdout = File.new('/dev/null', 'w')
+ end
+
+ # Replace stdout and stderr so anything else is output correctly.
+ def enable_output
+ $stderr = @orig_stderr
+ $stdout = @orig_stdout
+ @orig_stderr = nil
+ @orig_stdout = nil
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.