Skip to content

Commit

Permalink
command map prefix should still use map command
Browse files Browse the repository at this point in the history
it is valid use case to set both command map and prefix
in case of using different binary, the map should still be active
and prefix should be applied as extra

so following example correclty executes rake2.2 within bundler:

  SSHKit.config.command_map[:rake] = 'rake2.2'
  SSHKit.config.command_map.prefix[:rake] = 'bundle exec'

Only drawback is, that resulting command will be:

  bundle exec /usr/bin/env rake2.2
  • Loading branch information
Michal Cichra authored and mikz committed Feb 24, 2016
1 parent b8817e6 commit a13b366
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 3 additions & 7 deletions lib/sshkit/command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ def initialize(value = nil)
end

def [](command)
if prefix[command].any?
prefixes = prefix[command].map(&TO_VALUE)
prefixes = prefixes.join(" ")
prefixes = prefix[command].map(&TO_VALUE)
cmd = TO_VALUE.(@map[command])

"#{prefixes} #{command}"
else
TO_VALUE.(@map[command])
end
[*prefixes, cmd].compact.join(' ')
end

def prefix
Expand Down
16 changes: 12 additions & 4 deletions test/unit/test_command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ def test_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_procs
map = CommandMap.new
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
map = CommandMap.new
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
Expand All @@ -62,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
Expand All @@ -74,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

0 comments on commit a13b366

Please sign in to comment.