Sha256: 057b8f0bec8805f307953d0d26c0a90e8ce935bd2238f17ca054e5916b197431

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require 'set'

module TestFileFinder
  class FileFinder
    attr_reader :strategies

    def initialize(paths: [])
      @paths = [paths].flatten
      @strategies = []
    end

    def use(strategy)
      @strategies << strategy
    end

    def test_files
      search
    end

    private

    attr_reader :paths

    def search
      file_name_patterns, plain_guesses = file_path_guesses.partition(&file_name_pattern?)

      file_name_patterns.flat_map { |pattern| Dir.glob(pattern) } +
        plain_guesses.select { |path| File.exist?(path) }
    end

    # Returns true if a test file name contains metacharacter like *, {, [, ?
    # which indicates a file name pattern.
    #
    # See https://rubyapi.org/o/dir#method-c-glob
    def file_name_pattern?
      proc { |guess| guess.match?(/[*{\[?]/) }
    end

    def file_path_guesses
      strategies.each_with_object(Set.new) do |strategy, result|
        matches = strategy.match(paths)
        result.merge(matches)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
test_file_finder-0.3.1 lib/test_file_finder/file_finder.rb
test_file_finder-0.3.0 lib/test_file_finder/file_finder.rb