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
# 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
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



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

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

#condense_rerunsObject



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

def condense_reruns
  @git_controller.capsule_manager.condense_reruns
end

#determine_rerun_usageObject



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

def determine_rerun_usage
  if @user_config.manual_rerun_patterns.nil?
    pair_reruns_with_usages
  else
    @rerun_capsules.clear
    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



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

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



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
110
111
112
113
# File 'lib/flakey_spec_catcher/rerun_manager.rb', line 84

def inject_manual_reruns(patterns, usage)
  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
        add_rerun_capsule(testcase: pattern, usage: usage)
      end

    # No line numbers, queue up all matching files
    else
      matching_files.each do |file|
        add_rerun_capsule(testcase: file, usage: usage)
      end
    end
  end
end

#pair_reruns_with_usagesObject



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

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

  reruns.each do |rerun|
    match_found = false
    configured_usage_patterns.each do |usage_pattern_pair|
      next if match_found

      pattern, usage = usage_pattern_pair
      if rerun =~ /#{pattern}/
        add_rerun_capsule(testcase: rerun, usage: usage)
        match_found = true
      end
    end
    add_rerun_capsule(testcase: rerun) unless match_found
  end
end

#tests_for_rerunObject



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

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