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

Nu-hin / remote_ruby / 16672100282

01 Aug 2025 09:57AM UTC coverage: 95.234%. Remained the same
16672100282

push

github

Nu-hin
Add path_to_ruby config option

2 of 4 new or added lines in 1 file covered. (50.0%)

24 existing lines in 1 file now uncovered.

1119 of 1175 relevant lines covered (95.23%)

141.38 hits per line

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

24.32
/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, :ruby_executable
20✔
12

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

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

24
    def open(code, stdin, stdout, stderr)
20✔
UNCOV
25
      ret = nil
×
26
      Net::SSH.start(host, nil, config) do |ssh|
×
27
        with_temp_file(code, ssh) do |fname|
×
28
          res = run_code(ssh, fname, stdin, stdout, stderr)
×
29
          raise "Process exited with code #{res}" unless res.zero?
×
30

UNCOV
31
          ret = get_result(ssh, fname)
×
32
        end
33
      end
UNCOV
34
      ret
×
35
    end
36

37
    def connection_name
20✔
UNCOV
38
      "#{user}@#{host}:#{working_dir || '~'}> "
×
39
    end
40

41
    private
20✔
42

43
    def handle_stdin(chan, stdin)
20✔
UNCOV
44
      return if stdin.nil?
×
45

UNCOV
46
      if stdin.is_a?(StringIO)
×
47
        chan.send_data(stdin.string)
×
48
        chan.eof!
×
49
        return
×
50
      end
51

UNCOV
52
      stdin = RemoteRuby::CompatIOReader.new(stdin)
×
53

UNCOV
54
      chan.connection.listen_to(stdin.readable) do |io|
×
55
        data = io.read_nonblock(4096)
×
56
        chan.send_data(data)
×
57
      rescue EOFError
UNCOV
58
        chan.connection.stop_listening_to(stdin.readable)
×
59
        chan.eof!
×
60
      end
61

UNCOV
62
      chan.on_close do
×
63
        chan.connection.stop_listening_to(stdin.readable)
×
64
        stdin.join
×
65
      end
66
    end
67

68
    def handle_stdout(chan, stdout)
20✔
UNCOV
69
      return if stdout.nil?
×
70

UNCOV
71
      chan.on_data do |_, data|
×
72
        stdout.write(data)
×
73
      end
74
    end
75

76
    def handle_stderr(chan, stderr)
20✔
UNCOV
77
      return if stderr.nil?
×
78

UNCOV
79
      chan.on_extended_data do |_, _, data|
×
80
        stderr.write(data)
×
81
      end
82
    end
83

84
    def handle_exit_code(chan)
20✔
UNCOV
85
      chan.on_request('exit-status') do |_, data|
×
86
        yield data.read_long
×
87
      end
88
    end
89

90
    def run_remote_process(ssh, cmd, stdin, stdout, stderr)
20✔
UNCOV
91
      res = nil
×
92

UNCOV
93
      ssh.open_channel do |channel|
×
94
        channel.exec(cmd) do |ch, success|
×
95
          raise UnableToExecuteError unless success
×
96

UNCOV
97
          handle_stdin(ch, stdin)
×
98
          handle_stdout(ch, stdout)
×
99
          handle_stderr(ch, stderr)
×
100
          handle_exit_code(ch) do |code|
×
101
            res = code
×
102
          end
103
        end
104
      end.wait
105

UNCOV
106
      res
×
107
    end
108

109
    def run_code(ssh, fname, stdin, stdout, stderr)
20✔
NEW
110
      cmd = "cd '#{working_dir}' && #{ruby_executable} \"#{fname}\""
×
111
      run_remote_process(ssh, cmd, stdin, stdout, stderr)
×
112
    end
113

114
    def get_result(ssh, fname)
20✔
UNCOV
115
      ssh.exec!("cat \"#{fname}\"")
×
116
    end
117

118
    def with_temp_file(code, ssh)
20✔
UNCOV
119
      out = StringIO.new
×
120
      cmd = 'f=$(mktemp --tmpdir remote_ruby.XXXXXX) && cat > $f && echo $f'
×
121
      run_remote_process(ssh, cmd, StringIO.new(code), out, nil)
×
122
      fname = out.string.strip
×
123

UNCOV
124
      yield fname
×
125
    ensure
UNCOV
126
      ssh.exec!("rm \"#{fname}\"")
×
127
    end
128
  end
129
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