Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/sshkit/backends/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def with(environment, &_block)
end

def as(who, &_block)
who_old = [@user, @group]
if who.is_a? Hash
@user = who[:user] || who["user"]
@group = who[:group] || who["group"]
Expand All @@ -118,8 +119,7 @@ def as(who, &_block)
EOTEST
yield
ensure
remove_instance_variable(:@user)
remove_instance_variable(:@group)
@user, @group = *who_old
end

class << self
Expand Down
55 changes: 55 additions & 0 deletions test/unit/backends/test_abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,61 @@ def test_within_home
assert_equal 'cd ~/foo && /usr/bin/env cat file', backend.executed_command.to_command
end

def test_as_properly_clears
backend = ExampleBackend.new do
as :root do
execute :cat, 'file', :strip => false
end

execute :cat, 'file', :strip => false
end

backend.run

assert_equal '/usr/bin/env cat file', backend.executed_command.to_command
end

def test_as_root
backend = ExampleBackend.new do
as :root do
execute :cat, 'file', :strip => false
end
end

backend.run

assert_equal 'sudo -u root -- sh -c /usr/bin/env\\ cat\\ file', backend.executed_command.to_command
end

def test_nested_as
backend = ExampleBackend.new do
as :root do
as :other_user do
execute :cat, 'file', :strip => false
end
end
end

backend.run

assert_equal 'sudo -u other_user -- sh -c /usr/bin/env\\ cat\\ file', backend.executed_command.to_command
end

def test_nested_as_properly_clears
backend = ExampleBackend.new do
as :root do
as :other_user do
execute :cat, 'file', :strip => false
end
execute :cat, 'file', :strip => false
end
end

backend.run

assert_equal 'sudo -u root -- sh -c /usr/bin/env\\ cat\\ file', backend.executed_command.to_command
end

def test_background_logs_deprecation_warnings
deprecation_out = +''
SSHKit.config.deprecation_output = deprecation_out
Expand Down