Class: PreCommit::RSpecFocusCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/checks/rspec_focus_check.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



4
5
6
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 4

def error_message
  @error_message
end

#grep_commandObject

Returns the value of attribute grep_command.



4
5
6
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 4

def grep_command
  @grep_command
end

#spec_filesObject

Returns the value of attribute spec_files.



4
5
6
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 4

def spec_files
  @spec_files
end

#staged_filesObject

Returns the value of attribute staged_files.



4
5
6
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 4

def staged_files
  @staged_files
end

Class Method Details

.call(quiet = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 6

def self.call(quiet=false)
  dirs = ['spec/'].reject {|d| !File.exists?(d)}
  check = new
  check.staged_files = Utils.staged_files(*dirs)

  result = check.run
  if !quiet && !result
    $stderr.puts check.error_message
    $stderr.puts
    check.print_dash_n_reminder
  end
  result
end

Instance Method Details

#detected_bad_code?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 32

def detected_bad_code?
  spec_files = staged_files.split(' ').select do |file|
    file =~ /_spec\.rb$/
  end

  return false if spec_files.empty?

  diff = `git diff --cached -G:focus #{spec_files.join(" ")}`
  !!(diff =~ /[\W\s]:focus[\W\s]/)
end

#instances_of_rspec_focus_violationsObject



43
44
45
46
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 43

def instances_of_rspec_focus_violations
  cmd = grep_command || "git grep"
  `#{cmd} -nHw ':focus' #{staged_files}`
end


48
49
50
51
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 48

def print_dash_n_reminder
  $stderr.puts 'You can bypass this check using `git commit -n`'
  $stderr.puts
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pre-commit/checks/rspec_focus_check.rb', line 20

def run
  return true if staged_files.empty?

  if detected_bad_code?
    @error_message = "pre-commit: :focus in spec(s) found:\n"
    @error_message += instances_of_rspec_focus_violations
    false
  else
    true
  end
end