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

Nu-hin / remote_ruby / 13529570178

25 Feb 2025 07:40PM UTC coverage: 90.599% (-2.3%) from 92.926%
13529570178

push

github

Nu-hin
Remove stream splitter. Store the result in the code file

20 of 32 new or added lines in 6 files covered. (62.5%)

1 existing line in 1 file now uncovered.

877 of 968 relevant lines covered (90.6%)

60.69 hits per line

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

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

3
require 'net/ssh'
16✔
4
require 'remote_ruby/pipes'
16✔
5

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

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

13
    def initialize(host:, user: nil, working_dir: nil)
16✔
14
      super
×
15
      @host = host
×
16
      @working_dir = working_dir
×
17
      @config = Net::SSH.configuration_for(@host, true)
×
18
      @user = user || @config[:user]
×
19
    end
20

21
    def open(code)
16✔
22
      Net::SSH.start(host, user, config) do |ssh|
×
23
        with_temp_file(code, ssh) do |fname|
×
24
          Pipes.with_pipes do |p|
×
25
            t = Thread.new do
×
26
              run_code(ssh, fname, p.in_r, p.out_w, p.err_w)
×
NEW
27
              get_result(ssh, fname, p.res_w)
×
28
            end
29

NEW
30
            yield p.in_w, p.out_r, p.err_r, p.res_r
×
UNCOV
31
            t.join
×
32
          end
33
        end
34
      end
35
    end
36

37
    def run_code(ssh, fname, stdin_r, stdout_w, stderr_w)
16✔
38
      ssh.open_channel do |channel|
×
39
        channel.exec("cd '#{working_dir}' && ruby #{fname}") do |ch, success|
×
40
          raise UnableToExecuteError unless success
×
41

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

49
          ch.on_data do |_, data|
×
50
            stdout_w << data
×
51
          end
52

53
          ch.on_extended_data do |_, _, data|
×
54
            stderr_w << data
×
55
          end
56

57
          ch.on_close do |_|
×
58
            stdout_w.close
×
59
            stderr_w.close
×
60
          end
61
        end
62
      end.wait
63
    end
64

65
    def get_result(ssh, fname, res_w)
16✔
NEW
66
      ssh.open_channel do |channel|
×
NEW
67
        channel.exec("cat #{fname}") do |ch, success|
×
NEW
68
          raise UnableToExecuteError unless success
×
69

NEW
70
          ch.on_data do |_, data|
×
NEW
71
            res_w << data
×
72
          end
73

NEW
74
          ch.on_close do |_|
×
NEW
75
            res_w.close
×
76
          end
77
        end
78
      end.wait
79
    end
80

81
    def connection_name
16✔
82
      "#{user}@#{host}:#{working_dir || '~'}> "
×
83
    end
84

85
    def with_temp_file(code, ssh)
16✔
86
      fname = String.new
×
87
      code_channel = ssh.open_channel do |channel|
×
88
        channel.exec('f=$(mktemp remote_ruby.rb.XXXXXX) && cat > $f && echo $f') do |ch, success|
×
89
          raise UnableToExecuteError unless success
×
90

91
          ch.on_data do |_, data|
×
92
            fname << data
×
93
          end
94

95
          ch.send_data(code)
×
96
          ch.eof!
×
97
        end
98
      end
99

100
      code_channel.wait
×
101

102
      yield fname
×
103
    ensure
104
      ssh.exec!("rm #{fname}")
×
105
    end
106
  end
107
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