Module: Autoproj::SelfTest

Included in:
Minitest::Test
Defined in:
lib/autoproj/test.rb

Overview

This module is the common setup for all tests

It should be included in the toplevel describe blocks

Examples:

require 'rubylib/test'
describe Autoproj do
  include Autoproj::SelfTest
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#wsObject (readonly)

The workspace created by the last call to #ws_create



55
56
57
# File 'lib/autoproj/test.rb', line 55

def ws
  @ws
end

#ws_os_package_resolverObject (readonly)

Returns the value of attribute ws_os_package_resolver.



301
302
303
# File 'lib/autoproj/test.rb', line 301

def ws_os_package_resolver
  @ws_os_package_resolver
end

#ws_package_managersHash<String,PackageManagers::Manager> (readonly)

Define package managers for the next workspace created by #ws_create

Use #ws_define_package_manager

Two package managers called ‘os’ and ‘os_indep’ are always created, ‘os’ is used as the os package manager.

Returns:



53
54
55
# File 'lib/autoproj/test.rb', line 53

def ws_package_managers
  @ws_package_managers
end

Instance Method Details

#autoproj_gemfile_to_local_checkoutObject



131
132
133
134
135
136
137
138
139
# File 'lib/autoproj/test.rb', line 131

def autoproj_gemfile_to_local_checkout
    autoproj_dir  = find_gem_dir("autoproj").full_gem_path
    autobuild_dir = find_gem_dir("autobuild").full_gem_path
    <<~GEMFILE
        source "https://rubygems.org"
        gem "autoproj", path: "#{autoproj_dir}"
        gem "autobuild", path: "#{autobuild_dir}"
    GEMFILE
end

#capture_deprecation_messageObject



267
268
269
270
271
272
273
274
275
# File 'lib/autoproj/test.rb', line 267

def capture_deprecation_message
    level = Autoproj.warn_deprecated_level
    Autoproj.warn_deprecated_level = -1
    capture_subprocess_io do
        yield
    end
ensure
    Autoproj.warn_deprecated_level = level
end

#create_bootstrapObject



106
107
108
# File 'lib/autoproj/test.rb', line 106

def create_bootstrap
    ws_create
end

#data_path(*args) ⇒ Object



101
102
103
104
# File 'lib/autoproj/test.rb', line 101

def data_path(*args)
    File.expand_path(File.join(*args),
                     File.join(__dir__, "..", "..", "test", "data"))
end

#find_bundled_gem_path(bundler, gem_name, gemfile) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/autoproj/test.rb', line 277

def find_bundled_gem_path(bundler, gem_name, gemfile)
    out_r, out_w = IO.pipe
    result = Autoproj.bundler_unbundled_system(
        bundler, "show", gem_name,
        out: out_w,
        chdir: File.dirname(gemfile)
    )
    out_w.close
    output = out_r.read.chomp
    assert result, "bundler show #{gem_name} failed, output: '#{output}'"
    output
end

#find_gem_dir(gem_name) ⇒ Object



124
125
126
127
128
129
# File 'lib/autoproj/test.rb', line 124

def find_gem_dir(gem_name)
    Bundler.definition.specs.each do |spec|
        return spec if spec.name == gem_name
    end
    nil
end

#fixture_gem_homeObject



204
205
206
# File 'lib/autoproj/test.rb', line 204

def fixture_gem_home
    File.join(__dir__, "..", "..", "vendor", "test_gem_home")
end

#gemfile_arubaObject



498
499
500
501
502
503
504
505
506
507
508
# File 'lib/autoproj/test.rb', line 498

def gemfile_aruba
    base_dir = File.expand_path("../../", __dir__)
    gemfile_path = File.join(base_dir, "tmp", "Gemfile.local")
    File.open(gemfile_path, "w") do |io|
        io.write <<~GEMFILE
        source 'https://rubygems.org'
        gem 'autoproj', path: '#{base_dir}'
        GEMFILE
    end
    gemfile_path
end

#in_wsObject



295
296
297
298
299
# File 'lib/autoproj/test.rb', line 295

def in_ws
    Dir.chdir(@ws.root_dir) do
        yield
    end
end

#invoke_test_script(name, *arguments, dir: make_tmpdir, gemfile_source: nil, use_autoproj_from_rubygems: (ENV["USE_AUTOPROJ_FROM_RUBYGEMS"] == "1"), interactive: true, seed_config: File.join(scripts_dir, "seed-config.yml"), env: {}, display_output: false, copy_from: nil, **system_options) ⇒ Object



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
167
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
201
202
# File 'lib/autoproj/test.rb', line 141

def invoke_test_script(name, *arguments,
    dir: make_tmpdir,
    gemfile_source: nil,
    use_autoproj_from_rubygems: (ENV["USE_AUTOPROJ_FROM_RUBYGEMS"] == "1"),
    interactive: true,
    seed_config: File.join(scripts_dir, "seed-config.yml"),
    env: {}, display_output: false, copy_from: nil,
    **system_options)
    package_base_dir = File.expand_path(File.join("..", ".."), __dir__)

    script = File.expand_path(name, scripts_dir)
    unless File.file?(script)
        raise ArgumentError, "no test script #{name} in #{scripts_dir}"
    end

    if env["HOME"]
        @home_dir = env["HOME"]
    else
        env["HOME"] = (@home_dir ||= make_tmpdir)
    end
    arguments << "--seed-config" << seed_config if seed_config

    if gemfile_source || !use_autoproj_from_rubygems
        gemfile_path = File.join(dir, "Gemfile-dev")
        File.open(gemfile_path, "w") do |io|
            io.puts(gemfile_source || autoproj_gemfile_to_local_checkout)
        end
        arguments << "--gemfile" << gemfile_path
    end

    arguments << "--no-interactive" unless interactive

    if copy_from
        test_workspace = File.expand_path(copy_from, scripts_dir)
        if File.directory?(test_workspace)
            FileUtils.cp_r test_workspace, dir
            dir = File.join(dir, File.basename(test_workspace))
        end
    end
    result = nil
    stdout, stderr = capture_subprocess_io do
        default_env = Hash[
            "TEST_COMMAND_NAME" => to_s.gsub(/[^\w]/, "_"),
            "PACKAGE_BASE_DIR" => package_base_dir,
            "RUBY" => Gem.ruby
        ]
        result = Autoproj.bundler_unbundled_system(
            default_env.merge(env), script, *arguments,
            in: :close, **Hash[chdir: dir].merge(system_options)
        )
    end

    if !result
        puts stdout
        puts stderr
        flunk("test script #{name} failed")
    elsif display_output
        puts stdout
        puts stderr
    end
    [dir, stdout, stderr]
end

#make_tmpdirObject



114
115
116
117
118
# File 'lib/autoproj/test.rb', line 114

def make_tmpdir
    dir = Dir.mktmpdir
    @tmpdir << dir
    dir
end

#prepare_fixture_gem_homeObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/autoproj/test.rb', line 208

def prepare_fixture_gem_home
    FileUtils.rm_rf fixture_gem_home
    FileUtils.mkdir_p File.dirname(fixture_gem_home)
    bundled_gems_path = File.expand_path(File.join("..", ".."),
                                         find_gem_dir("utilrb").full_gem_path)
    FileUtils.cp_r bundled_gems_path, fixture_gem_home

    vendor = File.join(__dir__, "..", "..", "vendor")
    bundler_filename = "bundler-#{Bundler::VERSION}.gem"
    cached_bundler_gem = File.join(vendor, bundler_filename)
    unless File.file?(cached_bundler_gem)
        FileUtils.mkdir_p vendor
        Autoproj.bundler_unbundled_system(
            Ops::Install.guess_gem_program, "fetch", "-v",
            Bundler::VERSION, "bundler", chdir: vendor
        )

        unless File.file?(cached_bundler_gem)
            existing = Dir.enum_for(:glob, File.join(vendor, "*")).to_a.sort
            raise "cannot download the bundler gem. "\
                  "Expected #{bundler_filename}, found: #{existing.join(', ')}"
        end
    end

    capture_subprocess_io do
        Autoproj.bundler_unbundled_system(
            Hash["GEM_HOME" => fixture_gem_home, "GEM_PATH" => nil],
            Ops::Install.guess_gem_program, "install", "--no-document",
            cached_bundler_gem
        )
    end
end

#scripts_dirObject



120
121
122
# File 'lib/autoproj/test.rb', line 120

def scripts_dir
    File.expand_path(File.join("..", "..", "test", "scripts"), __dir__)
end

#setupObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/autoproj/test.rb', line 57

def setup
    if ENV["AUTOPROJ_CURRENT_ROOT"]
        raise "cannot have a workspace's env.sh loaded while running the "\
              "Autoproj test suite"
    end

    Autobuild.progress_display_mode = :newline

    if defined?(Autoproj::CLI::Main)
        Autoproj::CLI::Main.default_report_on_package_failures = :raise
    end
    FileUtils.rm_rf fixture_gem_home
    @gem_server_pid = nil
    @tmpdir = Array.new
    @ws = nil
    @ws_package_managers = Hash.new
    Autobuild.logdir = make_tmpdir
    ws_define_package_manager "os"
    ws_define_package_manager "os_indep"
    Autobuild.progress_display_period = 0

    super
end

#skip_long_tests?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/autoproj/test.rb', line 110

def skip_long_tests?
    ENV["AUTOPROJ_SKIP_LONG_TESTS"] == "1"
end

#start_gem_server(path = fixture_gem_home) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/autoproj/test.rb', line 241

def start_gem_server(path = fixture_gem_home)
    require "socket"
    require "rubygems/server"
    if @gem_server_pid
        raise ArgumentError,
              "#start_gem_server already called, "\
              "call stop_gem_server before calling start_gem_server again"
    end
    @gem_server_pid = spawn(
        Hash["RUBYOPT" => nil],
        Gem.ruby, Ops::Install.guess_gem_program, "server",
        "--quiet", "--dir", path, out: :close, err: :close
    )
    loop do
        TCPSocket.new("127.0.0.1", 8808)
        break
    rescue Errno::ECONNREFUSED
    end
end

#stop_gem_serverObject



261
262
263
264
265
# File 'lib/autoproj/test.rb', line 261

def stop_gem_server
    Process.kill "INT", @gem_server_pid
    Process.waitpid @gem_server_pid
    @gem_server_pid = nil
end

#teardownObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/autoproj/test.rb', line 81

def teardown
    super
    @tmpdir.each do |dir|
        FileUtils.remove_entry_secure dir
    end
    Rake::Task.clear
    Autobuild::Package.clear
    Autoproj.silent = false

    stop_gem_server if @gem_server_pid

    FileUtils.rm_rf fixture_gem_home
    if defined?(Autoproj::CLI::Main)
        Autoproj::CLI::Main.default_report_on_package_failures = nil
    end
    if ENV.delete("AUTOPROJ_CURRENT_ROOT")
        raise "AUTOPROJ_CURRENT_ROOT has been set by this test !!!!"
    end
end

#workspace_env(dir, varname) ⇒ Object



290
291
292
293
# File 'lib/autoproj/test.rb', line 290

def workspace_env(dir, varname)
    _, stdout, = invoke_test_script "display-env.sh", varname, dir: dir
    stdout.chomp
end

#ws_add_metapackage_to_layout(name, *packages) ⇒ Object



382
383
384
385
386
# File 'lib/autoproj/test.rb', line 382

def ws_add_metapackage_to_layout(name, *packages)
    meta = ws.manifest.metapackage(name, *packages)
    ws.manifest.add_metapackage_to_layout(meta)
    meta
end

#ws_add_osdep_entries_to_layout(entries) ⇒ Object



392
393
394
395
396
397
# File 'lib/autoproj/test.rb', line 392

def ws_add_osdep_entries_to_layout(entries)
    ws_os_package_resolver.add_entries(entries)
    entries.each_key do |pkg_name|
        ws.manifest.add_package_to_layout(pkg_name)
    end
end

#ws_add_package_set_to_layout(name, vcs = VCSDefinition.from_raw({ type: "none" }), **options) ⇒ Object



374
375
376
377
378
379
380
# File 'lib/autoproj/test.rb', line 374

def ws_add_package_set_to_layout(
    name, vcs = VCSDefinition.from_raw({ type: "none" }), **options
)
    package_set = ws_define_package_set(name, vcs, **options)
    ws.manifest.add_package_set_to_layout(package_set)
    package_set
end

#ws_add_package_to_layout(package_type, package_name, package_set: ws.manifest.main_package_set, &block) ⇒ Object



429
430
431
432
433
434
435
# File 'lib/autoproj/test.rb', line 429

def ws_add_package_to_layout(package_type, package_name,
    package_set: ws.manifest.main_package_set, &block)
    pkg = ws_define_package(package_type, package_name,
                            package_set: package_set, &block)
    ws.manifest.add_package_to_layout(pkg)
    pkg
end

#ws_clear_layoutObject



348
349
350
# File 'lib/autoproj/test.rb', line 348

def ws_clear_layout
    ws.manifest.clear_layout
end

#ws_create(dir = make_tmpdir, partial_config: false) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/autoproj/test.rb', line 321

def ws_create(dir = make_tmpdir, partial_config: false)
    require "autoproj/ops/main_config_switcher"
    FileUtils.cp_r Ops::MainConfigSwitcher::MAIN_CONFIGURATION_TEMPLATE,
                   File.join(dir, "autoproj")
    FileUtils.mkdir_p File.join(dir, ".autoproj")

    ws_create_os_package_resolver
    @ws = Workspace.new(
        dir, os_package_resolver: ws_os_package_resolver,
             package_managers: ws_package_managers
    )

    unless partial_config
        ws.config.set "osdeps_mode", "all"
        ws.config.set "apt_dpkg_update", true
    end
    ws.config.set "GITHUB", "http,ssh", true
    ws.config.set "GITORIOUS", "http,ssh", true
    ws.config.set "gems_install_path", File.join(dir, "gems")
    ws.prefix_dir = make_tmpdir
    ws.config.save

    # Make a valid (albeit empty) Gemfile
    File.open(File.join(ws.dot_autoproj_dir, "Gemfile"), "w").close
    ws
end

#ws_create_git_package_set(name, source_data = Hash.new) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/autoproj/test.rb', line 459

def ws_create_git_package_set(name, source_data = Hash.new)
    dir = make_tmpdir
    unless system("git", "init", chdir: dir, out: :close)
        raise "failed to run git init"
    end

    File.open(File.join(dir, "source.yml"), "w") do |io|
        YAML.dump(Hash["name" => name].merge(source_data), io)
    end
    unless system("git", "add", "source.yml", chdir: dir, out: :close)
        raise "failed to add the source.yml"
    end

    unless system("git", "commit", "-m", "add source.yml",
                  chdir: dir, out: :close)
        raise "failed to commit the source.yml"
    end

    dir
end

#ws_create_local_package_set(name, path, source_data: Hash.new, **options) ⇒ Object



363
364
365
366
367
368
369
370
371
372
# File 'lib/autoproj/test.rb', line 363

def ws_create_local_package_set(name, path, source_data: Hash.new, **options)
    vcs = VCSDefinition.from_raw({ type: "local", url: path })
    package_set = PackageSet.new(ws, vcs, name: name, **options)
    FileUtils.mkdir_p(path)
    File.open(File.join(path, "source.yml"), "w") do |io|
        YAML.dump(Hash["name" => name].merge(source_data), io)
    end
    ws.manifest.register_package_set(package_set)
    package_set
end

#ws_create_os_package_resolverObject



313
314
315
316
317
318
319
# File 'lib/autoproj/test.rb', line 313

def ws_create_os_package_resolver
    @ws_os_package_resolver = OSPackageResolver.new(
        operating_system: [["test_os_family"], ["test_os_version"]],
        package_managers: ws_package_managers.keys,
        os_package_manager: "os"
    )
end

#ws_create_package_file(pkg, name, content) ⇒ Object



489
490
491
492
493
494
495
496
# File 'lib/autoproj/test.rb', line 489

def ws_create_package_file(pkg, name, content)
    path = File.join(pkg.autobuild.srcdir, name)
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, "w") do |io|
        io.write content
    end
    path
end

#ws_create_package_set_file(pkg_set, name, content) ⇒ Object



480
481
482
483
484
485
486
487
# File 'lib/autoproj/test.rb', line 480

def ws_create_package_set_file(pkg_set, name, content)
    path = File.join(pkg_set.raw_local_dir, name)
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, "w") do |io|
        io.write content
    end
    path
end

#ws_define_osdep_entries(entries, file: nil) ⇒ Object



388
389
390
# File 'lib/autoproj/test.rb', line 388

def ws_define_osdep_entries(entries, file: nil)
    ws_os_package_resolver.add_entries(entries, file: file)
end

#ws_define_package(package_type, package_name, package_set: ws.manifest.main_package_set, create: true, &block) ⇒ Object



399
400
401
402
403
404
405
406
# File 'lib/autoproj/test.rb', line 399

def ws_define_package(package_type, package_name,
    package_set: ws.manifest.main_package_set,
    create: true, &block)
    package = Autobuild.send(package_type, package_name)
    ws_setup_package(
        package, package_set: package_set, create: create, &block
    )
end

#ws_define_package_manager(name, strict: false, call_while_empty: false) ⇒ Object



303
304
305
306
307
308
309
310
311
# File 'lib/autoproj/test.rb', line 303

def ws_define_package_manager(name, strict: false, call_while_empty: false)
    manager = Class.new(PackageManagers::Manager)
    manager.class_eval do
        define_method(:strict?) { strict }
        define_method(:call_while_empty?) { call_while_empty }
    end
    manager = flexmock(manager)
    ws_package_managers[name] = manager
end

#ws_define_package_overrides(package, package_set, vcs_spec) ⇒ Object



425
426
427
# File 'lib/autoproj/test.rb', line 425

def ws_define_package_overrides(package, package_set, vcs_spec)
    package_set.add_overrides_entry(package.name, vcs_spec)
end

#ws_define_package_set(name, vcs = VCSDefinition.from_raw({ type: "none" }), raw_local_dir: PackageSet.raw_local_dir_of(ws, vcs)) ⇒ Object



352
353
354
355
356
357
358
359
360
361
# File 'lib/autoproj/test.rb', line 352

def ws_define_package_set(
    name, vcs = VCSDefinition.from_raw({ type: "none" }),
    raw_local_dir: PackageSet.raw_local_dir_of(ws, vcs)
)
    package_set = PackageSet.new(
        ws, vcs, name: name, raw_local_dir: raw_local_dir
    )
    ws.manifest.register_package_set(package_set)
    package_set
end

#ws_define_package_vcs(package, vcs_spec) ⇒ Object



417
418
419
# File 'lib/autoproj/test.rb', line 417

def ws_define_package_vcs(package, vcs_spec)
    package.package_set.add_version_control_entry(package.name, vcs_spec)
end

#ws_resolve_vcs(package) ⇒ Object



421
422
423
# File 'lib/autoproj/test.rb', line 421

def ws_resolve_vcs(package)
    package.vcs = ws.manifest.importer_definition_for(package)
end

#ws_set_overrides_entry(package, package_set, entry) ⇒ Object



441
442
443
# File 'lib/autoproj/test.rb', line 441

def ws_set_overrides_entry(package, package_set, entry)
    package_set.add_overrides_entry(package.name, entry)
end

#ws_set_version_control_entry(package, entry) ⇒ Object



437
438
439
# File 'lib/autoproj/test.rb', line 437

def ws_set_version_control_entry(package, entry)
    package.package_set.add_version_control_entry(package.name, entry)
end

#ws_setup_package(package, package_set: ws.manifest.main_package_set, create: true) {|package| ... } ⇒ Object

Yields:

  • (package)


408
409
410
411
412
413
414
415
# File 'lib/autoproj/test.rb', line 408

def ws_setup_package(package, package_set: ws.manifest.main_package_set,
    create: true)
    package.srcdir = File.join(ws.root_dir, package.name.to_s)
    FileUtils.mkdir_p package.srcdir if create
    autoproj_package = ws.register_package(package, nil, package_set)
    yield(package) if block_given?
    autoproj_package
end

#ws_setup_package_dirs(package, create_srcdir: true) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/autoproj/test.rb', line 445

def ws_setup_package_dirs(package, create_srcdir: true)
    package.autobuild.srcdir = srcdir = File.join(ws.root_dir, package.name)
    if create_srcdir
        FileUtils.mkdir_p srcdir
    elsif File.directory?(srcdir)
        FileUtils.rm_rf srcdir
    end
    package.autobuild.builddir = builddir =
        File.join(ws.root_dir, "build", package.name)
    package.autobuild.prefix = prefix =
        File.join(ws.root_dir, "prefix", package.name)
    [srcdir, builddir, prefix]
end