Class: Retest::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/retest/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files: [], cache: {}, prompt: nil) ⇒ Repository

Returns a new instance of Repository.



5
6
7
8
9
# File 'lib/retest/repository.rb', line 5

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

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



3
4
5
# File 'lib/retest/repository.rb', line 3

def cache
  @cache
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/retest/repository.rb', line 3

def files
  @files
end

#promptObject

Returns the value of attribute prompt.



3
4
5
# File 'lib/retest/repository.rb', line 3

def prompt
  @prompt
end

Instance Method Details

#add(added) ⇒ Object



54
55
56
57
58
59
# File 'lib/retest/repository.rb', line 54

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

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

#find_test(path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/retest/repository.rb', line 11

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

#find_tests(paths) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/retest/repository.rb', line 25

def find_tests(paths)
  search_tests(paths)
    .values
    .compact
    .uniq
    .sort
end

#remove(removed) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/retest/repository.rb', line 61

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

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

#search_tests(paths) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/retest/repository.rb', line 33

def search_tests(paths)
  result = {}
  ruby_files = paths.select { |path| path.end_with?('.rb') }

  ruby_files.each do |path|
    result[path] ||= find_test(path)
  end

  result
end

#sync(added:, removed:) ⇒ Object



49
50
51
52
# File 'lib/retest/repository.rb', line 49

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

#test_filesObject



45
46
47
# File 'lib/retest/repository.rb', line 45

def test_files
  files.select { |file| MatchingOptions::Path.new(file).test? }
end