diff --git a/lib/foreman.rb b/lib/foreman.rb index 63140f9..265d799 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -2,5 +2,7 @@ module Foreman VERSION = "0.0.1" + class AppDoesNotExist < Exception; end + end diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 4519209..23dc367 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -29,7 +29,11 @@ class Foreman::CLI < Thor desc "scale APP PROCESS AMOUNT", "Change the concurrency of a given process type" def scale(app, process, amount) - Foreman::Configuration.new(app).scale(process, amount) + config = Foreman::Configuration.new(app) + error "No such process: #{process}." unless config.processes[process] + config.scale(process, amount) + rescue Foreman::AppDoesNotExist + error "No such app: #{app}." end private ###################################################################### diff --git a/lib/foreman/configuration.rb b/lib/foreman/configuration.rb index 949c2f5..5479f0a 100644 --- a/lib/foreman/configuration.rb +++ b/lib/foreman/configuration.rb @@ -12,7 +12,7 @@ class Foreman::Configuration end def scale(process, amount) - old_amount = processes[process] + old_amount = processes[process].to_i processes[process] = amount.to_i amount = amount.to_i @@ -43,6 +43,8 @@ private ###################################################################### config["#{app}_processes"].split(" ").each do |process| processes[process] = config["#{app}_#{process}"].to_i end + rescue Errno::ENOENT + raise Foreman::AppDoesNotExist end def run(command) diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 0641dc0..094e36c 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -61,15 +61,26 @@ describe "Foreman::CLI" do describe "scale" do describe "without an existing configuration" do - # TODO + it "displays an error" do + mock_error(subject, "No such app: testapp.") do + subject.scale("testapp", "alpha", "2") + end + end end describe "with an existing configuration" do before(:each) { write_foreman_config("testapp") } - it "scales the specified process" do - mock.instance_of(Foreman::Configuration).scale("testprocess", "2") - subject.scale("testapp", "testprocess", "2") + it "scales a process that exists" do + mock.instance_of(Foreman::Configuration).scale("alpha", "2") + subject.scale("testapp", "alpha", "2") + end + + it "errors if a process that does not exist is specified" do + mock_error(subject, "No such process: invalidprocess.") do + dont_allow.instance_of(Foreman::Configuration).scale + subject.scale("testapp", "invalidprocess", "2") + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index afa5f75..2c4b9c8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,19 +3,6 @@ require "rspec" $:.unshift "lib" -# def stub_configuration -# config = mock(Foreman::Configuration) -# Foreman::Configuration.stub(:new).and_return(config) -# config -# end -# -# -# def stub_export_upstart -# upstart = mock(Foreman::Export::Upstart) -# Foreman::Export::Upstart.stub(:new).and_return(upstart) -# upstart -# end - def mock_error(subject, message) mock_exit do mock(subject).puts("ERROR: #{message}") @@ -27,17 +14,11 @@ def mock_exit(&block) block.should raise_error(SystemExit) end -# def stub_engine -# engine = mock(Foreman::Engine) -# stub(Foreman::Engine).new { engine } -# engine -# end -# def write_foreman_config(app) File.open("/etc/foreman/#{app}.conf", "w") do |file| file.puts %{#{app}_processes="alpha bravo"} file.puts %{#{app}_alpha="1"} - file.puts %{#{app}_bravo="2"} + file.puts %{#{app}_bravo="2"} end end