Sha256: 50e8c2b61e8a8d7b892df4d77e4a4a1735ac8507722fceb9c2ec73bd86be8b74

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 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 test_files
      files.select { |file| MatchingOptions::Path.new(file).test? }
    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.2.1 lib/retest/repository.rb
retest-2.2.0 lib/retest/repository.rb
retest-2.1.0 lib/retest/repository.rb
retest-2.0.1 lib/retest/repository.rb
retest-2.0.0 lib/retest/repository.rb
retest-2.0.0.pre5 lib/retest/repository.rb
retest-2.0.0.pre4 lib/retest/repository.rb