File: spec_helper.rb

package info (click to toggle)
ruby-dbus 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 3,786; sh: 53; makefile: 8
file content (106 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
coverage = if ENV["COVERAGE"]
             ENV["COVERAGE"] == "true"
           else
             # heuristics: enable for interactive builds (but not in OBS)
             # or in Travis
             ENV["DISPLAY"] || ENV["TRAVIS"]
           end

if coverage
  require "simplecov"
  SimpleCov.root File.expand_path("../..", __FILE__)

  # do not cover specs
  SimpleCov.add_filter "_spec.rb"
  # do not cover the activesupport helpers
  SimpleCov.add_filter "/core_ext/"

  # use coveralls for on-line code coverage reporting at Travis CI
  if ENV["TRAVIS"]
    require "coveralls"
  end
  SimpleCov.start
end

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)

if Object.const_defined? "RSpec"
  # http://betterspecs.org/#expect
  RSpec.configure do |config|
    config.expect_with :rspec do |c|
      c.syntax = :expect
    end
  end
end

require "tempfile"
require "timeout"

TOPDIR = File.expand_path("../..", __FILE__)

# path of config file for a private bus
def config_file_path
  "#{TOPDIR}/spec/tools/dbus-limited-session.conf"
end

# set ENV[variable] to value and restore it after block is done
def with_env(variable, value, &block)
  old_value = ENV[variable]
  ENV[variable] = value
  block.call
  ENV[variable] = old_value
end

# Set up a private session bus and run *block* with that.
def with_private_bus(&block)
  address_file = Tempfile.new("dbus-address")
  pid_file     = Tempfile.new("dbus-pid")
  output_file  = Tempfile.new("dbus-output") # just in case

  temp_dir = Dir.mktmpdir
  with_env("XDG_DATA_DIRS", temp_dir) do
    cmd = "dbus-daemon --nofork --config-file=#{config_file_path} " \
          "--print-address=3 3>#{address_file.path} " \
          "--print-pid=4 4>#{pid_file.path} " \
          ">#{output_file.path} 2>&1 &"
    system cmd

    # wait until dbus-daemon writes the info
    Timeout.timeout(10) do
      until File.size?(address_file) && File.size?(pid_file)
        sleep 0.1
      end
    end

    address = address_file.read.chomp
    pid = pid_file.read.chomp.to_i

    with_env("DBUS_SESSION_BUS_ADDRESS", address) do
      block.call
    end

    Process.kill("TERM", pid)
  end
  FileUtils.rm_rf temp_dir
end

def with_service_by_activation(&block)
  name = "org.ruby.service"
  exec = "#{TOPDIR}/spec/service_newapi.rb"

  service_dir = "#{ENV["XDG_DATA_DIRS"]}/dbus-1/services"
  FileUtils.mkdir_p service_dir
  # file name actually does not need to match the service name
  File.open("#{service_dir}/#{name}.service", "w") do |f|
    s = <<-TEXT.gsub(/^\s*/, "")
      [D-BUS Service]
      Name=#{name}
      Exec=#{exec}
    TEXT
    f.write(s)
  end

  block.call

  system "pkill -f #{exec}"
end