gregwebs / quicktest

Utility for inlining unit tests with the code under test.

quicktest / tasks / helpers.rb
100644 60 lines (49 sloc) 1.106 kb
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
def exit_msg msg, code=1
  puts msg; exit code
end
 
# exit on non-zero error code
def run! *command
  res = run(*command)
  if (s = $?.exitstatus) != 0
    exit_msg(res, s)
  else
    res
  end
end
 
# this is like backticks, but the command will be shell escaped
def run *command
  res = run_shell_escaped *command
  return <<-EOS if $?.exitstatus != 0
 
exit code: #{$?.exitstatus}
command result:
#{res}
 
failure on command:
#{command.join(' ')}
EOS
 
  res
end
 
def run_shell_escaped *command
  command = command.flatten.map do |str|
    str =~/^'.*'$/ ? str : str.split(/\s+/)
  end.flatten
  res = IO.popen('-') {|io| io ? io.read : exec(command.shift, *command)}
end
 
def out *command
  (puts (run! *command))
end
 
def cd_tmp
  Dir.mkdir 'tmp' unless File.directory? 'tmp'
  Dir.chdir('tmp') do |dir|
    yield dir
  end
  rm_rf 'tmp'
end
 
class IO
  def self.write( file, str )
    self.open( file, 'w' ) { |fh| fh.print str }
  end
  def self.read_write( file, write_file=file )
    self.write(write_file, (yield( self.read( file ))))
  end
end
 
Dir.glob('tasks/*.rake').sort.each {|fn| import fn}