• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Nu-hin / remote_ruby / 13660785819

04 Mar 2025 06:49PM UTC coverage: 93.79% (+1.1%) from 92.717%
13660785819

push

github

Nu-hin
Rework adapters to receive writeable streams instead of yielding
readable

151 of 161 new or added lines in 18 files covered. (93.79%)

1 existing line in 1 file now uncovered.

876 of 934 relevant lines covered (93.79%)

91.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.22
/lib/remote_ruby/ssh_adapter.rb
1
# frozen_string_literal: true
2

3
require 'net/ssh'
20✔
4
require 'remote_ruby/compat_io_reader'
20✔
5

6
module RemoteRuby
20✔
7
  # An adapter for executing Ruby code on a remote host via SSH
8
  class SSHAdapter < ConnectionAdapter
20✔
9
    UnableToExecuteError = Class.new(StandardError)
20✔
10

11
    attr_reader :host, :config, :working_dir, :user
20✔
12

13
    def initialize(host:, working_dir: nil, use_ssh_config_file: true, **params)
20✔
14
      super
×
15
      @host = host
×
16
      @working_dir = working_dir
×
17
      @config = Net::SSH.configuration_for(@host, use_ssh_config_file)
×
18

19
      @config = @config.merge(params)
×
20
      @user = @config[:user]
×
21
    end
22

23
    def open(code, stdin, stdout, stderr)
20✔
NEW
24
      ret = nil
×
25
      Net::SSH.start(host, nil, config) do |ssh|
×
26
        with_temp_file(code, ssh) do |fname|
×
NEW
27
          res = run_code(ssh, fname, stdin, stdout, stderr)
×
NEW
28
          ret = get_result(ssh, fname, res)
×
29
        end
30
      end
NEW
31
      ret
×
32
    end
33

34
    def run_code(ssh, fname, stdin_r, stdout_w, stderr_w)
20✔
35
      res = nil
×
36
      ssh.open_channel do |channel|
×
37
        channel.exec("cd '#{working_dir}' && ruby \"#{fname}\"") do |ch, success|
×
38
          raise UnableToExecuteError unless success
×
39

NEW
40
          ssh.listen_to(RemoteRuby::CompatIOReader.new(stdin_r).readable) do |io|
×
41
            data = io.read_nonblock(4096)
×
42
            ch.send_data(data)
×
43
          rescue EOFError
44
            ch.eof!
×
45
          end
46

47
          ch.on_data do |_, data|
×
NEW
48
            stdout_w.write(data)
×
49
          end
50

51
          ch.on_extended_data do |_, _, data|
×
NEW
52
            stderr_w.write(data)
×
53
          end
54

55
          ch.on_request('exit-status') do |_, data|
×
56
            res = data.read_long
×
57
          end
58
        end
59
      end.wait
60

UNCOV
61
      res
×
62
    end
63

64
    def get_result(ssh, fname, process_status)
20✔
NEW
65
      raise "Process exited with code #{process_status}" unless process_status.zero?
×
66

NEW
67
      ssh.exec!("cat \"#{fname}\"")
×
68
    end
69

70
    def connection_name
20✔
71
      "#{user}@#{host}:#{working_dir || '~'}> "
×
72
    end
73

74
    def with_temp_file(code, ssh)
20✔
75
      fname = String.new
×
76
      ssh.open_channel do |channel|
×
77
        channel.exec('f=$(mktemp --tmpdir remote_ruby.XXXXXX) && cat > $f && echo $f') do |ch, success|
×
78
          raise UnableToExecuteError unless success
×
79

80
          ch.on_data do |_, data|
×
81
            fname << data
×
82
          end
83

84
          ch.send_data(code)
×
85
          ch.eof!
×
86
        end
87
      end.wait
88

89
      fname.strip!
×
90

91
      yield fname
×
92
    ensure
93
      Net::SSH.start(host, nil, config) do |del_ssh|
×
94
        del_ssh.exec!("rm \"#{fname}\"")
×
95
      end
96
    end
97
  end
98
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc