Sha256: 388a077e4c122e7618172d86a58eceef6ba24a9e6393df64088f91856c178beb

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

module Retest
  class Repository
    attr_accessor :files, :cache, :prompt

    def initialize(files: [], cache: {}, prompt: nil)
      @cache  = cache
      @files  = files
      @prompt = prompt || Prompt.new
    end

    def find_test(path)
      return unless path
      return if path.empty?

      unless cache.key?(path)
        ok_to_cache, test_file = select_from path, MatchingOptions.for(path, files: files)
        if ok_to_cache
          cache[path] = test_file
        end
      end

      cache[path]
    end

    def find_tests(paths)
      paths
        .select { |path| Regexp.new("\.rb$") =~ path }
        .map    { |path| find_test(path) }
        .compact
        .uniq
        .sort
    end

    def sync(added:, removed:)
      add(added)
      remove(removed)
    end

    def add(added)
      return if added&.empty?

      files.push(*added)
      files.sort!
    end

    def remove(removed)
      return if removed&.empty?

      if removed.is_a?(Array)
        removed.each { |file| files.delete(file) }
      else
        files.delete(removed)
      end
    end

    private

    def select_from(path, matching_tests)
      case matching_tests.count
      when 0
        [false, nil]
      when  1
        [true, matching_tests.first]
      else
        [true, prompt.ask_which_test_to_use(path, matching_tests)]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
retest-2.0.0.pre3 lib/retest/repository.rb
retest-2.0.0.pre2 lib/retest/repository.rb
retest-2.0.0.pre1 lib/retest/repository.rb
retest-2.0.0.pre lib/retest/repository.rb
retest-1.13.2 lib/retest/repository.rb
retest-1.13.1 lib/retest/repository.rb
retest-1.13.0 lib/retest/repository.rb