lib/rspeed/splitter.rb in rspeed-0.3.0 vs lib/rspeed/splitter.rb in rspeed-0.4.0

- old
+ new

@@ -1,21 +1,48 @@ +# frozen_string_literal: true module RSpeed class Splitter DEFAULT_PATTERN = 'rspeed_*' - def append(files = file_data) + def initialize(specs_path: './spec/**/*_spec.rb') + @specs_path = specs_path + end + + def actual_examples + @actual_examples ||= begin + [].tap do |examples| + Dir[@specs_path].each do |file| + data = File.open(file).read + lines = data.split("\n") + + lines&.each.with_index do |item, index| + examples << "#{file}:#{index + 1}" if item.gsub(/\s+/, '') =~ /^it/ + end + end + + stream(:actual_examples, examples) + end + end + end + + def append(files = CSV.read('rspeed.csv')) files.each do |time, file| redis.lpush('rspeed_tmp', { file: file, time: time.to_f }.to_json) end end def destroy(pattern = DEFAULT_PATTERN) keys(pattern).each { |key| redis.del key } end def diff + actual_data = rspeed_data.select { |item| actual_examples.include?(item[:file]) } + added_data = added_examples.map { |item| { file: item, time: 0 } } + + removed_examples # called just for stream for now + actual_data + added_data end def first_pipe? pipe == 1 @@ -89,46 +116,38 @@ json end private - def actual_data - rspeed_data.select { |item| actual_files.include?(item[:file]) } + def added_examples + @added_examples ||= begin + (actual_examples - rspeed_examples).tap { |examples| stream(:added_examples, examples) } + end end - def actual_files - @actual_files ||= Dir['./spec/**/*_spec.rb'] - end - - def added_data - added_specs.map { |item| { file: item, time: 0 } } - end - - def added_specs - actual_files - old_files - end - - def file_data - CSV.read('rspeed.csv') - end - - def old_files - rspeed_data.map { |item| item[:file] } - end - def redis @redis ||= ::Redis.new(db: ENV['RSPEED_DB'], host: ENV['RSPEED_HOST'], port: ENV.fetch('RSPEED_PORT') { 6379 }) end - def removed_specs - old_files - actual_files + def removed_examples + @removed_examples ||= begin + (rspeed_examples - actual_examples).tap { |examples| stream(:removed_examples, examples) } + end end def removed_time - removed_specs.map { |item| item[0].to_f }.sum + removed_examples.map { |item| item[0].to_f }.sum end def rspeed_data @rspeed_data ||= get('rspeed').map { |item| JSON.parse(item, symbolize_names: true) } + end + + def rspeed_examples + rspeed_data.map { |item| item[:file] } + end + + def stream(type, data) + puts "PIPE: #{pipe} with #{type}: #{data}" end end end