Sha256: 2d6e46d57037c9f26654b138f6377bff2ca76667a7e57c6c29c772380908a0ba

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Retest
  class Repository
    attr_accessor :files, :cache, :stdin, :stdout

    def initialize(files: [], cache: {}, stdin: $stdin, stdout: $stdout)
      @cache  = cache
      @files  = files
      @stdin  = stdin
      @stdout = stdout
    end

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

      @path = path
      cache[@path] ||= select_from TestOptions.for(@path, files: files)
    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(tests)
      case tests.count
      when 0, 1
        tests.first
      else
        ask_question tests
        tests[get_input]
      end
    end

    def ask_question(tests)
      stdout.puts(<<~QUESTION)
        We found few tests matching: #{@path}
        #{list_options(tests)}

        Which file do you want to use?
        Enter the file number now:
      QUESTION
    end

    def list_options(tests)
      tests.map.with_index do |file, index|
        "[#{index}] - #{file}"
      end.join("\n")
    end

    def get_input
      stdin.gets.chomp.to_i
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
retest-1.10.0 lib/retest/repository.rb
retest-1.9.0 lib/retest/repository.rb
retest-1.8.0 lib/retest/repository.rb
retest-1.7.0 lib/retest/repository.rb
retest-1.6.2 lib/retest/repository.rb
retest-1.6.1 lib/retest/repository.rb