module StartingBlocks::Watcher

Public Class Methods

add_it(file_that_changed) click to toggle source
# File lib/starting_blocks/watcher.rb, line 56
def add_it(file_that_changed)
  return if not_concerned_about? file_that_changed
  StartingBlocks.display "Adding: #{file_that_changed}"
  @all_files << file_that_changed
end
delete_it(file_that_changed) click to toggle source
# File lib/starting_blocks/watcher.rb, line 71
def delete_it(file_that_changed)
  return if not_concerned_about? file_that_changed
  StartingBlocks.display "Deleting: #{file_that_changed}"
  @all_files.delete(file_that_changed)
end
filter_files_according_to_the_contract(files, contract) click to toggle source
# File lib/starting_blocks/watcher.rb, line 8
def self.filter_files_according_to_the_contract files, contract
  extensions = contract.extensions.map { |x| x.gsub('.', '').downcase }
  files.select do |file|
    splits = file.split('/')[-1].split('.')
    (splits.count == 1 && extensions.include?('')) ||
    (extensions.include?(splits[-1].downcase))
  end
end
filter_files_by_file_clues(files, clues) click to toggle source
# File lib/starting_blocks/watcher.rb, line 17
def self.filter_files_by_file_clues files, clues
  files.select do |file|
    file_without_path = file.split('/')[-1]
    matches = clues.select { |c| the_clue_and_the_file_match c, file }
    matches.count > 0
  end
end
run_it(file_that_changed) click to toggle source
# File lib/starting_blocks/watcher.rb, line 62
def run_it(file_that_changed)
  @running = true
  specs = get_the_specs_to_run file_that_changed
  StartingBlocks.display "Matches: #{specs.inspect}"
  results = @runner.run_files specs
  store_the_specs_if_they_failed results, specs
  @running = false
end
start_watching(dir, options) click to toggle source
# File lib/starting_blocks/watcher.rb, line 32
def start_watching(dir, options)
  StartingBlocks.display("Start watching #{dir.getwd} with #{options.inspect}")
  set_up_the_runner options
  set_up_the_contract options

  location = dir.getwd
  @all_files = Dir['**/*']

  puts "Listening to: #{location}"

  callback = Proc.new do |modified, added, removed|
               StartingBlocks.display("File counts: #{[modified.count, added.count, removed.count].inspect}")
               StartingBlocks::Watcher.add_it(added[0])      if added.count > 0
               StartingBlocks::Watcher.delete_it(removed[0]) if removed.count > 0
               next if @running

               modified = StartingBlocks::Watcher.filter_files_according_to_the_contract modified, @contract

               StartingBlocks::Watcher.run_it(modified[0])   if modified.count > 0
             end

  ::Listen::Listener.new location, &callback
end
the_clue_and_the_file_match(clue, file) click to toggle source
# File lib/starting_blocks/watcher.rb, line 27
def the_clue_and_the_file_match clue, file
  file_without_extension = file.split('.')[0]
  file_without_extension.end_with?(clue) || file_without_extension.start_with?(clue)
end

Private Class Methods

flush_file_name(file) click to toggle source
# File lib/starting_blocks/watcher.rb, line 117
def flush_file_name(file)
  the_file = file.downcase.split('/')[-1]
  @contract.file_clues.reduce(the_file) { |t, i| t.gsub(i, '') }
end
get_the_specs_to_run(file_that_changed) click to toggle source
# File lib/starting_blocks/watcher.rb, line 101
def get_the_specs_to_run(file_that_changed)
  filename = flush_file_name file_that_changed
  matches = @all_files.select { |x| flush_file_name(x).include?(filename) && x != file_that_changed }
  matches << file_that_changed

  specs = matches.select { |x| is_a_test_file?(x) && File.file?(x) }.map { |x| File.expand_path x }
  specs = (@last_failed_run + specs).flatten if @last_failed_run

  specs
end
is_a_test_file?(file) click to toggle source
# File lib/starting_blocks/watcher.rb, line 112
def is_a_test_file?(file)
  matches = @contract.file_clues.select { |clue| file.to_s.include?(clue) }
  matches.count > 0
end
not_concerned_about?(file) click to toggle source
# File lib/starting_blocks/watcher.rb, line 79
def not_concerned_about? file
  file.index('.git') == 0
end
set_up_the_contract(options) click to toggle source
# File lib/starting_blocks/watcher.rb, line 87
def set_up_the_contract options
  @contract = StartingBlocks::Contract.for options
end
set_up_the_runner(options) click to toggle source
# File lib/starting_blocks/watcher.rb, line 83
def set_up_the_runner options
  @runner = StartingBlocks::Runner.new(options)
end
store_the_specs_if_they_failed(results, specs) click to toggle source
# File lib/starting_blocks/watcher.rb, line 91
def store_the_specs_if_they_failed results, specs
  parsed_results = StartingBlocks::Publisher.result_builder.build_from results
  if parsed_results[:failures] > 0 || parsed_results[:skips] > 0 || parsed_results[:errors] > 0
    @last_failed_run = specs
  else
    @last_failed_run = nil
  end
rescue
end