Skip to content

Commit 810651d

Browse files
authored
Command: wrap a Proc interaction_handler into a MappingInteractionHandler (#436)
According to the doc one can pass a Hash or a Proc as a :interaction_handler option, but only Hashes were wrapped into a MappingInteractionHandler. Fix this and add a test.
1 parent d69522c commit 810651d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/sshkit/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def sanitize_command(cmd)
239239

240240
def call_interaction_handler(stream_name, data, channel)
241241
interaction_handler = options[:interaction_handler]
242-
interaction_handler = MappingInteractionHandler.new(interaction_handler) if interaction_handler.kind_of?(Hash)
242+
interaction_handler = MappingInteractionHandler.new(interaction_handler) if interaction_handler.kind_of?(Hash) or interaction_handler.kind_of?(Proc)
243243
interaction_handler.on_data(self, stream_name, data, channel) if interaction_handler.respond_to?(:on_data)
244244
end
245245

test/functional/backends/test_local.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ def test_interaction_handler
9999
end.run
100100
assert_equal("Enter Data\nCaptured SOME DATA", captured_command_result)
101101
end
102+
103+
def test_interaction_handler_with_proc
104+
captured_command_result = nil
105+
Local.new do
106+
command = 'echo Enter Data; read the_data; echo Captured $the_data;'
107+
captured_command_result = capture(command, interaction_handler:
108+
lambda { |data|
109+
case data
110+
when "Enter Data\n"
111+
"SOME DATA\n"
112+
when "Captured SOME DATA\n"
113+
nil
114+
end
115+
}
116+
)
117+
end.run
118+
assert_equal("Enter Data\nCaptured SOME DATA", captured_command_result)
119+
end
102120
end
103121
end
104122
end

0 commit comments

Comments
 (0)