diff --git a/lib/capistrano/recipes/deploy/local_dependency.rb b/lib/capistrano/recipes/deploy/local_dependency.rb index 505d89e8..c4fb8c36 100644 --- a/lib/capistrano/recipes/deploy/local_dependency.rb +++ b/lib/capistrano/recipes/deploy/local_dependency.rb @@ -30,7 +30,7 @@ module Capistrano # file is found that matches the parameter, this returns true. def find_in_path(utility) path = (ENV['PATH'] || "").split(File::PATH_SEPARATOR) - suffixes = RUBY_PLATFORM =~ /mswin/ ? %w(.bat .exe .com .cmd) : [""] + suffixes = self.class.on_windows? ? %w(.bat .exe .com .cmd) : [""] path.each do |dir| suffixes.each do |sfx| @@ -41,6 +41,10 @@ module Capistrano false end + + def self.on_windows? + RUBY_PLATFORM =~ /mswin/ + end end end -end \ No newline at end of file +end diff --git a/test/deploy/local_dependency_test.rb b/test/deploy/local_dependency_test.rb new file mode 100644 index 00000000..976c7a9a --- /dev/null +++ b/test/deploy/local_dependency_test.rb @@ -0,0 +1,73 @@ +require "utils" +require 'capistrano/recipes/deploy/local_dependency' + +class LocalDependencyTest < Test::Unit::TestCase + def setup + @config = { } + @dependency = Capistrano::Deploy::LocalDependency.new(@config) + end + + def test_should_use_standard_error_message + setup_for_one_path_entry(false) + @dependency.command("cat") + assert_equal "`cat' could not be found in the path on the local host", @dependency.message + end + + def test_should_use_alternative_message_if_provided + setup_for_one_path_entry(false) + @dependency.command("cat").or("Sorry") + assert_equal "Sorry", @dependency.message + end + + def test_env_with_no_path_should_never_find_command + ENV.expects(:[]).with("PATH").returns(nil) + assert !@dependency.command("cat").pass? + end + + def test_env_with_one_path_entry_should_fail_if_command_not_found + setup_for_one_path_entry(false) + assert !@dependency.command("cat").pass? + end + + def test_env_with_one_path_entry_should_pass_if_command_found + setup_for_one_path_entry(true) + assert @dependency.command("cat").pass? + end + + def test_env_with_three_path_entries_should_fail_if_command_not_found + setup_for_three_path_entries(false) + assert !@dependency.command("cat").pass? + end + + def test_env_with_three_path_entries_should_pass_if_command_found + setup_for_three_path_entries(true) + assert @dependency.command("cat").pass? + end + + def test_env_with_one_path_entry_on_windows_should_pass_if_command_found_with_extension + setup_for_one_path_entry_on_windows(true) + assert @dependency.command("cat").pass? + end + + private + + def setup_for_one_path_entry(command_found) + ENV.expects(:[]).with("PATH").returns("/bin") + File.expects(:executable?).with("/bin/cat").returns(command_found) + end + + def setup_for_three_path_entries(command_found) + path = %w(/bin /usr/bin /usr/local/bin).join(File::PATH_SEPARATOR) + ENV.expects(:[]).with("PATH").returns(path) + File.expects(:executable?).with("/usr/bin/cat").returns(command_found) + File.expects(:executable?).at_most(1).with("/bin/cat").returns(false) + File.expects(:executable?).at_most(1).with("/usr/local/bin/cat").returns(false) + end + + def setup_for_one_path_entry_on_windows(command_found) + Capistrano::Deploy::LocalDependency.expects(:on_windows?).returns(true) + ENV.expects(:[]).with("PATH").returns("/cygwin/bin") + File.stubs(:executable?).returns(false) + File.expects(:executable?).with("/cygwin/bin/cat.exe").returns(command_found) + end +end