Class: FlakeySpecCatcher::RerunManager

Inherits:
Object
  • Object
show all
Defined in:
lib/flakey_spec_catcher/rerun_manager.rb

Overview

RerunManager class

Pairs file changes with user-specified re-run commands. Those pairs are defined via environment variables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_controller: FlakeySpecCatcher::GitController.new, user_config: FlakeySpecCatcher::UserConfig.new) ⇒ RerunManager

Returns a new instance of RerunManager.



13
14
15
16
17
18
19
20
21
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 13

def initialize(git_controller: FlakeySpecCatcher::GitController.new,
  user_config: FlakeySpecCatcher::UserConfig.new)

  @git_controller = git_controller
  @user_config = user_config
  @rerun_capsules = []
  determine_rerun_usage
  split!
end

Instance Attribute Details

#rerun_capsulesObject (readonly)

Returns the value of attribute rerun_capsules.



11
12
13
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 11

def rerun_capsules
  @rerun_capsules
end

Instance Method Details

#all_non_example_groupsObject

Get all change contexts that can invoke expectations/assertions



50
51
52
53
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 50

def all_non_example_groups
  @git_controller.capsule_manager.sorted_change_contexts.reject(&:example_group?)
                 .map(&:rerun_info)
end

#condense_rerunsObject



45
46
47
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 45

def condense_reruns
  @git_controller.capsule_manager.condense_reruns
end

#determine_rerun_usageObject



23
24
25
26
27
28
29
30
31
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 23

def determine_rerun_usage
  if @user_config.manual_rerun_patterns.nil?
    pair_reruns_with_usages
  else
    @rerun_capsules = []
    inject_manual_reruns(@user_config.manual_rerun_patterns,
                         @user_config.manual_rerun_usage)
  end
end

#identify_tag_excluded_rerunsObject

Identifies all contexts that have a tag present



56
57
58
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 56

def identify_tag_excluded_reruns
  @git_controller.capsule_manager.find_reruns_by_tags(@user_config.excluded_tags)
end

#inject_manual_reruns(patterns, usage) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 81

def inject_manual_reruns(patterns, usage)
  tests = []
  patterns.tr(' ', '').split(',').each do |pattern|
    # Check if file exists first and handle if user supplies testcase:line_number
    file_name = pattern.split(':')[0]
    line_number_present = pattern.split(':').count > 1
    matching_files = Dir.glob(file_name)
    # If no file matches are run, don't queue up re-runs
    if matching_files.count.zero?
      puts "Specified pattern #{pattern} did not match an existing file"
      raise ArgumentError
    end
    # It won't make sense to have multiple files to run with one specific line number
    if line_number_present
      if matching_files.count > 1
        puts "Specified pattern #{pattern} matched multiple files but a line number was given"
        raise ArgumentError
      else
        tests.push pattern
      end
    # No line numbers, queue up all matching files
    else
      matching_files.each do |file|
        tests.push file
      end
    end
  end
  add_rerun_capsule(testcase: tests, usage: usage)
end

#pair_reruns_with_usagesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 60

def pair_reruns_with_usages
  reruns = tests_for_rerun
  configured_usage_patterns = @user_config.rspec_usage_patterns
  if configured_usage_patterns.count.zero?
    add_capsules_with_default_usage(reruns)
    return
  end

  configured_usage_patterns.each do |usage_pattern_pair|
    tests = []
    pattern, usage = usage_pattern_pair
    reruns.each do |rerun|
      tests.push rerun if rerun =~ /#{pattern}/
    end
    reruns -= tests
    add_rerun_capsule(testcase: tests, usage: usage)
  end
  add_rerun_capsule(testcase: reruns) if reruns.any?
end

#tests_for_rerunObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 33

def tests_for_rerun
  tests = if @user_config.rerun_file_only
    @git_controller.capsule_manager.changed_files
    # If no tags are specified, condense re-runs else expand them
  elsif @user_config.excluded_tags.empty?
    condense_reruns - identify_tag_excluded_reruns
  else
    all_non_example_groups - identify_tag_excluded_reruns
  end
  filter_reruns_by_ignore_files(tests)
end