Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 14 additions & 55 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def self::included( mod )

end


#
# Examples
#
Expand Down Expand Up @@ -121,93 +120,61 @@ def self::included( mod )
'white' => 37, 'on_white' => 47
}


###############
module_function
###############

module Loggable
### Create a string that contains the ANSI codes specified and return it
def ansi_code( *attributes )
attributes.flatten!
attributes.collect! {|at| at.to_s }

return '' unless /(?:vt10[03]|xterm(?:-color)?|linux|screen)/i =~ ENV['TERM']
attributes = ANSI_ATTRIBUTES.values_at( *attributes ).compact.join(';')
attrs = ANSI_ATTRIBUTES.values_at(*attributes).compact.join(';')

# $stderr.puts " attr is: %p" % [attributes]
if attributes.empty?
return ''
if attrs.empty?
''
else
return "\e[%sm" % attributes
"\e[%sm" % attrs
end
end


### Colorize the given +string+ with the specified +attributes+ and return it, handling
### line-endings, color reset, etc.
def colorize( *args )
string = ''

if block_given?
string = yield
else
string = args.shift
end

def colorize(attribute, string)
ending = string[/(\s)$/] || ''
string = string.rstrip

return ansi_code( args.flatten ) + string + ansi_code( 'reset' ) + ending
return ansi_code(attribute) + string + ansi_code('reset') + ending
end


### Output a message with highlighting.
def message( *msg )
$stderr.puts( colorize(:bold) { msg.flatten.join(' ') } )
def message(msg)
$stderr.puts(colorize('bold', msg))
end


### Output a logging message if $VERBOSE is true
def trace( *msg )
def trace(msg)
return unless $VERBOSE
output = colorize( msg.flatten.join(' '), 'yellow' )
$stderr.puts( output )
$stderr.puts(colorize('yellow', msg))
end


### Return the specified args as a string, quoting any that have a space.
def quotelist( *args )
return args.flatten.collect {|part| part.to_s =~ /\s/ ? part.to_s.inspect : part.to_s }
args.collect {|part| part.to_s =~ /\s/ ? part.to_s.inspect : part.to_s }.join(' ')
end


### Run the specified command +cmd+ with system(), failing if the execution
### fails.
def run( *cmd )
cmd.flatten!

if cmd.length > 1
trace( quotelist(*cmd) )
else
trace( cmd )
end
trace(cmd.length == 1 ? cmd.first : quotelist(*cmd))

system( *cmd )
raise "Command failed: [%s]" % [cmd.join(' ')] unless $?.success?
end


### Run the specified command +cmd+ after redirecting stdout and stderr to the specified
### +logpath+, failing if the execution fails.
def log_and_run( logpath, *cmd )
cmd.flatten!

if cmd.length > 1
trace( quotelist(*cmd) )
else
trace( cmd )
end
trace(cmd.length == 1 ? cmd.first : quotelist(*cmd))

# Eliminate the noise of creating/tearing down the database by
# redirecting STDERR/STDOUT to a logfile
Expand Down Expand Up @@ -419,11 +386,9 @@ def create_ca_cert(name, ca_key, x509_name, valid_years: 10)
def create_key(name, rsa_size: 2048)
ca_key = OpenSSL::PKey::RSA.new rsa_size

#cipher = OpenSSL::Cipher.new 'AES-128-CBC'

File.open "#{output_dir}/#{name}", 'w', 0600 do |io|
io.puts ca_key.to_text
io.write ca_key.export # (cipher)
io.write ca_key.export
end
ca_key
end
Expand Down Expand Up @@ -504,15 +469,13 @@ def check_for_lingering_connections( conn )
end
end


# Retrieve the names of the column types of a given result set.
def result_typenames(res)
@conn.exec_params( "SELECT " + res.nfields.times.map{|i| "format_type($#{i*2+1},$#{i*2+2})"}.join(","),
res.nfields.times.flat_map{|i| [res.ftype(i), res.fmod(i)] } ).
values[0]
end


# A matcher for checking the status of a PG::Connection to ensure it's still
# usable.
class ConnStillUsableMatcher
Expand Down Expand Up @@ -549,10 +512,8 @@ def failure_message
def failure_message_when_negated
"expected %p not to be usable, but it still is" % [ @conn ]
end

end


### Return a ConnStillUsableMatcher to be used like:
###
### expect( pg_conn ).to still_be_usable
Expand Down Expand Up @@ -700,7 +661,6 @@ def set_etc_hosts(hostaddr, hostname)
end
end


RSpec.configure do |config|
config.include( PG::TestingHelpers )

Expand Down Expand Up @@ -746,7 +706,6 @@ def set_etc_hosts(hostaddr, hostname)
end
end


# Do not wait for threads doing blocking calls at the process shutdown.
# Instead exit immediately after printing the rspec report, if we know there are pending IO calls, which do not react on ruby interrupts.
END{
Expand Down
Loading