Sha256: f0ada6c93f26fcb528810475ab84ba92b91190738c798400f48f173fc1fcf229

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'memoist'

class SimpleCov::Formatter::Terminal::FileDeterminer
  extend Memoist

  memoize \
  def executed_spec_file
    if executed_spec_files.size == 1
      executed_spec_files.first
    else
      raise(<<~ERROR)
        Multiple spec files were executed (#{executed_spec_files}), but
        SimpleCov::Formatter::Terminal only works when a single spec file is executed.
      ERROR
    end
  end

  memoize \
  def targeted_application_file
    env_variable_file = ENV.fetch('SIMPLECOV_TARGET_FILE', nil)
    if !env_variable_file.nil?
      puts('Determined targeted application file from SIMPLECOV_TARGET_FILE environment variable!!')
      return env_variable_file
    end

    return nil if unmappable_spec_regexes.any? { executed_spec_file.match?(_1) }

    spec_to_app_file_map.lazy.filter_map do |spec_file_regex, app_file_substitution|
      if executed_spec_file.match?(spec_file_regex)
        executed_spec_file.sub(spec_file_regex, app_file_substitution)
      end
    end.first&.sub(/_spec\.rb\z/, '.rb') ||
    raise("Could not map executed spec file #{executed_spec_file} to application file!")
  end

  memoize \
  def executed_spec_files
    SimpleCov::Formatter::Terminal::RSpecIntegration.executed_spec_files
  end

  memoize \
  def spec_to_app_file_map
    SimpleCov::Formatter::Terminal.config.spec_to_app_file_map ||
    SimpleCov::Formatter::Terminal::SpecToAppMapping.default_spec_to_app_map
  end

  memoize \
  def unmappable_spec_regexes
    SimpleCov::Formatter::Terminal.config.unmappable_spec_regexes
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple_cov-formatter-terminal-0.2.2 lib/simple_cov/formatter/terminal/file_determiner.rb
simple_cov-formatter-terminal-0.2.1 lib/simple_cov/formatter/terminal/file_determiner.rb
simple_cov-formatter-terminal-0.2.0 lib/simple_cov/formatter/terminal/file_determiner.rb
simple_cov-formatter-terminal-0.1.0 lib/simple_cov/formatter/terminal/file_determiner.rb