class FlakeySpecCatcher::UserConfig

UserConfig class

Captures user-defined settings to configure RSpec re-run settings.

Constants

USER_CONFIG_ENV_VARS

Attributes

break_on_first_failure[R]
dry_run[R]
enable_runs[R]
excluded_tags[R]
ignore_branches[R]
ignore_files[R]
manual_rerun_patterns[R]
manual_rerun_usage[R]
output_file[R]
repeat_factor[R]
rerun_file_only[R]
rspec_usage_patterns[R]
silent_mode[R]
split_index[R]
split_nodes[R]
test_options[R]
use_parent[R]
verbose[R]

Public Class Methods

new(cli_override: CliOverride.new) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 21
def initialize(cli_override: CliOverride.new)
  apply_env_var_settings
  @cli_override = cli_override
  override_settings
end

Private Instance Methods

apply_cli_override() click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 43
def apply_cli_override
  @manual_rerun_patterns = @cli_override.rerun_patterns
  @manual_rerun_usage = @cli_override.rerun_usage
  @use_parent = @cli_override.use_parent
  @repeat_factor = @cli_override.repeat_factor if @cli_override.repeat_factor.to_i.positive?
  @break_on_first_failure = @cli_override.break_on_first_failure
  @enable_runs = @cli_override.enable_runs
  @dry_run = @cli_override.dry_run
  @split_nodes = @cli_override.split_nodes unless @cli_override.split_nodes.nil?
  @split_index = @cli_override.split_index unless @cli_override.split_index.nil?
  @excluded_tags = if @cli_override.excluded_tags.empty?
    @excluded_tags
  else
    @cli_override.excluded_tags
  end
  @output_file = set_output_file
  @verbose = @cli_override.verbose
  @test_options = @cli_override.test_options
end
apply_env_var_settings() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/flakey_spec_catcher/user_config.rb, line 30
def apply_env_var_settings
  @repeat_factor = initialize_repeat_factor(ENV['FSC_REPEAT_FACTOR'])
  @ignore_files = env_var_string_to_array(ENV['FSC_IGNORE_FILES'])
  @ignore_branches = env_var_string_to_array(ENV['FSC_IGNORE_BRANCHES'])
  @silent_mode = env_var_string_to_bool(ENV['FSC_SILENT_MODE'])
  @rerun_file_only = env_var_string_to_bool(ENV['FSC_RERUN_FILE_ONLY'])
  @rspec_usage_patterns = env_var_string_to_pairs(ENV['FSC_USAGE_PATTERNS'])
  @excluded_tags = env_var_string_to_tags(ENV['FSC_EXCLUDED_TAGS'])
  @output_file = ENV['FSC_OUTPUT_FILE']
  @split_nodes = env_var_string_to_int_or_nil('FSC_NODE_TOTAL')
  @split_index = env_var_string_to_int_or_nil('FSC_NODE_INDEX')
end
env_var_string_to_array(env_var) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 101
def env_var_string_to_array(env_var)
  if env_var.nil? || env_var.empty?
    []
  else
    env_var.split(',').map(&:strip)
  end
end
env_var_string_to_bool(env_var) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 109
def env_var_string_to_bool(env_var)
  if env_var.to_s.casecmp('true').zero?
    true
  else
    false
  end
end
env_var_string_to_int_or_nil(env_var_name) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 89
def env_var_string_to_int_or_nil(env_var_name)
  env_var = ENV[env_var_name]
  if env_var.nil? || env_var.empty?
    nil
  else
    check = env_var.to_i
    raise ArgumentError, "#{env_var_name} must be positive" if check.negative?

    check
  end
end
env_var_string_to_pairs(env_var) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 117
def env_var_string_to_pairs(env_var)
  if env_var.nil? || env_var.empty?
    []
  else
    env_var.scan(/{.*?}/).map do |pair|
      pair.tr('{}', '').split('=>').map(&:strip)
    end
  end
end
env_var_string_to_tags(tag_string) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 127
def env_var_string_to_tags(tag_string)
  if tag_string.nil? || tag_string.empty?
    {}
  else
    tag_strings = tag_string.tr('\'\" ', '').split(',').uniq
    tags_with_values = {}
    return if tag_strings.nil? || tag_strings.empty?

    # Since tags can have a value represented as strings or symbols
    #  we'll only remove the hash rockets and not colons
    #  Example: ":tag => 'special'" => tags_with_values[:tag] = 'special'
    #  Example: ":tag => :special" => tags_with_values[:tag] = :special

    tag_strings.each do |str|
      tag_and_value = str.sub(/=>/, ' ').split(' ')
      tags_with_values[tag_and_value[0]] = tag_and_value[1]
    end

    tags_with_values
  end
end
format_regex_scan_results(matches) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 149
def format_regex_scan_results(matches)
  # Scanning the commit message will result in an array of matches
  #  based on the specified regex. If the pattern uses groups like ours does
  #  then an array of arrays will be returned.
  # See: https://apidock.com/ruby/String/scan

  # For each array of matches, flatten the individual match group sub-array
  #  then remove the formatter quotes we require the values to be wrapped in
  matches.map { |m| remove_formatter_quotes(m.join('')) }
end
initialize_repeat_factor(env_var) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 85
def initialize_repeat_factor(env_var)
  env_var.to_i.positive? ? env_var.to_i : 20
end
override_settings() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/flakey_spec_catcher/user_config.rb, line 64
def override_settings
  apply_cli_override
  return unless @manual_rerun_patterns.nil?

  parse_commit_message
end
override_user_config(env_var, env_value) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/flakey_spec_catcher/user_config.rb, line 174
def override_user_config(env_var, env_value)
  case env_var
  when 'FSC_REPEAT_FACTOR'
    @repeat_factor = initialize_repeat_factor(env_value)
  when 'FSC_IGNORE_FILES'
    @ignore_files = env_var_string_to_array(env_value)
  when 'FSC_IGNORE_BRANCHES'
    @ignore_branches = env_var_string_to_array(env_value)
  when 'FSC_SILENT_MODE'
    @silent_mode = env_var_string_to_bool(env_value)
  when 'FSC_RERUN_FILE_ONLY'
    @rerun_file_only = env_var_string_to_bool(env_value)
  when 'FSC_USAGE_PATTERNS'
    @rspec_usage_patterns = env_var_string_to_pairs(env_value)
  when 'FSC_EXCLUDED_TAGS'
    @excluded_tags = env_var_string_to_tags(env_value)
  when 'FSC_OUTPUT_FILE'
    @output_file = env_value
  end
end
parse_commit_message() click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 160
def parse_commit_message
  commit_message = `git show -q`
  USER_CONFIG_ENV_VARS.each do |env_var|
    matches = commit_message.scan(/#{env_var}\s*=\s*('.*?')/)
    next unless matches.count.positive?

    # In the case of multiple overrides being specified for the same config
    #  variable, we'll only use the first
    env_value = format_regex_scan_results(matches).first
    override_user_config(env_var, env_value)
  end
end
remove_formatter_quotes(env_var) click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 81
def remove_formatter_quotes(env_var)
  env_var.tr('\'\"', '')
end
set_output_file() click to toggle source
# File lib/flakey_spec_catcher/user_config.rb, line 71
def set_output_file
  if !@cli_override.output_file.nil?
    @cli_override.output_file
  elsif @output_file.nil? || @output_file.strip.empty?
    File::NULL
  else
    @output_file
  end
end