Class: Pendaxes::Detector::RSpec

Inherits:
Pendaxes::Detector show all
Defined in:
lib/pendaxes/detectors/rspec.rb

Constant Summary collapse

PENDERS =
%w(xit xexample xspecify pending).freeze
GREP_CMD =
( %w(grep --name-only -e) \
+ PENDERS.join(' --or -e ').split(/ /) \
+ ['--']
).freeze
PARENTS =
%w(context describe it example specify xexample xspecify xit).freeze

Instance Method Summary collapse

Methods inherited from Pendaxes::Detector

#initialize

Methods included from Pendaxes::Defaults

#defaults

Methods included from Finder

#announce, #find, #inherited

Constructor Details

This class inherits a constructor from Pendaxes::Detector

Instance Method Details

#detectObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pendaxes/detectors/rspec.rb', line 19

def detect
  @workspace.dive do
    pattern = @config.pattern.is_a?(Array) ? @config.pattern : [@config.pattern]
    grep_raw = @workspace.git(*GREP_CMD, *pattern)
    return [] unless grep_raw
    grep = grep_raw.force_encoding("UTF-8")
    files = grep.split(/\r?\n/).map(&:chomp)

    files.inject([]) do |pendings, file|
      @config.out.puts "* #{file}" if @config.out
      file_content = File.read(file).force_encoding(@config.encoding || "UTF-8")
      lines = file_content.split(/\r?\n/)
      tokens = Ripper.lex(file_content, file)
      _prev = nil

      tokens.each_with_index do |token, i|
        prev = _prev
        _prev = token[1]
        next if prev == :on_symbeg
        next unless token[1] == :on_ident && PENDERS.include?(token[2])
        pending = {}

        line = token[0][0]

        parent = (i-1).downto(0).inject {|_, j|
          break lines[tokens[j][0][0]-1] if tokens[j][1] == :on_ident && (j.zero? || tokens[j-1][1] != :on_symbeg) && PARENTS.include?(tokens[j][2])
          nil
        }
        parent.gsub!(/^[ \t]+/, '') if parent

        pending[:example] = {
          file: file, line: line,
          message: lines[line-1].gsub(/^[ \t]+/, ''), parent: parent
        }

        pending[:commit] = blame(file, line)
        next unless pending[:commit]
        pending[:allowed] = (Time.now - pending[:commit][:at]) <= @config.allowed_for

        pendings << pending
      end

      pendings
    end
  end
end