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

Nu-hin / remote_ruby / 13674380715

05 Mar 2025 10:57AM UTC coverage: 94.18% (-0.1%) from 94.295%
13674380715

push

github

Nu-hin
Ensuring CompatIO threads join

15 of 18 new or added lines in 4 files covered. (83.33%)

1 existing line in 1 file now uncovered.

890 of 945 relevant lines covered (94.18%)

91.59 hits per line

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

21.43
/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✔
24
      ret = nil
×
25
      Net::SSH.start(host, nil, config) do |ssh|
×
26
        with_temp_file(code, ssh) do |fname|
×
27
          res = run_code(ssh, fname, stdin, stdout, stderr)
×
28
          ret = get_result(ssh, fname, res)
×
29
        end
30
      end
31
      ret
×
32
    end
33

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

NEW
41
          ssh.listen_to(stdin_r.readable) do |io|
×
42
            data = io.read_nonblock(4096)
×
43
            ch.send_data(data)
×
44
          rescue EOFError
NEW
45
            stdin_r.join
×
UNCOV
46
            ch.eof!
×
47
          end
48

49
          ch.on_data do |_, data|
×
50
            stdout_w.write(data)
×
51
          end
52

53
          ch.on_extended_data do |_, _, data|
×
54
            stderr_w.write(data)
×
55
          end
56

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

63
      res
×
64
    end
65

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

69
      ssh.exec!("cat \"#{fname}\"")
×
70
    end
71

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

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

82
          ch.on_data do |_, data|
×
83
            fname << data
×
84
          end
85

86
          ch.send_data(code)
×
87
          ch.eof!
×
88
        end
89
      end.wait
90

91
      fname.strip!
×
92

93
      yield fname
×
94
    ensure
95
      Net::SSH.start(host, nil, config) do |del_ssh|
×
96
        del_ssh.exec!("rm \"#{fname}\"")
×
97
      end
98
    end
99
  end
100
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