Skip to content

Commit

Permalink
Remove legacy adapters. Add more integration specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nu-hin committed Dec 20, 2024
1 parent 0f95651 commit 4288016
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 342 deletions.
8 changes: 2 additions & 6 deletions lib/remote_ruby/connection_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module RemoteRuby
# Base class for other connection adapters.
# Base class for connection adapters.
class ConnectionAdapter
# Initializers of adapters should receive only keyword arguments.
# May be overriden in a child class.
Expand All @@ -17,11 +17,7 @@ def open(_code)
end

require 'remote_ruby/connection_adapter/eval_adapter'
require 'remote_ruby/connection_adapter/stdin_process_adapter'
require 'remote_ruby/connection_adapter/ssh_stdin_adapter'
require 'remote_ruby/connection_adapter/local_stdin_adapter'
require 'remote_ruby/connection_adapter/cache_adapter'
require 'remote_ruby/connection_adapter/caching_adapter'
require 'remote_ruby/connection_adapter/tmp_file_adapter'
require 'remote_ruby/connection_adapter/ssh_tmp_file_adapter'
require 'remote_ruby/connection_adapter/ssh_adapter'
require 'remote_ruby/connection_adapter/tmp_file_adapter'
25 changes: 0 additions & 25 deletions lib/remote_ruby/connection_adapter/local_stdin_adapter.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/remote_ruby/connection_adapter/ssh_stdin_adapter.rb

This file was deleted.

60 changes: 0 additions & 60 deletions lib/remote_ruby/connection_adapter/ssh_tmp_file_adapter.rb

This file was deleted.

35 changes: 0 additions & 35 deletions lib/remote_ruby/connection_adapter/stdin_process_adapter.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/remote_ruby/execution_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(**params)
@use_cache = params.delete(:use_cache) || false
@save_cache = params.delete(:save_cache) || false
@cache_dir = params.delete(:cache_dir) || File.join(Dir.pwd, 'cache')
@in_stream = params.delete(:in_stream) || $stdin
@out_stream = params.delete(:out_stream) || $stdout
@err_stream = params.delete(:err_stream) || $stderr
@adapter_klass = params.delete(:adapter) || ::RemoteRuby::SSHAdapter
Expand All @@ -45,7 +46,7 @@ def execute(locals = nil, &block)
private

attr_reader :params, :adapter_klass, :use_cache, :save_cache, :cache_dir,
:out_prefix, :out_stream, :err_stream, :flavours, :cache_prefix
:out_prefix, :in_stream, :out_stream, :err_stream, :flavours, :cache_prefix

def assign_locals(local_names, values, block)
local_names.each do |local|
Expand Down Expand Up @@ -102,6 +103,7 @@ def execute_code(ruby_code, client_locals = {})
code: compiler.compiled_code,
adapter: adapter(compiler.code_hash),
prefix: "#{cp}#{out_prefix}",
in_stream: in_stream,
out_stream: out_stream,
err_stream: err_stream
)
Expand Down
141 changes: 141 additions & 0 deletions spec/integration/ssh_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true

describe 'Connecting to remote host with SSH adapter',
type: :integration do
let(:ec) do
RemoteRuby::ExecutionContext.new(
adapter: RemoteRuby::SSHAdapter,
host: ssh_host,
user: ssh_user,
working_dir: ssh_workdir
)
end

context 'with do-blocks' do
it 'reads string from stdin' do
with_stdin_redirect("John doe\n") do
expect do
ec.execute do
puts gets
end
end.to output("John doe\n").to_stdout
end
end

it 'receives integer result' do
result = ec.execute do
17 + 5
end

expect(result).to eq(22)
end

it 'passers integer locals' do
x = 17
y = 5
result = ec.execute do
x + y
end

expect(result).to eq(22)
end

it 'receives string result' do
result = ec.execute do
'a' * 3
end

expect(result).to eq('aaa')
end

it 'prints to stdout' do
s = 'Something'

expect do
ec.execute do
puts s
end
end.to output("#{s}\n").to_stdout
end

it 'prints to stderr' do
s = 'Something'

expect do
ec.execute do
warn s
end
end.to output("#{s}\n").to_stderr
end

it 'receives complex local context' do
a = 3
b = 'Hello'
c = 5.0
d = Time.new(2025, 1, 1)
e = nil
f = [1, 2, 3]
g = { a: 1, b: 2 }

result = ec.execute do
{
a: a * 2,
b: "#{b} World",
c: c * 2,
d: Time.new(d.year + 1, d.month, d.day),
e: e.nil?,
f: f.map { |x| x * 2 },
g: g.transform_values { |v| v * 2 }
}
end

expect(result).to eq(
a: 6,
b: 'Hello World',
c: 10.0,
d: Time.new(2026, 1, 1),
e: true,
f: [2, 4, 6],
g: { a: 2, b: 4 }
)
end

it 'modifies local variables' do
a = 3
b = 'Hello'
c = 5.0
d = Time.new(2025, 1, 1)
e = nil
f = [1, 2, 3]
g = { a: 1, b: 2 }

ec.execute do
a *= 2
b = "#{b} World"
c *= 2
d = Time.new(d.year + 1, d.month, d.day)
e = e.nil?
f = f.map { |x| x * 2 }
g = g.transform_values { |v| v * 2 }
end

expect(a).to eq(6)
expect(b).to eq('Hello World')
expect(c).to eq(10.0)
expect(d).to eq(Time.new(2026, 1, 1))
expect(e).to eq(true)
expect(f).to eq([2, 4, 6])
expect(g).to eq(a: 2, b: 4)
end
end

context 'with {}-blocks' do
it 'prints to stdout' do
s = 'Something'

expect do
ec.execute { puts s }
end.to output(Regexp.new(s)).to_stdout
end
end
end
35 changes: 0 additions & 35 deletions spec/integration/ssh_stdin_spec.rb

This file was deleted.

Loading

0 comments on commit 4288016

Please sign in to comment.