Module: Roby::Test::ArubaMinitest

Defined in:
lib/roby/test/aruba_minitest.rb

Overview

Minitest-usable Aruba wrapper

Aruba 0.14 is incompatible with Minitest because of their definition of the #run method This change hacks around the problem, by moving the Aruba API to a side stub object.

The run methods are renamed as they have been renamed in Aruba 1.0 alpha, run -> run_command and run_simple -> run_command_and_stop

Defined Under Namespace

Classes: API

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/roby/test/aruba_minitest.rb', line 156

def method_missing(name, *args, &block)
    if @aruba_api.respond_to?(name)
        return @aruba_api.send(name, *args, &block)
    end

    super
end

Instance Attribute Details

#roby_binObject (readonly)

Returns the value of attribute roby_bin.



26
27
28
# File 'lib/roby/test/aruba_minitest.rb', line 26

def roby_bin
  @roby_bin
end

Instance Method Details

#assert_command_finished_successfully(cmd) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/roby/test/aruba_minitest.rb', line 169

def assert_command_finished_successfully(cmd)
    refute cmd.timed_out?,
           "#{cmd} timed out on stop\n-- STDOUT\n#{cmd.stdout}\n"\
           "STDERR\n#{cmd.stderr}"
    assert_equal 0, cmd.exit_status,
                 "#{cmd} finished with a non-zero exit status "\
                 "(#{cmd.exit_status})\n-- STDOUT\n#{cmd.stdout}\n"\
                 "-- STDERR\n#{cmd.stderr}"
end

#assert_command_stops(cmd, fail_on_error: true) ⇒ Object



164
165
166
167
# File 'lib/roby/test/aruba_minitest.rb', line 164

def assert_command_stops(cmd, fail_on_error: true)
    cmd.stop
    assert_command_finished_successfully(cmd) if fail_on_error
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/roby/test/aruba_minitest.rb', line 152

def respond_to_missing?(name, include_private = false)
    @aruba_api.respond_to?(name) || super
end

#roby_allocate_interface_serverInteger

Create a TCPServer to be used as the underlying's roby run server

Returns:

  • (Integer)

    the file descriptor number to be passed to --interface-fd



48
49
50
51
52
53
# File 'lib/roby/test/aruba_minitest.rb', line 48

def roby_allocate_interface_server
    raise "interface server already allocated" if @interface_server

    @interface_server = ::TCPServer.new(0)
    @interface_server.fileno
end

#roby_allocate_portObject



61
62
63
64
65
66
# File 'lib/roby/test/aruba_minitest.rb', line 61

def roby_allocate_port
    server = ::TCPServer.new(0)
    server.local_address.ip_port
ensure
    server&.close
end

#roby_client_args(interface_version) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Command line arguments to connect to a Roby instance



128
129
130
131
# File 'lib/roby/test/aruba_minitest.rb', line 128

def roby_client_args(interface_version)
    "--host=localhost:#{roby_interface_port} " \
        "--interface-version=#{interface_version}"
end

#roby_interface_portObject

Port of the interface server allocated with #roby_allocate_interface_server



57
58
59
# File 'lib/roby/test/aruba_minitest.rb', line 57

def roby_interface_port
    @interface_server.local_address.ip_port
end

#run_command(cmd, exit_timeout: 45, **opts) ⇒ Object



76
77
78
79
# File 'lib/roby/test/aruba_minitest.rb', line 76

def run_command(cmd, exit_timeout: 45, **opts)
    opts[:exit_timeout] ||= exit_timeout
    @aruba_api.run_command(cmd, opts)
end

#run_command_and_stop(cmd, fail_on_error: true, exit_timeout: 45, **opts) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/roby/test/aruba_minitest.rb', line 68

def run_command_and_stop(cmd, fail_on_error: true, exit_timeout: 45, **opts)
    opts[:exit_timeout] ||= exit_timeout
    cmd = run_command(cmd, **opts)
    cmd.stop
    assert_command_finished_successfully(cmd) if fail_on_error
    cmd
end

#run_roby(cmd, fail_on_error: true, exit_timeout: 45, **opts) ⇒ Aruba::Command

Run a subcommand of the roby CLI

Parameters:

  • cmd (String)

    the command (e.g. quit --retry)

Returns:

  • (Aruba::Command)

    the aruba command object



97
98
99
100
101
# File 'lib/roby/test/aruba_minitest.rb', line 97

def run_roby(cmd, fail_on_error: true, exit_timeout: 45, **opts)
    opts[:exit_timeout] ||= exit_timeout
    run_command("#{Gem.ruby} #{roby_bin} #{cmd}",
                fail_on_error: fail_on_error, **opts)
end

#run_roby_and_stop(cmd, *args, fail_on_error: true, exit_timeout: 45, **opts) ⇒ Aruba::Command

Run a subcommand of the roby CLI and wait for it to stop

Parameters:

  • cmd (String)

    the command (e.g. quit --retry)

Returns:

  • (Aruba::Command)

    the aruba command object



85
86
87
88
89
90
91
# File 'lib/roby/test/aruba_minitest.rb', line 85

def run_roby_and_stop(
    cmd, *args, fail_on_error: true, exit_timeout: 45, **opts
)
    opts[:exit_timeout] ||= exit_timeout
    run_command_and_stop("#{Gem.ruby} #{roby_bin} #{cmd}", *args,
                         fail_on_error: fail_on_error, **opts)
end

#run_roby_client(cmd, *args, interface_version: 1, **opts) ⇒ Object

Run a Roby CLI command that requires to connect to a Roby instance

The command expects the interface itself to have been generated using #roby_allocate_interface_server



137
138
139
# File 'lib/roby/test/aruba_minitest.rb', line 137

def run_roby_client(cmd, *args, interface_version: 1, **opts)
    run_roby("#{cmd} #{roby_client_args(interface_version)}", *args, **opts)
end

#run_roby_client_and_stop(cmd, *args, interface_version: 1, **opts) ⇒ Object

Run a Roby CLI command that requires to connect to a Roby instance, and wait for it to stop

The command expects the interface itself to have been generated using #roby_allocate_interface_server



146
147
148
149
150
# File 'lib/roby/test/aruba_minitest.rb', line 146

def run_roby_client_and_stop(cmd, *args, interface_version: 1, **opts)
    run_roby_and_stop(
        "#{cmd} #{roby_client_args(interface_version)}", *args, **opts
    )
end

#run_roby_environmentObject



41
42
43
# File 'lib/roby/test/aruba_minitest.rb', line 41

def run_roby_environment
    { "ROBY_PLUGIN_PATH" => @roby_plugin_path.join(":") }
end

#run_roby_run(cmd = "", interface_version: 1, forwarded_ios: [], **opts) ⇒ Object

Run roby run

Unlike calling run_roby directly, it sets up the --interface-fd argument properly if #roby_allocate_interface_server has been called



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roby/test/aruba_minitest.rb', line 107

def run_roby_run(cmd = "", interface_version: 1, forwarded_ios: [], **opts)
    if @interface_server
        arg =
            if interface_version == 1
                "--interface-fd"
            else
                "--interface-v2-fd"
            end

        cmd = "--interface-versions=#{interface_version} " \
              "#{arg}=#{@interface_server.fileno} #{cmd}"

        forwarded_ios += [@interface_server.fileno]
    end

    run_roby("run #{cmd}", forwarded_ios: forwarded_ios, **opts)
end

#setupObject



28
29
30
31
32
33
# File 'lib/roby/test/aruba_minitest.rb', line 28

def setup
    super
    @aruba_api = API.new
    @aruba_api.setup_aruba
    @roby_bin = File.join(Roby::BIN_DIR, "roby")
end

#teardownObject



35
36
37
38
39
# File 'lib/roby/test/aruba_minitest.rb', line 35

def teardown
    @interface_server&.close
    stop_all_commands
    super
end

#wait_for_output(cmd, channel, timeout: 5) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/roby/test/aruba_minitest.rb', line 179

def wait_for_output(cmd, channel, timeout: 5)
    deadline = Time.now + timeout
    while Time.now < deadline
        if yield(cmd.send(channel))
            assert(true) # To account for the assertion
            return
        end
    end

    flunk("timed out waiting for #{channel} to match in #{cmd}")
end