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 committed Dec 30, 2015
1 parent 9ed24c7 commit 87d8186
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ appear at the top.
* allow command map entries (`SSHKit::CommandMap#[]`) to be Procs
[PR #310]((https://github.com/capistrano/sshkit/pull/310)
@mikz
* when using `SSHKit::CommandMap#prefix`, resolve the command through `SSHKit::CommandMap#[]`
[PR #311]((https://github.com/capistrano/sshkit/pull/311)
@mikz

## 1.8.1

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 @@ -40,14 +40,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
15 changes: 11 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,8 +62,15 @@ 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_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 87d8186

Please sign in to comment.