Class: Overcommit::GitHook::TestHistory

Inherits:
HookSpecificCheck show all
Includes:
HookRegistry
Defined in:
lib/overcommit/plugins/pre_commit/test_history.rb

Constant Summary collapse

TEST_RESULTS_FILE =
'.spec-results'

Instance Method Summary collapse

Methods included from HookRegistry

included

Methods inherited from HookSpecificCheck

friendly_name, #initialize, #name, required!, skippable?, #staged, stealth!, #stealth?

Constructor Details

This class inherits a constructor from Overcommit::GitHook::HookSpecificCheck

Instance Method Details

#relevant_testsObject



5
6
7
8
9
# File 'lib/overcommit/plugins/pre_commit/test_history.rb', line 5

def relevant_tests
  @relevant_test ||=
    `relevant-tests 2> /dev/null -- #{modified_files.join(' ')}`.
    split("\n").map { |r| File.expand_path r }
end

#run_checkObject



16
17
18
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
# File 'lib/overcommit/plugins/pre_commit/test_history.rb', line 16

def run_check
  output = []
  unless relevant_tests.any?
    return :warn, 'No relevant tests for this change...write some?'
  end

  begin
    good_tests = File.open(TEST_RESULTS_FILE, 'r').readlines.map do |spec_file|
      File.expand_path spec_file.strip
    end
  rescue Errno::ENOENT
    good_tests = []
  end

  unless good_tests.any?
    return :bad,
      'The relevant tests for this change have not yet been run using `specr`'
  end

  missed_tests = (relevant_tests - good_tests)
  unless missed_tests.empty?
    output << 'The following relevant tests have not been run recently:'
    output << missed_tests.sort
    return :bad, output
  end

  # Find files modified after the tests were run
  test_time = File.mtime(TEST_RESULTS_FILE)
  untested_files = modified_files.reject do |file|
    File.mtime(file) < test_time
  end

  unless untested_files.empty?
    output << 'The following files were modified after `specr` was run.'
    output << '(their associated tests may be broken):'
    output << untested_files.sort
    return :bad, output
  end

  return :good
end

#skip?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/overcommit/plugins/pre_commit/test_history.rb', line 11

def skip?
  !FileTest.exist?('spec/support/record_results_formatter.rb')
end