Skip to content

Commit 6634a67

Browse files
authored
Merge pull request #687 from jeltz/spec-clean
Clean up spec helpers, especially the `Loggable` module
2 parents ca0476b + 4ff3518 commit 6634a67

File tree

1 file changed

+14
-55
lines changed

1 file changed

+14
-55
lines changed

spec/helpers.rb

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def self::included( mod )
9393

9494
end
9595

96-
9796
#
9897
# Examples
9998
#
@@ -121,93 +120,61 @@ def self::included( mod )
121120
'white' => 37, 'on_white' => 47
122121
}
123122

124-
125123
###############
126124
module_function
127125
###############
128126

129127
module Loggable
130128
### Create a string that contains the ANSI codes specified and return it
131129
def ansi_code( *attributes )
132-
attributes.flatten!
133-
attributes.collect! {|at| at.to_s }
134-
135130
return '' unless /(?:vt10[03]|xterm(?:-color)?|linux|screen)/i =~ ENV['TERM']
136-
attributes = ANSI_ATTRIBUTES.values_at( *attributes ).compact.join(';')
131+
attrs = ANSI_ATTRIBUTES.values_at(*attributes).compact.join(';')
137132

138-
# $stderr.puts " attr is: %p" % [attributes]
139-
if attributes.empty?
140-
return ''
133+
if attrs.empty?
134+
''
141135
else
142-
return "\e[%sm" % attributes
136+
"\e[%sm" % attrs
143137
end
144138
end
145139

146-
147140
### Colorize the given +string+ with the specified +attributes+ and return it, handling
148141
### line-endings, color reset, etc.
149-
def colorize( *args )
150-
string = ''
151-
152-
if block_given?
153-
string = yield
154-
else
155-
string = args.shift
156-
end
157-
142+
def colorize(attribute, string)
158143
ending = string[/(\s)$/] || ''
159144
string = string.rstrip
160145

161-
return ansi_code( args.flatten ) + string + ansi_code( 'reset' ) + ending
146+
return ansi_code(attribute) + string + ansi_code('reset') + ending
162147
end
163148

164-
165149
### Output a message with highlighting.
166-
def message( *msg )
167-
$stderr.puts( colorize(:bold) { msg.flatten.join(' ') } )
150+
def message(msg)
151+
$stderr.puts(colorize('bold', msg))
168152
end
169153

170-
171154
### Output a logging message if $VERBOSE is true
172-
def trace( *msg )
155+
def trace(msg)
173156
return unless $VERBOSE
174-
output = colorize( msg.flatten.join(' '), 'yellow' )
175-
$stderr.puts( output )
157+
$stderr.puts(colorize('yellow', msg))
176158
end
177159

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

184-
185165
### Run the specified command +cmd+ with system(), failing if the execution
186166
### fails.
187167
def run( *cmd )
188-
cmd.flatten!
189-
190-
if cmd.length > 1
191-
trace( quotelist(*cmd) )
192-
else
193-
trace( cmd )
194-
end
168+
trace(cmd.length == 1 ? cmd.first : quotelist(*cmd))
195169

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

200-
201174
### Run the specified command +cmd+ after redirecting stdout and stderr to the specified
202175
### +logpath+, failing if the execution fails.
203176
def log_and_run( logpath, *cmd )
204-
cmd.flatten!
205-
206-
if cmd.length > 1
207-
trace( quotelist(*cmd) )
208-
else
209-
trace( cmd )
210-
end
177+
trace(cmd.length == 1 ? cmd.first : quotelist(*cmd))
211178

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

422-
#cipher = OpenSSL::Cipher.new 'AES-128-CBC'
423-
424389
File.open "#{output_dir}/#{name}", 'w', 0600 do |io|
425390
io.puts ca_key.to_text
426-
io.write ca_key.export # (cipher)
391+
io.write ca_key.export
427392
end
428393
ca_key
429394
end
@@ -504,15 +469,13 @@ def check_for_lingering_connections( conn )
504469
end
505470
end
506471

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

515-
516479
# A matcher for checking the status of a PG::Connection to ensure it's still
517480
# usable.
518481
class ConnStillUsableMatcher
@@ -549,10 +512,8 @@ def failure_message
549512
def failure_message_when_negated
550513
"expected %p not to be usable, but it still is" % [ @conn ]
551514
end
552-
553515
end
554516

555-
556517
### Return a ConnStillUsableMatcher to be used like:
557518
###
558519
### expect( pg_conn ).to still_be_usable
@@ -700,7 +661,6 @@ def set_etc_hosts(hostaddr, hostname)
700661
end
701662
end
702663

703-
704664
RSpec.configure do |config|
705665
config.include( PG::TestingHelpers )
706666

@@ -746,7 +706,6 @@ def set_etc_hosts(hostaddr, hostname)
746706
end
747707
end
748708

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

0 commit comments

Comments
 (0)