diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5b2acc..51728e43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ appear at the top. @beatrichartz * `SSHKit::Backend::Printer#test` now always returns true [PR #312](https://github.com/capistrano/sshkit/pull/312) @mikz + * when using `SSHKit::CommandMap#prefix`, resolve the command through `SSHKit::CommandMap#[]` + [PR #311]((https://github.com/capistrano/sshkit/pull/311) + @mikz ### New features @@ -32,6 +35,9 @@ appear at the top. and have a cleaner internal API. You can still completely disable the pool by setting `SSHKit::Backend::Netssh.pool.idle_timeout = 0`. @mattbrictson @byroot [PR #328](https://github.com/capistrano/sshkit/pull/328) + * Allow command map entries (`SSHKit::CommandMap#[]`) to be Procs + [PR #310]((https://github.com/capistrano/sshkit/pull/310) + @mikz ### Bug fixes diff --git a/lib/sshkit/command_map.rb b/lib/sshkit/command_map.rb index fde164b4..608b1be5 100644 --- a/lib/sshkit/command_map.rb +++ b/lib/sshkit/command_map.rb @@ -31,19 +31,17 @@ def [](command) end end + TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj } + def initialize(value = nil) @map = CommandHash.new(value || defaults) end def [](command) - if prefix[command].any? - prefixes = prefix[command].map{ |prefix| prefix.respond_to?(:call) ? prefix.call : prefix } - prefixes = prefixes.join(" ") + prefixes = prefix[command].map(&TO_VALUE) + cmd = TO_VALUE.(@map[command]) - "#{prefixes} #{command}" - else - @map[command] - end + [*prefixes, cmd].compact.join(' ') end def prefix diff --git a/test/unit/test_command_map.rb b/test/unit/test_command_map.rb index 65e85f93..99948ebb 100644 --- a/test/unit/test_command_map.rb +++ b/test/unit/test_command_map.rb @@ -16,12 +16,21 @@ def test_setter assert_equal map[:rake], "/usr/local/rbenv/shims/rake" end + def test_setter_procs + map = CommandMap.new + i = 0 + map[:rake] = -> { i += 1; "/usr/local/rbenv/shims/rake#{i}" } + + assert_equal map[:rake], "/usr/local/rbenv/shims/rake1" + assert_equal map[:rake], "/usr/local/rbenv/shims/rake2" + end + def test_prefix map = CommandMap.new map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec") map.prefix[:rake].push("bundle exec") - assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_procs @@ -29,7 +38,7 @@ def test_prefix_procs map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec") map.prefix[:rake].push(proc{ "bundle exec" }) - assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_unshift @@ -37,7 +46,7 @@ def test_prefix_unshift map.prefix[:rake].push("bundle exec") map.prefix[:rake].unshift("/home/vagrant/.rbenv/bin/rbenv exec") - assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_indifferent_setter @@ -53,7 +62,7 @@ def test_indifferent_prefix map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec") map.prefix["rake"].push("bundle exec") - assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_initialization_is_thread_safe @@ -65,5 +74,13 @@ def test_prefix_initialization_is_thread_safe end threads.each(&:join) end + + def test_prefix_setter + map = CommandMap.new({}) + map[:rake] = 'rake2.2' + map.prefix[:rake].push('bundle exec') + + assert_equal map[:rake], 'bundle exec rake2.2' + end end end