Class: OCRunner::TestRunner

Inherits:
Object
  • Object
show all
Includes:
Console
Defined in:
lib/ocrunner/test_runner.rb

Defined Under Namespace

Classes: BuildFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Console

#blue, #colorize, #green, #indent, #present, #red

Constructor Details

#initialize(options) ⇒ TestRunner

Returns a new instance of TestRunner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ocrunner/test_runner.rb', line 9

def initialize(options)
  @suites = []
  @log = ''
  @current_directory = Dir.pwd
  @options = options
  @passed = true
  @compilation_error_occurred = false
  @output = []
  
  build_command
  setup
  run_tests
  summarize
  display_results
end

Instance Attribute Details

#compilation_error_occurredObject (readonly)

Returns the value of attribute compilation_error_occurred.



7
8
9
# File 'lib/ocrunner/test_runner.rb', line 7

def compilation_error_occurred
  @compilation_error_occurred
end

Instance Method Details

#build_commandObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/ocrunner/test_runner.rb', line 30

def build_command
  @command = "xcodebuild -target #{@options[:target]} -configuration #{@options[:config]} " +
             "-sdk #{@options[:sdk]} #{@options[:parallel] ? '-parallelizeTargets' : ''} build"
 if @options[:debug_command]
   present do
     puts indent @command
   end
   exit
 end
end

#build_error(message) ⇒ Object



84
85
86
87
# File 'lib/ocrunner/test_runner.rb', line 84

def build_error(message)
  out red(message)
  @passed = false
end

#build_failedObject



89
90
91
92
# File 'lib/ocrunner/test_runner.rb', line 89

def build_failed
  growl('BUILD FAILED!')
  out red('*** BUILD FAILED ***')
end

#build_succeededObject



94
95
96
97
# File 'lib/ocrunner/test_runner.rb', line 94

def build_succeeded
  growl('Build succeeded.')
  out green('*** BUILD SUCCEEDED ***')
end

#clean_path(path) ⇒ Object



208
209
210
211
# File 'lib/ocrunner/test_runner.rb', line 208

def clean_path(path)
  return 'unknown' if path.nil?
  path.gsub(@current_directory + '/', '')
end

#compilation_error_occurred!Object



194
195
196
# File 'lib/ocrunner/test_runner.rb', line 194

def compilation_error_occurred!
  @compilation_error_occurred = true
end

#display_resultsObject



78
79
80
81
82
# File 'lib/ocrunner/test_runner.rb', line 78

def display_results
  puts @log if @options[:verbose] || (compilation_error_occurred && @options[:loud_compilation])
  puts @output.join("\n")
  puts
end

#execute(cmd, &block) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/ocrunner/test_runner.rb', line 223

def execute(cmd, &block)
  IO.popen("#{cmd} 2>&1") do |f| 
    while line = f.gets do 
      yield line
    end
  end
end

#growl(message) ⇒ Object



213
214
215
216
217
218
219
220
221
# File 'lib/ocrunner/test_runner.rb', line 213

def growl(message)
  if @options[:growl]
    execute "growlnotify -i \"xcodeproj\" -m \"#{message}\"" do |error|
      if error =~ /command not found/
        out red('You must have growl and growl notify installed to enable growl support. See http://growl.info.')
      end
    end
  end
end

#out(line = '') ⇒ Object



204
205
206
# File 'lib/ocrunner/test_runner.rb', line 204

def out(line = '')
  @output << line.rstrip
end

#process_console_output(line) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ocrunner/test_runner.rb', line 99

def process_console_output(line)
  
  if @options[:oclog]
    if line.include?("\033\[35m")
      line =~ /[\-|\+](\[.+\]):(\d+):(.+):/
      out blue("#{$1} on line #{$2} of #{clean_path($3)}:")
      out line.slice(line.index("\033\[35m")..-1)
      @debug_output = true unless line.include?("\033[0m")
      return
    end

    if line.include?("\033[0m")
      @debug_output = false
      out line
      out
      return
    end
  
    if @debug_output
      out line
      return
    end 
  end

  # test case started
  if line =~ /Test Case '-\[.+ (.+)\]' started/
    @current_case = TestCase.new($1)
    @current_suite.cases << @current_case
  end

  # test case passed
  if line =~ /Test Case .+ passed/
    @current_case = nil
    print(green('.'))
  end
  
  # test failure
  if line =~ /(.+\.m):(\d+): error: -\[(.+) (.+)\] :(?: (.+):?)?/
    @current_case.errors << TestError.new($1, $2, $5)
    test_failure_occurred!
  end

  # start test suite
  if line =~ /Test Suite '([^\/]+)' started/
    @current_suite = TestSuite.new($1)
    @suites << @current_suite
    print "#{$1} "
  end

  # finish test suite
  if @current_suite && line =~ /^Executed/ && line =~ /\(([\d\.]+)\) seconds/
    @current_suite.time = $1
    print "\n" # clear console line
  end

  # test executable not found
  if line =~ /The executable for the test bundle at (.+\.octest) could not be found/
    build_error("Test executable #{clean_path($1)} could not be found")
  end

  # compilation errors
  if !@current_case && line =~ /(.+\.m):(\d+): error: (.*)/
    compilation_error_occurred!
    build_error($&)      
  end
  
  # compilation reference error
  if line =~ /"(.+)", referenced from:/
    compilation_error_occurred!
    build_error($&)
  end
  
  # linking error
  if line =~ /-\[\w+ \w+\] in .+\.o/
    compilation_error_occurred!
    build_error($&)
  end
  
  # bus error
  if line =~ /Bus error/
    build_error('Bus error while running tests.')        
  end      
  
  # segfault
  if line =~ /Segmentation fault/
    build_error('Segmentation fault while running tests.')        
  end
  
  # no Xcode project found
  if line =~ /does not contain an Xcode project/
    build_error('No Xcode project was found.')
  end
  
end

#run_testsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/ocrunner/test_runner.rb', line 41

def run_tests
  
  puts "ocrunner started. control-c to exit, control-\\ to toggle verbosity\n\n"
  
  execute @command do |line|
    @log << line unless line =~ /setenv/
    process_console_output(line)
    $stdout.flush
  end
end

#setupObject



25
26
27
28
# File 'lib/ocrunner/test_runner.rb', line 25

def setup
  puts "-"*80
  puts
end

#summarizeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ocrunner/test_runner.rb', line 52

def summarize
  
  @suites.each do |suite|
    suite.failed_cases.each do |kase|
      out '  ' + red("[#{suite.name} #{kase.name}] FAIL")
      kase.errors.each do |error|
        out '    ' + red(error.message) + " line #{error.line} of #{clean_path(error.path)}"
      end
    end
    out if suite.failures?
  end
  
  @suites.each do |suite|
    number = suite.failed_cases.size
    out "Suite '#{suite.name}': #{suite.cases.size - number} passes and #{number} failures in #{suite.time} seconds."
  end
  
  out
  
  if @passed
    build_succeeded
  else
    build_failed
  end
end

#test_failure_occurred!Object



198
199
200
201
202
# File 'lib/ocrunner/test_runner.rb', line 198

def test_failure_occurred!
  print red('.') if @current_case.passed? # only print a dot for the case's first failure
  @current_case.fail!
  @passed = false
end