Module: OroGen::Gen::RTT_CPP::SelfTest

Includes:
OroGen::Gen, OroGen::Gen::RTT_CPP
Included in:
OroGen
Defined in:
lib/orogen/gen/test.rb

Overview

This module includes common methods used by orogen's test suite. The basic idea is to have a set of methods that allow:

- each module to be built and installed in a separate directory
- that working directory to be automatically removed at teardown

Moreover, two environment variables allow to change the cleanup behaviour.

Normally, when a module build is requested, whatever files that are in the module's working directory is deleted, and that same directory is removed at teardown.

If TEST_KEEP_WC is set, the module's working directory won't be deleted at teardown.

If TEST_DONT_CLEAN is set, the module's working directory is not cleaned. It speeds up the testing process when the test files don't change but orogen does change.

Constant Summary

Constants included from OroGen::Gen::RTT_CPP

AUTOMATIC_AREA_NAME, Attribute, ConfigError, ConfigurationObject, DynamicInputPort, DynamicOutputPort, InputPort, OROGEN_LIB_DIR, Operation, OutputPort, Port, Property, TaskContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OroGen::Gen::RTT_CPP

adapt_namespace, base_dir, clean, cleanup_dir, cmake_pkgconfig_link, cmake_pkgconfig_link_corba, cmake_pkgconfig_link_noncorba, cmake_pkgconfig_require, create_or_update_symlink, default_deployment_name, define_default_deployments=, define_default_deployments_enabled?, enable, extended_states=, extended_states_enabled?, load_template, multiline_string_to_cxx, orocos_target, orocos_target=, really_clean, render_template, save_automatic, save_generated, save_public_automatic, save_user, template_path, touch

Methods included from TaskDeploymentGeneration

#generate_activity_setup, #method_missing, #rtt_priority, #rtt_scheduler, #to_deployer_xml

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OroGen::Gen::RTT_CPP::TaskDeploymentGeneration

Instance Attribute Details

#subdirObject (readonly)

Returns the value of attribute subdir


36
37
38
# File 'lib/orogen/gen/test.rb', line 36

def subdir
  @subdir
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory


30
31
32
# File 'lib/orogen/gen/test.rb', line 30

def working_directory
  @working_directory
end

Instance Method Details

#build_test_project(dirname, transports = [], test_bin = nil, wc_dirname = nil) ⇒ Object


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/orogen/gen/test.rb', line 168

def build_test_project(dirname, transports = [], test_bin = nil, wc_dirname = nil)
    source             = File.join(path_to_data, dirname)
    @working_directory = File.join(path_to_test, 'wc', wc_dirname || dirname)
    @subdir = [dirname]

    if ENV['TEST_DONT_CLEAN'] != '1' || !File.directory?(working_directory)
        # Copy +dirname+ in place of wc
        FileUtils.rm_rf working_directory
        FileUtils.mkdir_p File.dirname(working_directory)
        FileUtils.cp_r source, working_directory
    end

    project = nil
    in_wc do
        spec = Dir.glob("*.orogen").to_a.first
        project = Project.load(spec)
        project.enable_transports(*transports)

        compile_wc(project) do
            FileUtils.cp 'templates/CMakeLists.txt', 'CMakeLists.txt'
            File.open('CMakeLists.txt', 'a') do |io|
                yield(io) if block_given?
            end
        end
    end

    if test_bin
        in_prefix do
            assert(system(test_bin, redirect_to_logfile), "failed to run test program #{test_bin}, see #{logfile} for output")
        end
    end
    project
end

#build_typegen(name, header_files, transports) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/orogen/gen/test.rb', line 139

def build_typegen(name, header_files, transports)
    @working_directory = File.join(path_to_test, 'wc', name)
    header_files = [*header_files].map do |file|
        File.join(path_to_data, file)
    end

    if ENV['TEST_DONT_CLEAN'] != '1' || !File.directory?(working_directory)
        # Copy +dirname+ in place of wc
        FileUtils.rm_rf working_directory
        FileUtils.mkdir_p working_directory
    end

    in_wc do
        FileUtils.mkdir_p "types"
        header_files.each do |file|
            FileUtils.cp file, "types"
        end
        cmdline = ["typegen"]
        if !transports.empty?
            cmdline << "--transports=#{transports.join(",")}"
        end
        cmdline << "-o" << "typekit_output" << name << "types"

        assert(system(*cmdline, redirect_to_logfile), "typegen failed, log file in #{logfile}")
    end

    compile_wc(nil, "typekit_output")
end

#call_make(*args) ⇒ Object


131
132
133
134
135
136
137
# File 'lib/orogen/gen/test.rb', line 131

def call_make(*args)
    make_cmd = ["make"]
    if ENV['TEST_MAKE_OPTIONS']
        make_cmd.concat(ENV['TEST_MAKE_OPTIONS'].split(','))
    end
    system(*make_cmd, *args, redirect_to_logfile)
end

#clear_wcObject


61
62
63
64
65
66
67
68
# File 'lib/orogen/gen/test.rb', line 61

def clear_wc
    if ENV['TEST_KEEP_WC'] != '1' && ENV['TEST_DONT_CLEAN'] != '1'
        if File.directory?(path_to_wc_root)
            FileUtils.rm_rf path_to_wc_root
            @working_directory = nil
        end
    end
end

#compile_and_test(project, test_bin) ⇒ Object


203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/orogen/gen/test.rb', line 203

def compile_and_test(project, test_bin)
    compile_wc(project) do
        FileUtils.cp 'templates/CMakeLists.txt', 'CMakeLists.txt'
        File.open('CMakeLists.txt', 'a') do |io|
            yield(io) if block_given?
        end
    end

    in_prefix do
        assert(system(test_bin, redirect_to_logfile), "failed to run test program #{test_bin}, see #{logfile} for output")
    end
end

#compile_wc(project = nil, *subdir) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/orogen/gen/test.rb', line 112

def compile_wc(project = nil, *subdir)
    in_wc(*subdir) do
        if project
            unless project.deffile
                project.deffile = File.join(working_directory, "#{project.name}.orogen")
            end
            project.generate
        end

        yield if block_given?

        FileUtils.mkdir('build') unless File.directory?('build')
        Dir.chdir('build') do
            assert(system("cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=#{prefix_directory} ..", redirect_to_logfile), "failed to configure, see #{logfile} for more details")
            assert(call_make, "failed to build, see #{logfile} for more details")
        end
    end
end

#copy_in_wc(file, destination = nil) ⇒ Object


70
71
72
73
74
75
76
77
# File 'lib/orogen/gen/test.rb', line 70

def copy_in_wc(file, destination = nil)
    if destination
        destination = File.expand_path(destination, working_directory)
        FileUtils.mkdir_p destination
    end

    FileUtils.cp File.expand_path(file, path_to_test), (destination || working_directory)
end

#create_wc(*subdir) ⇒ Object


52
53
54
55
56
57
58
59
# File 'lib/orogen/gen/test.rb', line 52

def create_wc(*subdir)
    required_wc = File.join(path_to_test, 'wc', *subdir)
    if working_directory != required_wc
        @working_directory = required_wc
        FileUtils.mkdir_p working_directory
        @subdir = subdir
    end
end

#in_prefix(&block) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/orogen/gen/test.rb', line 94

def in_prefix(&block)
    old_pkgconfig = ENV['PKG_CONFIG_PATH']
    in_wc do
        Dir.chdir("build") do
            assert(call_make('install'), "failed to install, see #{logfile} for more details")
        end

        ENV['PKG_CONFIG_PATH'] += ":" + File.join(prefix_directory, 'lib', 'pkgconfig')
        Dir.chdir(prefix_directory, &block)
    end
ensure
    ENV['PKG_CONFIG_PATH'] = old_pkgconfig
end

#in_wc(*subdir, &block) ⇒ Object


79
80
81
# File 'lib/orogen/gen/test.rb', line 79

def in_wc(*subdir, &block)
    Dir.chdir(File.join(working_directory, *subdir), &block)
end

#installObject


87
88
89
90
91
92
93
# File 'lib/orogen/gen/test.rb', line 87

def install
    in_wc do
        Dir.chdir("build") do
            assert(call_make('install'), "failed to install, see #{logfile} for more details")
        end
    end
end

#logfileObject


108
109
110
# File 'lib/orogen/gen/test.rb', line 108

def logfile
    File.join(working_directory, 'test.log')
end

#prefix_directoryObject


32
33
34
# File 'lib/orogen/gen/test.rb', line 32

def prefix_directory
    File.join(path_to_wc_root, "prefix", *subdir)
end

#redirect_to_logfileObject


83
84
85
# File 'lib/orogen/gen/test.rb', line 83

def redirect_to_logfile
    Hash[[STDOUT,STDERR] => [logfile, 'a']]
end

#setupObject


38
39
40
41
42
# File 'lib/orogen/gen/test.rb', line 38

def setup
    @subdir = Array.new
    @old_pkg_config = ENV['PKG_CONFIG_PATH'].dup if ENV['PKG_CONFIG_PATH']
    super if defined? super
end

#teardownObject


44
45
46
47
48
49
50
# File 'lib/orogen/gen/test.rb', line 44

def teardown
    clear_wc
    ENV['PKG_CONFIG_PATH'] = @old_pkg_config
    Utilrb::PkgConfig.clear_cache
    super if defined? super
    flexmock_teardown
end