module Spectator::Specs
Public Instance Methods
Checks if the commands `bundle exec rspec` and `rspec` actually run the same program, and sets the `@bundle` instance variable accordingly.
This is meant to speed up the execution of `rspec`.
# File lib/spectator/emacs.rb, line 233 def check_if_bundle_needed if `bundle exec #{rspec_command} -v` == `#{rspec_command} -v` @bundle = "" else @bundle = "bundle exec " end end
Returns a hash that summarizes the rspec results.
@param [String] output the rspec output @return [Hash] a hash table with keys “`:examples, :failures, :pending, :summary, :status“`.
* **:examples** => number of examples ran * **:failures** => number of failed examples * **:pending** => number of pending examples ran * **:summary** => the summary string from which the above have been extracted * **:status** => one of ```:failure, :pending, :success```
# File lib/spectator/emacs.rb, line 168 def extract_rspec_stats(output) summary_regex = /^(\d*)\sexamples?,\s(\d*)\s(errors?|failures?)[^\d]*((\d*)\spending)?/ summary_line = output.split("\n").detect { |line| line.match(summary_regex) } matchdata = summary_line.match(summary_regex) raise SummaryExtractionError.new if matchdata.nil? _, examples, failures, _, pending = matchdata.to_a stats = {:examples => examples.to_i, :failures => failures.to_i, :pending => pending.to_i, :summary => summary_line} stats.merge(:status => rspec_status(stats)) end
Returns a hash that summarizes the rspec results.
Redefine this method if you are using a non standard rspec formatter, see the {file:README.md} for details. @param [String] output the rspec output @return [Hash] a hash table with keys “`:examples, :failures, :pending, :summary, :status“`.
* **`:examples`**: number of examples ran * **`:failures`**: number of failed examples * **`:pending`**: number of pending examples ran * **`:summary`**: the summary string from which the above have been extracted * **`:status`**: one of ``` :success, :pending, :failure ```
# File lib/spectator/emacs.rb, line 194 def extract_rspec_summary(output) extract_rspec_stats output end
Runs the `rspec` command with the given options, and notifies Emacs
of the results.
@param [String] options The command line arguments to pass to rspec.
# File lib/spectator/emacs.rb, line 269 def rspec(options) unless options.empty? results = run("#{@bundle}#{rspec_command} --failure-exit-code 99 #{options}") status = results[:status].exitstatus if status == 1 puts "An error occurred when running the tests".red puts "RSpec output:" puts "STDERR:" puts results[:stderr] puts "-" * 80 puts "STDOUT:" puts results[:stdout] puts "sending error message" send_error(results[:stdout], results[:stderr]) else begin stats = extract_rspec_summary results[:stdout] puts(stats[:summary].send(results[:status] == 0 ? :green : :red)) # enotify_notify results[:stdout], stats rspec_send_results results[:stdout], stats rescue StandardError => e puts "ERROR extracting summary from rspec output: #{e}".red puts e.backtrace puts "RSpec output:" puts "STDERR:" puts results[:stderr] puts "-" * 80 puts "STDOUT:" puts results[:stdout] print "Exit? (y/N)" answer = STDIN.gets abort "Execution aborted by the user" if answer.strip.downcase == 'y' end end end end
Sends a notification to emacs via Enotify
@param [String] rspec_output The rspec command output @param [Hash] stats A Hash
table with keys “`:examples, :failures, :pending, :summary, :status“`.
See {#extract_rspec_summary} for details about the meaning of the key/value pairs in this table.
# File lib/spectator/emacs.rb, line 215 def rspec_send_results(rspec_output, stats) begin print "--- Sending notification to #{@enotify_host}:#{@enotify_port}" \ " through #{@enotify_slot_id}... ".cyan enotify_notify rspec_output, stats puts "Success!".green rescue SocketError puts "Failed!".red enotify_connect rspec_send_results rspec_output, stats end end
Summarizes the rspec results as one of `:failure, :pending, :success`.
@param [Hash] rspec_stats A Hash
table with keys “`:examples, :failures, :pending, :summary, :status“`.
See {#extract_rspec_summary} for details about the meaning of the key/value pairs in this table.
# File lib/spectator/emacs.rb, line 148 def rspec_status(rspec_stats) if rspec_stats[:failures] > 0 :failure elsif rspec_stats[:pending] > 0 :pending else :success end end
Runs a command and returns a hash containing exit status, standard output and standard error contents.
@return [Hash] a hash table with keys `:status, :stdout, :stderr`.
# File lib/spectator/emacs.rb, line 202 def run(cmd) puts "=== running: #{cmd} ".ljust(terminal_columns, '=').cyan pid, _, stdout, stderr = Open4::popen4 cmd _, status = Process::waitpid2 pid puts "===".ljust(terminal_columns, '=').cyan {:status => status, :stdout => stdout.read.strip, :stderr => stderr.read.strip} end
# File lib/spectator/emacs.rb, line 241 def send_error(stdout, stderr) report_header = """################################## ERROR REPORT ################################## An error happened during the test run. Check your spec files. >-------------------------------- Error Message --------------------------------< """ message = { :id => @enotify_slot_id, :notification => { :text => @notification_messages[:error], :face => @notification_face[:error], :help => "", # format_tooltip(stats), :mouse_1 => "tdd" }, :data => { :mode => "org", :report_text => report_header + stdout + stderr } } enotify_send message end