Class: Cutest

Inherits:
Object
  • Object
show all
Defined in:
lib/cutest.rb,
lib/cutest/database/sequel_driver.rb,
lib/cutest/database/activerecord_driver.rb

Defined Under Namespace

Modules: Database Classes: AssertionFailed, Scope

Constant Summary collapse

VERSION =
"1.8.1"
FILTER =
%r[/(ruby|jruby|rbx)[-/]([0-9\.])+]
CACHE =
Hash.new { |h, k| h[k] = File.readlines(k) }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



19
20
21
# File 'lib/cutest.rb', line 19

def config
  @config
end

.reset_configObject

Returns the value of attribute reset_config.



19
20
21
# File 'lib/cutest.rb', line 19

def reset_config
  @reset_config
end

Class Method Details

.capture_outputObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cutest.rb', line 132

def capture_output
  old_stdout = STDOUT.clone
  pipe_r, pipe_w = IO.pipe
  pipe_r.sync    = true
  output         = ""
  reader = Thread.new do
    begin
      loop do
        output << pipe_r.readpartial(1024)
      end
    rescue EOFError
    end
  end
  STDOUT.reopen(pipe_w)
  yield
ensure
  STDOUT.reopen(old_stdout)
  pipe_w.close
  reader.join
  return output
end

.code(fn, ln) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/cutest.rb', line 105

def code(fn, ln)
  begin
    CACHE[fn][ln.to_i - 1].strip
  rescue
    "(Can't display line)"
  end
end

.display_errorObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cutest.rb', line 113

def display_error
  if cutest[:backtrace]
    bt = $!.backtrace
    bt.each do |line|
      display_trace line
    end
  end

  puts "  \033[93m#{$!.class}: \033[31m#{$!.message}"
  puts ""
end

.display_trace(line) ⇒ Object



125
126
127
128
129
130
# File 'lib/cutest.rb', line 125

def display_trace(line)
  fn, ln = line.split(":")

  puts "  → \033[0mfile: #{fn} ↪#{ln}\e[0m"
  puts "  → \033[90mline: #{code(fn, ln)}\e[0m"
end

.load_envs(env) ⇒ Object



33
34
35
36
37
38
# File 'lib/cutest.rb', line 33

def load_envs env
  File.foreach env do |line|
    key, value = line.split "="
    ENV[key] = value.gsub('\n', '').strip
  end
end

.now_run(files) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cutest.rb', line 57

def now_run files
  status = files.all? do |file|
    run_file(file)

    Process.wait2.last.success?
  end

  puts

  status
end

.reset_config!Object



29
30
31
# File 'lib/cutest.rb', line 29

def reset_config!
  @config = OpenStruct.new database: {}
end

.run(files) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/cutest.rb', line 47

def run(files)
  if !cutest[:warnings]
    Cutest.silence_warnings do
      Cutest.now_run files
    end
  else
    Cutest.now_run files
  end
end

.run_file(file) ⇒ Object



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
# File 'lib/cutest.rb', line 69

def run_file(file)
  fork do
    begin
      load(file)
    rescue LoadError, SyntaxError
      display_error
      exit 1

    rescue StandardError
      trace = $!.backtrace
      pivot = trace.index { |line| line.match(file) }

      puts "  \e[93mTest: \e[0m%s\e[31m✘\e[0m\n" % (cutest[:test] != '' ? "#{cutest[:test]} " : '')

      if pivot
        other = trace[0..pivot].select { |line| line !~ FILTER }
        other.reverse.each { |line| display_trace(line) }
      else
        display_trace(trace.first)
      end

      display_error

      if not cutest[:pry_rescue]
        exit 1
      else
        begin
          Process.waitall
        rescue ThreadError, Interrupt
          # Ignore this as it's caused by Process.waitall when using -p
        end
      end
    end
  end
end

.setup {|config| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/cutest.rb', line 21

def setup
  yield config
end

.silence_warningsObject



40
41
42
43
44
45
# File 'lib/cutest.rb', line 40

def silence_warnings
  old_verbose, $VERBOSE = $VERBOSE, nil
  yield
ensure
  $VERBOSE = old_verbose
end