Module: UnionStationHooks::SpecHelper

Extended by:
SpecHelper
Included in:
SpecHelper
Defined in:
lib/union_station_hooks_core/spec_helper.rb

Overview

Contains helper methods for use in unit tests across all the ‘union_station_hooks_*` gems.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/union_station_hooks_core/spec_helper.rb', line 36

def self.included(klass)
  # When included into another class, make sure that Utils
  # methods are made private.
  public_instance_methods(false).each do |method_name|
    klass.send(:private, method_name)
  end
end

Instance Method Details

#debug?Boolean

Checks whether ‘initialize_debugging` enabled debugging mode.

Returns:

  • (Boolean)


141
142
143
# File 'lib/union_station_hooks_core/spec_helper.rb', line 141

def debug?
  @@debug
end

#debug_shellObject

Opens a debug shell. By default, the debug shell is opened in the current working directory. If the current module has the ‘prepare_debug_shell` method, that method is called before opening the debug shell. The method could, for example, change the working directory.

This method does not raise an exception if the debug shell exits with an error.



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/union_station_hooks_core/spec_helper.rb', line 185

def debug_shell
  puts '------ Opening debug shell -----'
  @orig_dir = Dir.pwd
  begin
    if respond_to?(:prepare_debug_shell)
      prepare_debug_shell
    end
    system('bash')
  ensure
    Dir.chdir(@orig_dir)
  end
  puts '------ Exiting debug shell -----'
end

#dump_file_path(category = 'requests') ⇒ Object

Returns the path of a specific UstRouter dump file. Requires that ‘@dump_dir` is set.



201
202
203
204
# File 'lib/union_station_hooks_core/spec_helper.rb', line 201

def dump_file_path(category = 'requests')
  raise '@dump_dir variable required' if !@dump_dir
  "#{@dump_dir}/#{category}"
end

#eventually(deadline_duration = 3, check_interval = 0.05) ⇒ Object

Asserts that something should eventually happen. This is done by checking that the given block eventually returns true. The block is called once every ‘check_interval` msec. If the block does not return true within `deadline_duration` secs, then an exception is raised.



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/union_station_hooks_core/spec_helper.rb', line 232

def eventually(deadline_duration = 3, check_interval = 0.05)
  deadline = Time.now + deadline_duration
  while Time.now < deadline
    if yield
      return
    else
      sleep(check_interval)
    end
  end
  raise 'Time limit exceeded'
end

#find_passenger_configObject

Lookup the ‘passenger-config` command, either by respecting the `PASSENGER_CONFIG` environment variable, or by looking it up in `PATH`. If the command cannot be found, the current process aborts with an error message.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/union_station_hooks_core/spec_helper.rb', line 56

def find_passenger_config
  passenger_config = ENV['PASSENGER_CONFIG']
  if passenger_config.nil? || passenger_config.empty?
    passenger_config = find_passenger_config_vendor ||
      find_passenger_config_in_path
  end
  if passenger_config.nil? || passenger_config.empty?
    abort 'ERROR: The unit tests are to be run against a specific ' \
      'Passenger version. However, the \'passenger-config\' command is ' \
      'not found. Please install Passenger, or (if you are sure ' \
      'Passenger is installed) set the PASSENGER_CONFIG environment ' \
      'variable to the \'passenger-config\' command.'
  end
  passenger_config
end

#find_passenger_config_in_pathObject

Looks for the passenger-config command in PATH, returning nil if not found.



74
75
76
77
78
79
80
# File 'lib/union_station_hooks_core/spec_helper.rb', line 74

def find_passenger_config_in_path
  ENV['PATH'].split(':').each do |path|
    if File.exist?("#{path}/passenger-config")
      return "#{path}/passenger-config"
    end
  end
end

#find_passenger_config_vendorObject

Checks whether this union_station_hooks installation is a copy that is vendored into Passenger, and if so, returns the full path to the containing Passenger’s passenger-config command.



85
86
87
88
89
90
91
92
# File 'lib/union_station_hooks_core/spec_helper.rb', line 85

def find_passenger_config_vendor
  path = "#{UnionStationHooks::ROOT}/../../../../../bin/passenger-config"
  if File.exist?(path)
    File.expand_path(path)
  else
    nil
  end
end

#get(path) ⇒ Object



162
163
164
165
# File 'lib/union_station_hooks_core/spec_helper.rb', line 162

def get(path)
  response = get_response(path)
  return_200_response_body(path, response)
end

#get_response(path) ⇒ Object



157
158
159
160
# File 'lib/union_station_hooks_core/spec_helper.rb', line 157

def get_response(path)
  uri = URI.parse("#{root_url}#{path}")
  Net::HTTP.get_response(uri)
end

#initialize!Object

To be called during initialization of the test suite.



45
46
47
48
49
50
# File 'lib/union_station_hooks_core/spec_helper.rb', line 45

def initialize!
  load_passenger
  initialize_ush_api
  initialize_debugging
  undo_bundler
end

#initialize_debuggingObject



121
122
123
124
125
126
127
# File 'lib/union_station_hooks_core/spec_helper.rb', line 121

def initialize_debugging
  @@debug = !ENV['DEBUG'].to_s.empty?
  if @@debug
    UnionStationHooks.require_lib('log')
    UnionStationHooks::Log.debugging = true
  end
end

#initialize_ush_apiObject



117
118
119
# File 'lib/union_station_hooks_core/spec_helper.rb', line 117

def initialize_ush_api
  UnionStationHooks.require_lib('api')
end

#load_passengerObject

Uses ‘find_passenger_config` to lookup a Passenger installation, and loads the Passenger Ruby support library associated with that installation. All the constants defined in the Passenger Ruby support library are loaded. In addition, checks whether the Passenger agent executable is installed. If not, the current process aborts with an error message.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/union_station_hooks_core/spec_helper.rb', line 100

def load_passenger
  passenger_config = find_passenger_config
  puts "Using Passenger installation at: #{passenger_config}"
  passenger_ruby_libdir = `#{passenger_config} about ruby-libdir`.strip
  require("#{passenger_ruby_libdir}/phusion_passenger")
  PhusionPassenger.locate_directories
  PhusionPassenger.require_passenger_lib 'constants'
  puts "Loaded Passenger version #{PhusionPassenger::VERSION_STRING}"

  agent = PhusionPassenger.find_support_binary(PhusionPassenger::AGENT_EXE)
  if agent.nil?
    abort "ERROR: The Passenger agent isn't installed. Please ensure " \
      "that it is installed, e.g. using:\n\n" \
      "  #{passenger_config} install-agent\n\n"
  end
end

#read_dump_file(category = 'requests') ⇒ Object

Reads the contents of a specific UstRouter dump file. Requires that ‘@dump_dir` is set.

Raises:

  • SystemError Something went wrong during reading.



210
211
212
# File 'lib/union_station_hooks_core/spec_helper.rb', line 210

def read_dump_file(category = 'requests')
  File.read(dump_file_path(category))
end

#return_200_response_body(path, response) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/union_station_hooks_core/spec_helper.rb', line 167

def return_200_response_body(path, response)
  if response.code == '200'
    response.body
  else
    raise "HTTP request to #{path} failed.\n" \
      "Code: #{response.code}\n" \
      "Body:\n" \
      "#{response.body}"
  end
end

#should_never_happen(deadline_duration = 0.5, check_interval = 0.05) ⇒ Object

Asserts that something should never happen. This is done by checking that the given block never returns true. The block is called once every ‘check_interval` msec, until `deadline_duration` seconds have passed. If the block ever returns true, then an exception is raised.



248
249
250
251
252
253
254
255
256
257
# File 'lib/union_station_hooks_core/spec_helper.rb', line 248

def should_never_happen(deadline_duration = 0.5, check_interval = 0.05)
  deadline = Time.now + deadline_duration
  while Time.now < deadline
    if yield
      raise "That which shouldn't happen happened anyway"
    else
      sleep(check_interval)
    end
  end
end

#silence_warningsObject

Makes ‘UnionStationHooks::Log.warn` not print anything.



224
225
226
# File 'lib/union_station_hooks_core/spec_helper.rb', line 224

def silence_warnings
  UnionStationHooks::Log.warn_callback = lambda { |_message| }
end

#undo_bundlerObject

Unit tests must undo the Bundler environment so that the gem’s own Gemfile doesn’t affect subprocesses that may have their own Gemfile.



132
133
134
135
136
137
138
# File 'lib/union_station_hooks_core/spec_helper.rb', line 132

def undo_bundler
  clean_env = nil
  Bundler.with_clean_env do
    clean_env = ENV.to_hash
  end
  ENV.replace(clean_env)
end

#wait_for_dump_file_existance(category = 'requests') ⇒ Object

Waits until the dump file exists. Raises an error if this doesn’t become true within the default #eventually timeout.



217
218
219
220
221
# File 'lib/union_station_hooks_core/spec_helper.rb', line 217

def wait_for_dump_file_existance(category = 'requests')
  eventually do
    File.exist?(dump_file_path(category))
  end
end

#write_file(path, content) ⇒ Object

Writes the given content to the file at the given path. If or or more parent directories don’t exist, then they are created.



147
148
149
150
151
152
153
154
155
# File 'lib/union_station_hooks_core/spec_helper.rb', line 147

def write_file(path, content)
  dir = File.dirname(path)
  if !File.exist?(dir)
    FileUtils.mkdir_p(dir)
  end
  File.open(path, 'wb') do |f|
    f.write(content)
  end
end