Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve command map combined with prefix #311

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 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 All @@ -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

Expand Down
12 changes: 5 additions & 7 deletions lib/sshkit/command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions test/unit/test_command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,37 @@ 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
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 @@ -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
Expand All @@ -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