Permalink
Browse files

Logger and version tests. Lay the actor class to rest.

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6315 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent bdef53c commit f74ab735ed2f45ab229d43a2a4f43a577a765b03 @jamis jamis committed Mar 4, 2007
Showing with 189 additions and 104 deletions.
  1. +8 −1 Rakefile
  2. +0 −70 lib/capistrano/actor.rb
  3. +10 −10 lib/capistrano/logger.rb
  4. +4 −5 lib/capistrano/shell.rb
  5. +7 −15 lib/capistrano/version.rb
  6. +12 −0 test/cli_test.rb
  7. +1 −3 test/configuration_test.rb
  8. +123 −0 test/logger_test.rb
  9. +24 −0 test/version_test.rb
View
9 Rakefile
@@ -23,6 +23,13 @@ Rake::TestTask.new do |t|
t.verbose = true
end
+desc "Run code-coverage analysis using rcov"
+task :coverage do
+ rm_rf "coverage"
+ files = Dir["test/**/*_test.rb"]
+ system "rcov #{files.join(' ')}"
+end
+
GEM_SPEC = eval(File.read("#{File.dirname(__FILE__)}/#{PKG_NAME}.gemspec"))
Rake::GemPackageTask.new(GEM_SPEC) do |p|
@@ -35,7 +42,7 @@ desc "Build the RDoc API documentation"
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = "doc"
rdoc.title = "Capistrano -- A framework for remote command execution"
- rdoc.options << '--line-numbers --inline-source --main README'
+ rdoc.options += %w(--line-numbers --inline-source --main README)
rdoc.rdoc_files.include 'README'
rdoc.rdoc_files.include 'lib/**/*.rb'
rdoc.template = "jamis"
View
70 lib/capistrano/actor.rb
@@ -1,70 +0,0 @@
-require 'erb'
-require 'capistrano/command'
-require 'capistrano/transfer'
-require 'capistrano/gateway'
-require 'capistrano/ssh'
-require 'capistrano/utils'
-
-module Capistrano
-
- # An Actor is the entity that actually does the work of determining which
- # servers should be the target of a particular task, and of executing the
- # task on each of them in parallel. An Actor is never instantiated
- # directly--rather, you create a new Configuration instance, and access the
- # new actor via Configuration#actor.
- class Actor
- # Renders an ERb template and returns the result. This is useful for
- # dynamically building documents to store on the remote servers.
- #
- # Usage:
- #
- # render("something", :foo => "hello")
- # look for "something.rhtml" in the current directory, or in the
- # capistrano/recipes/templates directory, and render it with
- # foo defined as a local variable with the value "hello".
- #
- # render(:file => "something", :foo => "hello")
- # same as above
- #
- # render(:template => "<%= foo %> world", :foo => "hello")
- # treat the given string as an ERb template and render it with
- # the given hash of local variables active.
- def render(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
- options[:file] = args.shift if args.first.is_a?(String)
- raise ArgumentError, "too many parameters" unless args.empty?
-
- case
- when options[:file]
- file = options.delete :file
- unless file[0] == ?/
- dirs = [".",
- File.join(File.dirname(__FILE__), "recipes", "templates")]
- dirs.each do |dir|
- if File.file?(File.join(dir, file))
- file = File.join(dir, file)
- break
- elsif File.file?(File.join(dir, file + ".rhtml"))
- file = File.join(dir, file + ".rhtml")
- break
- end
- end
- end
-
- render options.merge(:template => File.read(file))
-
- when options[:template]
- erb = ERB.new(options[:template])
- b = Proc.new { binding }.call
- options.each do |key, value|
- next if key == :template
- eval "#{key} = options[:#{key}]", b
- end
- erb.result(b)
-
- else
- raise ArgumentError, "no file or template given for rendering"
- end
- end
- end
-end
View
20 lib/capistrano/logger.rb
@@ -1,6 +1,7 @@
module Capistrano
class Logger #:nodoc:
attr_accessor :level
+ attr_reader :device
IMPORTANT = 0
INFO = 1
@@ -11,30 +12,29 @@ class Logger #:nodoc:
def initialize(options={})
output = options[:output] || STDERR
- case
- when output.respond_to?(:puts)
- @device = output
- else
- @device = File.open(output.to_str, "a")
- @needs_close = true
+ if output.respond_to?(:puts)
+ @device = output
+ else
+ @device = File.open(output.to_str, "a")
+ @needs_close = true
end
@options = options
@level = 0
end
def close
- @device.close if @needs_close
+ device.close if @needs_close
end
def log(level, message, line_prefix=nil)
if level <= self.level
indent = "%*s" % [MAX_LEVEL, "*" * (MAX_LEVEL - level)]
- message.split(/\r?\n/).each do |line|
+ message.each do |line|
if line_prefix
- @device.print "#{indent} [#{line_prefix}] #{line.strip}\n"
+ device.puts "#{indent} [#{line_prefix}] #{line.strip}\n"
else
- @device.puts "#{indent} #{line.strip}\n"
+ device.puts "#{indent} #{line.strip}\n"
end
end
end
View
9 lib/capistrano/shell.rb
@@ -1,11 +1,10 @@
require 'thread'
-# The Capistrano::Shell class is the guts of the "shell" task. It implements
-# an interactive REPL interface that users can employ to execute tasks and
-# commands. It makes for a GREAT way to monitor systems, and perform quick
-# maintenance on one or more machines.
-
module Capistrano
+ # The Capistrano::Shell class is the guts of the "shell" task. It implements
+ # an interactive REPL interface that users can employ to execute tasks and
+ # commands. It makes for a GREAT way to monitor systems, and perform quick
+ # maintenance on one or more machines.
class Shell
# The actor instance employed by this shell
attr_reader :actor
View
22 lib/capistrano/version.rb
@@ -1,21 +1,13 @@
module Capistrano
module Version #:nodoc:
# A method for comparing versions of required modules. It expects two
- # arrays as parameters, and returns true if the first is no more than the
- # second.
- def self.check(expected, actual) #:nodoc:
- good = false
- if actual[0] > expected[0]
- good = true
- elsif actual[0] == expected[0]
- if actual[1] > expected[1]
- good = true
- elsif actual[1] == expected[1] && actual[2] >= expected[2]
- good = true
- end
- end
-
- good
+ # arrays of integers as parameters, the first being the minimum version
+ # required, and the second being the actual version available. It returns
+ # true if the actual version is at least equal to the required version.
+ def self.check(required, actual) #:nodoc:
+ required = required.map { |v| "%06d" % v }.join(".")
+ actual = actual.map { |v| "%06d" % v }.join(".")
+ return actual >= required
end
MAJOR = 1
View
12 test/cli_test.rb
@@ -0,0 +1,12 @@
+require "#{File.dirname(__FILE__)}/utils"
+require 'capistrano/cli'
+
+class CLI_Test < Test::Unit::TestCase
+ def setup
+ @cli = Capistrano::CLI.new([])
+ end
+
+ def test_flunk
+ flunk
+ end
+end
View
4 test/configuration_test.rb
@@ -1,7 +1,5 @@
require "#{File.dirname(__FILE__)}/utils"
-# if the following is uncommented, the capistrano gem gets loaded if it is
-# installed, for some reason...not sure why :(
-# require 'capistrano/configuration'
+require 'capistrano/configuration'
class ConfigurationTest < Test::Unit::TestCase
def setup
View
123 test/logger_test.rb
@@ -0,0 +1,123 @@
+require "#{File.dirname(__FILE__)}/utils"
+require 'capistrano/logger'
+require 'stringio'
+
+class LoggerTest < Test::Unit::TestCase
+ def setup
+ @io = StringIO.new
+ @logger = Capistrano::Logger.new(:output => @io)
+ end
+
+ def test_logger_should_use_STDERR_by_default
+ logger = Capistrano::Logger.new
+ assert_equal STDERR, logger.device
+ end
+
+ def test_logger_should_use_output_option_if_output_responds_to_puts
+ logger = Capistrano::Logger.new(:output => STDOUT)
+ assert_equal STDOUT, logger.device
+ end
+
+ def test_logger_should_open_file_if_output_does_not_respond_to_puts
+ File.expects(:open).with("logs/capistrano.log", "a").returns(:mock)
+ logger = Capistrano::Logger.new(:output => "logs/capistrano.log")
+ assert_equal :mock, logger.device
+ end
+
+ def test_close_should_not_close_device_if_device_is_default
+ logger = Capistrano::Logger.new
+ logger.device.expects(:close).never
+ logger.close
+ end
+
+ def test_close_should_not_close_device_is_device_is_explicitly_given
+ logger = Capistrano::Logger.new(:output => STDOUT)
+ STDOUT.expects(:close).never
+ logger.close
+ end
+
+ def test_close_should_close_device_when_device_was_implicitly_opened
+ f = mock("file", :close => nil)
+ File.expects(:open).with("logs/capistrano.log", "a").returns(f)
+ logger = Capistrano::Logger.new(:output => "logs/capistrano.log")
+ logger.close
+ end
+
+ def test_log_with_level_greater_than_threshold_should_ignore_message
+ @logger.level = 3
+ @logger.log(4, "message")
+ assert @io.string.empty?
+ end
+
+ def test_log_with_level_equal_to_threshold_should_log_message
+ @logger.level = 3
+ @logger.log(3, "message")
+ assert @io.string.include?("message")
+ end
+
+ def test_log_with_level_less_than_threshold_should_log_message
+ @logger.level = 3
+ @logger.log(2, "message")
+ assert @io.string.include?("message")
+ end
+
+ def test_log_with_multiline_message_should_log_each_line_separately
+ @logger.log(0, "first line\nsecond line")
+ assert @io.string.include?("*** first line")
+ assert @io.string.include?("*** second line")
+ end
+
+ def test_log_with_line_prefix_should_insert_line_prefix_before_message
+ @logger.log(0, "message", "prefix")
+ assert @io.string.include?("*** [prefix] message")
+ end
+
+ def test_log_with_level_0_should_have_strong_indent
+ @logger.log(0, "message")
+ assert @io.string.match(/^\*\*\* message/)
+ end
+
+ def test_log_with_level_1_should_have_weaker_indent
+ @logger.level = 1
+ @logger.log(1, "message")
+ assert @io.string.match(/^ \*\* message/)
+ end
+
+ def test_log_with_level_2_should_have_weaker_indent
+ @logger.level = 2
+ @logger.log(2, "message")
+ assert @io.string.match(/^ \* message/)
+ end
+
+ def test_log_with_level_3_should_have_weakest_indent
+ @logger.level = 3
+ @logger.log(3, "message")
+ assert @io.string.match(/^ message/)
+ end
+
+ def test_important_should_delegate_to_log_with_level_IMPORTANT
+ @logger.expects(:log).with(Capistrano::Logger::IMPORTANT, "message", "prefix")
+ @logger.important("message", "prefix")
+ end
+
+ def test_info_should_delegate_to_log_with_level_INFO
+ @logger.expects(:log).with(Capistrano::Logger::INFO, "message", "prefix")
+ @logger.info("message", "prefix")
+ end
+
+ def test_debug_should_delegate_to_log_with_level_DEBUG
+ @logger.expects(:log).with(Capistrano::Logger::DEBUG, "message", "prefix")
+ @logger.debug("message", "prefix")
+ end
+
+ def test_trace_should_delegate_to_log_with_level_TRACE
+ @logger.expects(:log).with(Capistrano::Logger::TRACE, "message", "prefix")
+ @logger.trace("message", "prefix")
+ end
+
+ def test_ordering_of_levels
+ assert Capistrano::Logger::IMPORTANT < Capistrano::Logger::INFO
+ assert Capistrano::Logger::INFO < Capistrano::Logger::DEBUG
+ assert Capistrano::Logger::DEBUG < Capistrano::Logger::TRACE
+ end
+end
View
24 test/version_test.rb
@@ -0,0 +1,24 @@
+require "#{File.dirname(__FILE__)}/utils"
+require 'capistrano/version'
+
+class VersionTest < Test::Unit::TestCase
+ def test_check_should_return_true_for_matching_parameters
+ assert Capistrano::Version.check([2], [2])
+ assert Capistrano::Version.check([2,1], [2,1])
+ assert Capistrano::Version.check([2,1,5], [2,1,5])
+ end
+
+ def test_check_should_return_true_if_first_is_less_than_second
+ assert Capistrano::Version.check([2], [3])
+ assert Capistrano::Version.check([2], [2,1])
+ assert Capistrano::Version.check([2,1], [2,2])
+ assert Capistrano::Version.check([2,1], [2,1,1])
+ end
+
+ def test_check_should_return_false_if_first_is_greater_than_second
+ assert !Capistrano::Version.check([3], [2])
+ assert !Capistrano::Version.check([3,1], [3])
+ assert !Capistrano::Version.check([3,2], [3,1])
+ assert !Capistrano::Version.check([3,2,1], [3,2])
+ end
+end

0 comments on commit f74ab73

Please sign in to comment.