-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathspec_helper.rb
98 lines (80 loc) · 2.38 KB
/
spec_helper.rb
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
# frozen_string_literal: true
require_relative "coverage_helper"
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
# http://betterspecs.org/#expect
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
require "tempfile"
require "timeout"
TOPDIR = File.expand_path("..", __dir__)
# 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/mock-service/spaghetti-monster.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
# This would kill also other instances,
# namely on the bus set up by test_env.
## system "pkill -f #{exec}"
end
# Make a binary string from readable YAML pieces; see data/marshall.yaml
def buffer_from_yaml(parts)
strings = parts.flatten.map do |part|
if part.is_a? Integer
part.chr
else
part
end
end
strings.join.force_encoding(Encoding::BINARY)
end