Module: Guard::Jasmine::Runner
- Extended by:
- Util
- Defined in:
- lib/guard/jasmine/runner.rb
Overview
The Jasmine runner handles the execution of the spec through the PhantomJS binary,
evaluates the JSON response from the PhantomJS Script guard_jasmine.coffee,
writes the result to the console and triggers optional system notifications.
Constant Summary
- THRESHOLDS =
Name of the coverage threshold options
[:statements_threshold, :functions_threshold, :branches_threshold, :lines_threshold]
Class Method Summary (collapse)
-
+ (Boolean) any_coverage_threshold?(options)
private
Do we should check the coverage?.
-
+ (Object) check_coverage(options)
private
Uses the Istanbul text reported to output the result of the last coverage run.
-
+ (Array<Hash>) collect_specs(suites)
private
Get all specs from the suites and its nested suites.
-
+ (Boolean) console_for_spec?(spec, options = { })
private
Are console logs shown for this spec?.
-
+ (Boolean) console_logs_shown?(suite, passed, options = { })
private
Are console logs shown for this suite?.
-
+ (Boolean) contains_failed_spec?(suite)
private
Tests if the given suite has a failing spec underneath.
-
+ (String) coverage_file
private
Get the coverage file to save all coverage data.
-
+ (String) coverage_root
private
Create and returns the coverage root directory.
-
+ (Boolean) description_shown?(passed, spec, options = { })
private
Is the description shown for this spec?.
-
+ (Boolean) error_logs_shown?(suite, passed, options = { })
private
Are error logs shown for this suite?.
-
+ (Boolean) errors_for_spec?(spec, options = { })
private
Are errors shown for this spec?.
-
+ (Hash) evaluate_response(output, file, options)
private
Evaluates the JSON response that the PhantomJS script writes to stdout.
-
+ (Array<String>) failed_paths_from(results)
private
Returns the failed spec file names.
-
+ (String) format_message(message, short)
private
Formats a message.
-
+ (Object) generate_html_report
private
Uses the Istanbul text reported to output the result of the last coverage run.
-
+ (Object) generate_summary_report
private
Uses the Istanbul text-summary reporter to output the summary of all the coverage runs combined.
-
+ (Object) generate_text_report(file, options)
private
Uses the Istanbul text reported to output the result of the last coverage run.
-
+ (Object) indent(message, level)
private
Indent a message.
-
+ (String) istanbul_coverage_options(options)
private
Converts the options to Istanbul recognized options.
-
+ (String) jasmine_suite(file, options)
private
Get the Jasmine test runner URL with the appended suite name that acts as the spec filter.
-
+ (Object) notify_coverage_result(coverage, file, options)
private
Notification about the coverage of a spec run, success or failure, and some stats.
-
+ (Object) notify_errors(result, options)
private
Show system notifications about the occurred errors.
-
+ (Object) notify_runtime_error(result, options)
private
Notification when a system error happens that prohibits the execution of the Jasmine spec.
-
+ (Object) notify_spec_result(result, options)
private
Notification about a spec run, success or failure, and some stats.
-
+ (Object) notify_start_message(paths, options)
private
Shows a notification in the console that the runner starts.
-
+ (String) phantomjs_command(options)
private
Get the PhantomJS binary and script to execute.
-
+ (String) phantomjs_script
private
Get the PhantomJS script that executes the spec and extracts the result from the headless DOM.
-
+ (String) query_string_for_suite(file, options)
private
The suite name must be extracted from the spec that will be run.
-
+ (Object) report_specdoc(result, passed, options)
private
Specdoc like formatting of the result.
-
+ (Object) report_specdoc_errors(spec, options, level)
private
Shows the errors for a given spec.
-
+ (Object) report_specdoc_logs(spec, options, level)
private
Shows the logs for a given spec.
-
+ (Object) report_specdoc_suite(suite, passed, options, level = 0)
private
Show the suite result.
-
+ (Boolean) response_status_for(results)
private
Returns the response status for the given result set.
-
+ (Boolean, Array<String>) run(paths, options = { })
Run the supplied specs.
-
+ (Object) run_jasmine_spec(file, options)
private
Run the Jasmine spec by executing the PhantomJS script.
-
+ (Boolean) specdoc_shown?(passed, options = { })
private
Is the specdoc shown for this suite?.
-
+ (Object) update_coverage(coverage, file, options)
private
Updates the coverage data with new data for the implementation file.
Methods included from Util
find_free_server_port, phantomjs_bin_valid?, runner_available?, which
Class Method Details
+ (Boolean) any_coverage_threshold?(options) (private)
Do we should check the coverage?
599 600 601 |
# File 'lib/guard/jasmine/runner.rb', line 599 def any_coverage_threshold?() THRESHOLDS.any? { |threshold| [threshold] != 0 } end |
+ (Object) check_coverage(options) (private)
Uses the Istanbul text reported to output the result of the last coverage run.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/guard/jasmine/runner.rb', line 302 def check_coverage() if any_coverage_threshold?() coverage = `#{ which('istanbul') } check-coverage #{ () } #{ coverage_file } 2>&1` coverage = coverage.split("\n").grep(/ERROR/).join.sub('ERROR:', '') failed = $? && $?.exitstatus != 0 if failed Formatter.error coverage Formatter.notify(coverage, :title => 'Code coverage failed', :image => :failed, :priority => 2) if [:notification] else Formatter.success 'Code coverage succeed' Formatter.notify('All code is adequately covered with specs', :title => 'Code coverage succeed') if [:notification] && ![:hide_success] end end end |
+ (Array<Hash>) collect_specs(suites) (private)
Get all specs from the suites and its nested suites.
547 548 549 550 551 552 553 |
# File 'lib/guard/jasmine/runner.rb', line 547 def collect_specs(suites) suites.inject([]) do |specs, suite| specs = (specs | suite['specs']) if suite['specs'] specs = (specs | collect_specs(suite['suites'])) if suite['suites'] specs end end |
+ (Boolean) console_for_spec?(spec, options = { }) (private)
Are console logs shown for this spec?
423 424 425 426 |
# File 'lib/guard/jasmine/runner.rb', line 423 def console_for_spec?(spec, = { }) spec['logs'] && ((spec['passed'] && [:console] == :always) || (!spec['passed'] && [:console] != :never)) end |
+ (Boolean) console_logs_shown?(suite, passed, options = { }) (private)
Are console logs shown for this suite?
404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/guard/jasmine/runner.rb', line 404 def console_logs_shown?(suite, passed, = { }) # Are console messages displayed? console_enabled = [:console] == :always || ([:console] == :failure && !passed) # Are there any logs to display at all for this suite? = suite['specs'].select do |spec| spec['logs'] && ([:console] == :always || ([:console] == :failure && !spec['passed'])) end any_logs_present = !.empty? console_enabled && any_logs_present end |
+ (Boolean) contains_failed_spec?(suite) (private)
Tests if the given suite has a failing spec underneath.
538 539 540 |
# File 'lib/guard/jasmine/runner.rb', line 538 def contains_failed_spec?(suite) collect_specs([suite]).any? { |spec| !spec['passed'] } end |
+ (String) coverage_file (private)
Get the coverage file to save all coverage data.
Creates tmp/coverage if not exists.
620 621 622 |
# File 'lib/guard/jasmine/runner.rb', line 620 def coverage_file File.join(coverage_root, 'coverage.json') end |
+ (String) coverage_root (private)
Create and returns the coverage root directory.
628 629 630 |
# File 'lib/guard/jasmine/runner.rb', line 628 def coverage_root File.(File.join('tmp', 'coverage')) end |
+ (Boolean) description_shown?(passed, spec, options = { }) (private)
Is the description shown for this spec?
463 464 465 |
# File 'lib/guard/jasmine/runner.rb', line 463 def description_shown?(passed, spec, = { }) specdoc_shown?(passed, ) || console_for_spec?(spec, ) || errors_for_spec?(spec, ) end |
+ (Boolean) error_logs_shown?(suite, passed, options = { }) (private)
Are error logs shown for this suite?
434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/guard/jasmine/runner.rb', line 434 def error_logs_shown?(suite, passed, = { }) # Are error messages displayed? errors_enabled = [:errors] == :always || ([:errors] == :failure && !passed) # Are there any errors to display at all for this suite? = suite['specs'].select do |spec| spec['errors'] && ([:errors] == :always || ([:errors] == :failure && !spec['passed'])) end any_errors_present= !.empty? errors_enabled && any_errors_present end |
+ (Boolean) errors_for_spec?(spec, options = { }) (private)
Are errors shown for this spec?
452 453 454 455 |
# File 'lib/guard/jasmine/runner.rb', line 452 def errors_for_spec?(spec, = { }) spec['errors'] && ((spec['passed'] && [:errors] == :always) || (!spec['passed'] && [:errors] != :never)) end |
+ (Hash) evaluate_response(output, file, options) (private)
Evaluates the JSON response that the PhantomJS script writes to stdout. The results triggers further notification actions.
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 193 194 195 |
# File 'lib/guard/jasmine/runner.rb', line 165 def evaluate_response(output, file, ) json = output.read json = json.encode('UTF-8') if json.respond_to?(:encode) begin result = MultiJson.decode(json, { :max_nesting => false }) if result['error'] notify_runtime_error(result, ) else result['file'] = file notify_spec_result(result, ) end if result['coverage'] && [:coverage] notify_coverage_result(result['coverage'], file, ) end result rescue MultiJson::DecodeError => e if e.data == '' Formatter.error('No response from the Jasmine runner!') else Formatter.error("Cannot decode JSON from PhantomJS runner: #{ e. }") Formatter.error("JSON response: #{ e.data }") end ensure output.close end end |
+ (Array<String>) failed_paths_from(results) (private)
Returns the failed spec file names.
75 76 77 |
# File 'lib/guard/jasmine/runner.rb', line 75 def failed_paths_from(results) results.map { |r| !r['passed'] ? r['file'] : nil }.compact end |
+ (String) format_message(message, short) (private)
Formats a message.
561 562 563 564 565 566 567 |
# File 'lib/guard/jasmine/runner.rb', line 561 def (, short) if =~ /(.*?) in http.+?assets\/(.*)\?body=\d+\s\((line\s\d+)/ short ? $1 : "#{ $1 } in #{ $2 } on #{ $3 }" else end end |
+ (Object) generate_html_report (private)
Uses the Istanbul text reported to output the result of the last coverage run.
321 322 323 324 |
# File 'lib/guard/jasmine/runner.rb', line 321 def generate_html_report `#{ which('istanbul') } report --root #{ coverage_root } html #{ coverage_file }` Formatter.info "Updated HTML report available at: #{ File.join('coverage', 'index.html') }" end |
+ (Object) generate_summary_report (private)
Uses the Istanbul text-summary reporter to output the summary of all the coverage runs combined.
329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/guard/jasmine/runner.rb', line 329 def generate_summary_report Formatter.info 'Spec coverage summary:' puts '' `#{ which('istanbul') } report --root #{ coverage_root } text-summary #{ coverage_file }`.each_line do |line| puts line.sub(/\n$/, '') if line =~ /\)$/ end puts '' end |
+ (Object) generate_text_report(file, options) (private)
Uses the Istanbul text reported to output the result of the last coverage run.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/guard/jasmine/runner.rb', line 278 def generate_text_report(file, ) Formatter.info 'Spec coverage details:' if file == [:spec_dir] matcher = /[|+]$/ else impl = file.sub('_spec', '').sub([:spec_dir], '') matcher = /(-+|All files|% Lines|#{ Regexp.escape(File.basename(impl)) }|#{ File.dirname(impl).sub(/^\//, '') }\/[^\/])/ end puts '' `#{ which('istanbul') } report --root #{ coverage_root } text #{ coverage_file }`.each_line do |line| puts line.sub(/\n$/, '') if line =~ matcher end puts '' end |
+ (Object) indent(message, level) (private)
Indent a message.
510 511 512 |
# File 'lib/guard/jasmine/runner.rb', line 510 def indent(, level) (' ' * level) + end |
+ (String) istanbul_coverage_options(options) (private)
Converts the options to Istanbul recognized options
608 609 610 611 612 613 |
# File 'lib/guard/jasmine/runner.rb', line 608 def () THRESHOLDS.inject([]) do |coverage, name| threshold = [name] coverage << (threshold != 0 ? "--#{ name.to_s.sub('_threshold', '') } #{ threshold }" : '') end.reject(&:empty?).join(' ') end |
+ (String) jasmine_suite(file, options) (private)
Get the Jasmine test runner URL with the appended suite name that acts as the spec filter.
118 119 120 |
# File 'lib/guard/jasmine/runner.rb', line 118 def jasmine_suite(file, ) [:jasmine_url] + query_string_for_suite(file, ) end |
+ (Object) notify_coverage_result(coverage, file, options) (private)
Notification about the coverage of a spec run, success or failure, and some stats.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/guard/jasmine/runner.rb', line 254 def notify_coverage_result(coverage, file, ) FileUtils.mkdir_p(coverage_root) unless File.exist?(coverage_root) update_coverage(coverage, file, ) if [:coverage_summary] generate_summary_report else generate_text_report(file, ) end check_coverage() if [:coverage_html] generate_html_report end end |
+ (Object) notify_errors(result, options) (private)
Show system notifications about the occurred errors.
521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/guard/jasmine/runner.rb', line 521 def notify_errors(result, ) collect_specs(result['suites']).each_with_index do |spec, index| if !spec['passed'] && [:max_error_notify] > index msg = spec['messages'].map { || (, true) }.join(', ') Formatter.notify("#{ spec['description'] }: #{ msg }", :title => 'Jasmine spec failed', :image => :failed, :priority => 2) if [:notification] end end end |
+ (Object) notify_runtime_error(result, options) (private)
Notification when a system error happens that prohibits the execution of the Jasmine spec.
204 205 206 207 208 |
# File 'lib/guard/jasmine/runner.rb', line 204 def notify_runtime_error(result, ) = "An error occurred: #{ result['error'] }" Formatter.error() Formatter.notify(, :title => 'Jasmine error', :image => :failed, :priority => 2) if [:notification] end |
+ (Object) notify_spec_result(result, options) (private)
Notification about a spec run, success or failure, and some stats.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/guard/jasmine/runner.rb', line 218 def notify_spec_result(result, ) specs = result['stats']['specs'] failures = result['stats']['failures'] time = result['stats']['time'] specs_plural = specs == 1 ? '' : 's' failures_plural = failures == 1 ? '' : 's' Formatter.info("\nFinished in #{ time } seconds") = "#{ specs } spec#{ specs_plural }, #{ failures } failure#{ failures_plural }" = "#{ }\nin #{ time } seconds" passed = failures == 0 if passed report_specdoc(result, passed, ) Formatter.success() Formatter.notify(, :title => 'Jasmine suite passed') if [:notification] && ![:hide_success] else report_specdoc(result, passed, ) Formatter.error() notify_errors(result, ) Formatter.notify(, :title => 'Jasmine suite failed', :image => :failed, :priority => 2) if [:notification] end Formatter.info("Done.\n") end |
+ (Object) notify_start_message(paths, options) (private)
Shows a notification in the console that the runner starts.
60 61 62 63 64 65 66 67 68 |
# File 'lib/guard/jasmine/runner.rb', line 60 def (paths, ) = if paths == [[:spec_dir]] 'Run all Jasmine suites' else "Run Jasmine suite#{ paths.size == 1 ? '' : 's' } #{ paths.join(' ') }" end Formatter.info(, :reset => true) end |
+ (String) phantomjs_command(options) (private)
Get the PhantomJS binary and script to execute.
106 107 108 |
# File 'lib/guard/jasmine/runner.rb', line 106 def phantomjs_command() [:phantomjs_bin] + ' ' + phantomjs_script end |
+ (String) phantomjs_script (private)
Get the PhantomJS script that executes the spec and extracts the result from the headless DOM.
127 128 129 |
# File 'lib/guard/jasmine/runner.rb', line 127 def phantomjs_script File.(File.join(File.dirname(__FILE__), 'phantomjs', 'guard-jasmine.js')) end |
+ (String) query_string_for_suite(file, options) (private)
The suite name must be extracted from the spec that
will be run. This is done by parsing from the head of
the spec file until the first describe function is
found.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/guard/jasmine/runner.rb', line 141 def query_string_for_suite(file, ) return '' if file == [:spec_dir] query_string = '' File.foreach(file) do |line| if line =~ /describe\s*[("']+(.*?)["')]+/ query_string = "?spec=#{ $1 }" break end end URI.encode(query_string) end |
+ (Object) report_specdoc(result, passed, options) (private)
Specdoc like formatting of the result.
348 349 350 351 352 |
# File 'lib/guard/jasmine/runner.rb', line 348 def report_specdoc(result, passed, ) result['suites'].each do |suite| report_specdoc_suite(suite, passed, ) end end |
+ (Object) report_specdoc_errors(spec, options, level) (private)
Shows the errors for a given spec.
491 492 493 494 495 496 497 498 499 500 501 502 503 |
# File 'lib/guard/jasmine/runner.rb', line 491 def report_specdoc_errors(spec, , level) if spec['errors'] && ([:errors] == :always || ([:errors] == :failure && !spec['passed'])) spec['errors'].each do |error| if error['trace'] error['trace'].each do |trace| Formatter.spec_failed(indent(" ➜ Exception: #{ error['msg'] } in #{ trace['file'] } on line #{ trace['line'] }", level)) end else Formatter.spec_failed(indent(" ➜ Exception: #{ error['msg'] }", level)) end end end end |
+ (Object) report_specdoc_logs(spec, options, level) (private)
Shows the logs for a given spec.
474 475 476 477 478 479 480 481 482 |
# File 'lib/guard/jasmine/runner.rb', line 474 def report_specdoc_logs(spec, , level) if spec['logs'] && ([:console] == :always || ([:console] == :failure && !spec['passed'])) spec['logs'].each do |log| log.split("\n").each_with_index do |, index| Formatter.info(indent(" #{ index == 0 ? '•' : ' ' } #{ }", level)) end end end end |
+ (Object) report_specdoc_suite(suite, passed, options, level = 0) (private)
Show the suite result.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/guard/jasmine/runner.rb', line 363 def report_specdoc_suite(suite, passed, , level = 0) # Print the suite description when the specdoc is shown or there are logs to display if specdoc_shown?(passed, ) || console_logs_shown?(suite, passed, ) || error_logs_shown?(suite, passed, ) Formatter.suite_name((' ' * level) + suite['description']) if passed || [:focus] && contains_failed_spec?(suite) end suite['specs'].each do |spec| if spec['passed'] if passed || ![:focus] || console_for_spec?(spec, ) || errors_for_spec?(spec, ) Formatter.success(indent(" ✔ #{ spec['description'] }", level)) if description_shown?(passed, spec, ) report_specdoc_errors(spec, , level) report_specdoc_logs(spec, , level) end else Formatter.spec_failed(indent(" ✘ #{ spec['description'] }", level)) if description_shown?(passed, spec, ) spec['messages'].each do || Formatter.spec_failed(indent(" ➤ #{ (, false) }", level)) if specdoc_shown?(passed, ) end report_specdoc_errors(spec, , level) report_specdoc_logs(spec, , level) end end suite['suites'].each { |s| report_specdoc_suite(s, passed, , level + 2) } if suite['suites'] end |
+ (Boolean) response_status_for(results) (private)
Returns the response status for the given result set.
84 85 86 |
# File 'lib/guard/jasmine/runner.rb', line 84 def response_status_for(results) results.none? { |r| r.has_key?('error') || !r['passed'] } end |
+ (Boolean, Array<String>) run(paths, options = { })
Run the supplied specs.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/guard/jasmine/runner.rb', line 38 def run(paths, = { }) return [false, []] if paths.empty? (paths, ) results = paths.inject([]) do |results, file| results << evaluate_response(run_jasmine_spec(file, ), file, ) if File.exist?(file) results end.compact [response_status_for(results), failed_paths_from(results)] end |
+ (Object) run_jasmine_spec(file, options) (private)
Run the Jasmine spec by executing the PhantomJS script.
94 95 96 97 98 |
# File 'lib/guard/jasmine/runner.rb', line 94 def run_jasmine_spec(file, ) suite = jasmine_suite(file, ) Formatter.info("Run Jasmine suite at #{ suite }") IO.popen("#{ phantomjs_command() } \"#{ suite }\" #{ [:timeout] * 1000 } #{ [:specdoc] } #{ [:focus] } #{ [:console] } #{ [:errors] }", 'r:UTF-8') end |
+ (Boolean) specdoc_shown?(passed, options = { }) (private)
Is the specdoc shown for this suite?
394 395 396 |
# File 'lib/guard/jasmine/runner.rb', line 394 def specdoc_shown?(passed, = { }) [:specdoc] == :always || ([:specdoc] == :failure && !passed) end |
+ (Object) update_coverage(coverage, file, options) (private)
Updates the coverage data with new data for the implementation file. It replaces the coverage data if the file is the spec dir.
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/guard/jasmine/runner.rb', line 576 def update_coverage(coverage, file, ) if file == [:spec_dir] File.write(coverage_file, MultiJson.encode(coverage, { :max_nesting => false })) else if File.exist?(coverage_file) impl = file.sub('_spec', '').sub([:spec_dir], '') coverage = MultiJson.decode(File.read(coverage_file), { :max_nesting => false }) coverage.each do |coverage_file, data| coverage[coverage_file] = data if coverage_file == impl end File.write(coverage_file, MultiJson.encode(coverage, { :max_nesting => false })) else File.write(coverage_file, MultiJson.encode({ })) end end end |