Class: PreCommit::CiCheck

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

Constant Summary collapse

CI_TASK_NAME =
'pre_commit:ci'

Instance Method Summary collapse

Instance Method Details

#callObject



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

def call
  if rakefile_present? && has_ci_task?
    run
  else
    $stderr.puts "pre-commit: skipping ci check. The `#{CI_TASK_NAME}` task is not defined."

    # pretend the check passed and move on
    true
  end
end

#has_ci_task?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/pre-commit/checks/ci_check.rb', line 39

def has_ci_task?
  require 'rake'
  Rake.application.init
  Rake.application.load_rakefile
  Rake::Task.task_defined?(CI_TASK_NAME)
rescue LoadError
  $stderr.puts 'pre-commit: rake not found, skipping ci check'
end

#rakefile_present?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/pre-commit/checks/ci_check.rb', line 33

def rakefile_present?
  ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].any? do |file|
    File.exist?(file)
  end
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pre-commit/checks/ci_check.rb', line 17

def run
  if tests_passed?
    true
  else
    $stderr.puts 'pre-commit: your test suite has failed'
    $stderr.puts "for the full output run `#{CI_TASK_NAME}`"
    $stderr.puts

    false
  end
end

#tests_passed?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pre-commit/checks/ci_check.rb', line 29

def tests_passed?
  system("rake #{CI_TASK_NAME} --silent")
end