File: spec_helper.rb

package info (click to toggle)
ruby-foreman 0.82.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 684 kB
  • ctags: 246
  • sloc: ruby: 1,979; sh: 88; makefile: 5
file content (176 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download
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
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
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
require "rspec"
require "timecop"
require "fakefs/safe"
require "fakefs/spec_helpers"

unless ENV['ADTTMP']
  ENV['PATH'] = 'bin:' + ENV['PATH']
end

def mock_export_error(message)
  expect { yield }.to raise_error(Foreman::Export::Exception, message)
end

def mock_error(subject, message)
  mock_exit do
    expect(subject).to receive(:puts).with("ERROR: #{message}")
    yield
  end
end

def make_pipe
  IO.method(:pipe).arity.zero? ? IO.pipe : IO.pipe("BINARY")
end

def foreman(args)
  capture_stdout do
    begin
      Foreman::CLI.start(args.split(" "))
    rescue SystemExit
    end
  end
end

def ruby
  RbConfig::CONFIG['RUBY_INSTALL_NAME']
end

def forked_foreman(args)
  rd, wr = make_pipe
  Process.spawn("#{ruby} -S foreman #{args}", :out => wr, :err => wr)
  wr.close
  rd.read
end

def fork_and_capture(&blk)
  rd, wr = make_pipe
  pid = fork do
    rd.close
    wr.sync = true
    $stdout.reopen wr
    $stderr.reopen wr
    blk.call
    $stdout.flush
    $stdout.close
  end
  wr.close
  Process.wait pid
  buffer = ""
  until rd.eof?
    buffer += rd.gets
  end
end

def fork_and_get_exitstatus(args)
  pid = Process.spawn("#{ruby} -S foreman #{args}", :out => "/dev/null", :err => "/dev/null")
  Process.wait(pid)
  $?.exitstatus
end

def mock_exit(&block)
  expect(block).to raise_error(SystemExit)
end

def write_foreman_config(app)
  File.open("/etc/foreman/#{app}.conf", "w") do |file|
    file.puts %{#{app}_processes="alpha bravo"}
    file.puts %{#{app}_alpha="1"}
    file.puts %{#{app}_bravo="2"}
  end
end

def write_procfile(procfile="Procfile", alpha_env="")
  FileUtils.mkdir_p(File.dirname(procfile))
  File.open(procfile, "w") do |file|
    file.puts "alpha: ./alpha" + " #{alpha_env}".rstrip
    file.puts "\n"
    file.puts "bravo:\t./bravo"
    file.puts "foo_bar:\t./foo_bar"
    file.puts "foo-bar:\t./foo-bar"
  end
  File.expand_path(procfile)
end

def write_file(file)
  FileUtils.mkdir_p(File.dirname(file))
  File.open(file, 'w') do |f|
    yield(f)
  end
end

def write_env(env=".env", options={"FOO"=>"bar"})
  File.open(env, "w") do |file|
    options.each do |key, val|
      file.puts "#{key}=#{val}"
    end
  end
end

def without_fakefs
  FakeFS.deactivate!
  ret = yield
  FakeFS.activate!
  ret
end

def load_export_templates_into_fakefs(type)
  without_fakefs do
    Dir[File.expand_path("../../data/export/#{type}/**/*", __FILE__)].inject({}) do |hash, file|
      next(hash) if File.directory?(file)
      hash.update(file => File.read(file))
    end
  end.each do |filename, contents|
    FileUtils.mkdir_p File.dirname(filename)
    File.open(filename, "w") do |f|
      f.puts contents
    end
  end
end

def resource_path(filename)
  File.expand_path("../resources/#{filename}", __FILE__)
end

def example_export_file(filename)
  FakeFS.deactivate!
  data = File.read(File.expand_path(resource_path("export/#{filename}"), __FILE__))
  FakeFS.activate!
  data
end

def preserving_env
  old_env = ENV.to_hash
  begin
    yield
  ensure
    ENV.clear
    ENV.update(old_env)
  end
end

def normalize_space(s)
  s.gsub(/\n[\n\s]*/, "\n")
end

def capture_stdout
  old_stdout = $stdout.dup
  rd, wr = make_pipe
  $stdout = wr
  yield
  wr.close
  rd.read
ensure
  $stdout = old_stdout
end

RSpec.configure do |config|
  config.color = true
  config.order = 'rand'
  config.include FakeFS::SpecHelpers, :fakefs
  config.before(:each) do
    FileUtils.mkdir_p('/tmp')
  end
  config.after(:each) do
    FileUtils.rm_rf('/tmp')
  end
end