Class: Roby::App::Rake::TestTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/roby/app/rake.rb

Overview

Rake task to run the Roby tests

To use, add the following to your Rakefile:

require 'roby/app/rake'
Roby::App::Rake::TestTask.new

It create a test task per robot configuration, named “test:$robot_name”. It also creates a test:all-robots task that runs each robot’s configuration in sequence. You can inspect these tasks with

rake --tasks

and call them with e.g.

rake test:default

The test:all-robots task will fail only at the end of all tests, reporting which configuration actually failed. To stop at the first failure, pass a ‘0’ as argument, e.g.

rake 'test:all-robots[0]'

Finally, the ‘test:all’ target runs syskit test –all (i.e. runs all tests in the default robot configuration)

The ‘test’ target points by default to test:all-robots. See below to change this.

The following examples show how to fine-tune the creates tests:

Examples:

restrict the tests to run only under the

'only_this_configuration' configuration

   Roby::App::Rake::TestTask.new do |t|
       t.robot_names = ['only_this_configuration']
   end

create tasks under the roby_tests namespace instead of ‘test’


Roby::App::Rake::TestCase.new('roby_tests')

make ‘test’ point to ‘test:all’


Roby::App::Rake::TestCase.new do |t|
    t.all_by_default = true
end

Defined Under Namespace

Classes: Failed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name = 'test', all_by_default: false) {|_self| ... } ⇒ TestTask

Returns a new instance of TestTask.

Yields:

  • (_self)

Yield Parameters:



86
87
88
89
90
91
92
93
94
# File 'lib/roby/app/rake.rb', line 86

def initialize(task_name = 'test', all_by_default: false)
    @task_name = task_name
    @app = Roby.app
    @all_by_default = all_by_default
    @robot_names = discover_robot_names
    @excludes = []
    yield self if block_given?
    define
end

Instance Attribute Details

#appObject

The app

It defaults to Roby.app



65
66
67
# File 'lib/roby/app/rake.rb', line 65

def app
  @app
end

#excludesArray<String>

Patterns matching excluded test files

It accepts any string that File.fnmatch? accepts

Returns:

  • (Array<String>)


80
81
82
# File 'lib/roby/app/rake.rb', line 80

def excludes
  @excludes
end

#robot_namesArray<String,(String,String)>

The list of robot configurations on which we should run the tests

Use ‘default’ for the default configuration. It defaults to all the robots defined in config/robots/

Returns:

  • (Array<String,(String,String)>)


73
74
75
# File 'lib/roby/app/rake.rb', line 73

def robot_names
  @robot_names
end

#task_nameObject (readonly)

The base test task name



60
61
62
# File 'lib/roby/app/rake.rb', line 60

def task_name
  @task_name
end

Instance Method Details

#defineObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/roby/app/rake.rb', line 98

def define
    each_robot do |robot_name, robot_type|
        task_name = task_name_for_robot(robot_name, robot_type)

        desc "run the tests for configuration #{robot_name}:#{robot_type}"
        task task_name do
            if !run_roby_test('-r', "#{robot_name},#{robot_type}")
                raise Failed.new("failed to run tests for #{robot_name}:#{robot_type}")
            end
        end
    end

    desc "run tests for all known robots"
    task "#{task_name}:all-robots", [:keep_going] do |t, args|
        failures = Array.new
        keep_going = args.fetch(:keep_going, '1') == '1'
        each_robot do |robot_name, robot_type|
            if !run_roby_test('-r', "#{robot_name},#{robot_type}")
                if keep_going
                    failures << [robot_name, robot_type]
                else
                    raise Failed.new("failed to run tests for #{robot_name}:#{robot_type}")
                end
            end
        end
        if !failures.empty?
            raise Failed.new("failed to run the following test(s): #{failures.map { |name, type| "#{name}:#{type}" }.join(", ")}")
        end
    end

    desc "run all tests"
    task "#{task_name}:all" do
        if !run_roby_test
            raise Failed.new("failed to run tests")
        end
    end

    if all_by_default?
        desc "run all tests"
        task task_name => "#{task_name}:all"
    else
        desc "run all robot tests"
        task task_name => "#{task_name}:all-robots"
    end
end

#discover_robot_namesObject

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.

Discover which robots are available on the current app



179
180
181
182
183
184
185
# File 'lib/roby/app/rake.rb', line 179

def discover_robot_names
    if !app.app_dir
        app.guess_app_dir
    end
    app.setup_robot_names_from_config_dir
    app.robots.each.to_a
end

#each_robot(&block) ⇒ Object

Enumerate the robots on which tests should be run



172
173
174
# File 'lib/roby/app/rake.rb', line 172

def each_robot(&block)
    robot_names.each(&block)
end

#run_roby(*args) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/roby/app/rake.rb', line 159

def run_roby(*args)
    pid = spawn(Gem.ruby, File.expand_path(File.join('..', '..', '..', 'bin', 'roby'), __dir__),
           *args)
    begin
        _, status = Process.waitpid2(pid)
        status.success?
    rescue Interrupt
        Process.kill pid
        Process.waitpid(pid)
    end
end

#run_roby_test(*args) ⇒ Object



152
153
154
155
156
157
# File 'lib/roby/app/rake.rb', line 152

def run_roby_test(*args)
    args = args + excludes.flat_map do |pattern|
        ["--exclude", pattern]
    end
    run_roby('test', *args)
end

#task_name_for_robot(robot_name, robot_type) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/roby/app/rake.rb', line 144

def task_name_for_robot(robot_name, robot_type)
    if robot_name == robot_type
        "#{task_name}:#{robot_name}"
    else
        "#{task_name}:#{robot_name}-#{robot_type}" 
    end
end