class Guard::RSpecFormatter

Constants

UNSUPPORTED_PATTERN

Public Class Methods

extract_spec_location(metadata) click to toggle source

rspec issue github.com/rspec/rspec-core/issues/793

# File lib/guard/rspec_formatter.rb, line 46
def extract_spec_location(metadata)
  root_metadata = metadata
  location = metadata[:location]

  until spec_path?(location)
    unless (metadata = _extract_group(metadata))
      STDERR.puts "no spec file location in #{root_metadata.inspect}"
      return root_metadata[:location]
    end

    # rspec issue https://github.com/rspec/rspec-core/issues/1243
    location = first_colon_separated_entry(metadata[:location])
  end

  location
end
rspec_3?() click to toggle source
# File lib/guard/rspec_formatter.rb, line 28
def self.rspec_3?
  ::RSpec::Core::Version::STRING.split(".").first == "3"
end
spec_path?(path) click to toggle source
# File lib/guard/rspec_formatter.rb, line 63
def spec_path?(path)
  pattern = ::RSpec.configuration.pattern

  flags = supported_fnmatch_flags(pattern)
  path ||= ""
  path = path.sub(/:\d+\z/, "")
  path = Pathname.new(path).cleanpath.to_s
  stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}"
  File.fnmatch(stripped, path, flags)
end

Private Class Methods

_extract_group(metadata) click to toggle source
# File lib/guard/rspec_formatter.rb, line 91
def _extract_group(metadata)
  metadata[:parent_example_group] || metadata[:example_group]
end
first_colon_separated_entry(entries) click to toggle source
# File lib/guard/rspec_formatter.rb, line 76
def first_colon_separated_entry(entries)
  (entries || "").split(":").first
end
supported_fnmatch_flags(pattern) click to toggle source
# File lib/guard/rspec_formatter.rb, line 80
def supported_fnmatch_flags(pattern)
  flags = File::FNM_PATHNAME | File::FNM_DOTMATCH

  # ruby >= 2
  return flags |= File::FNM_EXTGLOB if File.const_defined?(:FNM_EXTGLOB)

  raise Error::UnsupportedPattern if pattern =~ /[{}]/

  flags
end

Public Instance Methods

dump_summary(*args) click to toggle source
# File lib/guard/rspec_formatter.rb, line 96
def dump_summary(*args)
  return write_summary(*args) unless self.class.rspec_3?

  notification = args[0]
  write_summary(
    notification.duration,
    notification.example_count,
    notification.failure_count,
    notification.pending_count
  )
end
example_failed(failure) click to toggle source
# File lib/guard/rspec_formatter.rb, line 35
def example_failed(failure)
  examples.push failure.example
end
examples() click to toggle source
# File lib/guard/rspec_formatter.rb, line 39
def examples
  @examples ||= []
end

Private Instance Methods

_failed_paths() click to toggle source
# File lib/guard/rspec_formatter.rb, line 128
def _failed_paths
  klass = self.class
  failed = examples.select { |example| _status_failed?(example) }
  failed.map { |e| klass.extract_spec_location(e.metadata) }.sort.uniq
end
_message(example_count, failure_count, pending_count, duration) click to toggle source
# File lib/guard/rspec_formatter.rb, line 134
def _message(example_count, failure_count, pending_count, duration)
  message = "#{example_count} examples, #{failure_count} failures"
  message << " (#{pending_count} pending)" if pending_count > 0
  message << " in #{duration.round(4)} seconds"
  message
end
_status_failed?(example) click to toggle source
# File lib/guard/rspec_formatter.rb, line 141
def _status_failed?(example)
  if self.class.rspec_3?
    example.execution_result.status.to_s == "failed"
  else
    example.execution_result[:status].to_s == "failed"
  end
end
_write(&block) click to toggle source
# File lib/guard/rspec_formatter.rb, line 118
def _write(&block)
  file = RSpecFormatterResultsPath.new.path
  if Guard.const_defined?(:Compat)
    msg = "Guard::RSpec: using results file: #{file.inspect}"
    Guard::Compat::UI.debug(format(msg, file))
  end
  FileUtils.mkdir_p(File.dirname(file))
  File.open(file, "w", &block)
end
write_summary(duration, total, failures, pending) click to toggle source

Write summary to temporary file for runner

# File lib/guard/rspec_formatter.rb, line 111
def write_summary(duration, total, failures, pending)
  _write do |f|
    f.puts _message(total, failures, pending, duration)
    f.puts _failed_paths.join("\n") if failures > 0
  end
end