lib/rspeed/splitter.rb in rspeed-0.4.0 vs lib/rspeed/splitter.rb in rspeed-0.5.0
- old
+ new
@@ -1,114 +1,86 @@
# frozen_string_literal: true
module RSpeed
class Splitter
- DEFAULT_PATTERN = 'rspeed_*'
+ require 'json'
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|
+ Dir[@specs_path].sort.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/
+ lines&.each&.with_index do |item, index|
+ examples << "#{file}:#{index + 1}" if /^it/.match?(item.gsub(/\s+/, ''))
end
end
stream(:actual_examples, examples)
end
end
end
- def append(files = CSV.read('rspeed.csv'))
+ def append(files = CSV.read(RSpeed::Variable::CSV))
files.each do |time, file|
- redis.lpush('rspeed_tmp', { file: file, time: time.to_f }.to_json)
+ redis.lpush(RSpeed::Env.tmp_key, { 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
+ RSpeed::Env.pipe == 1
end
def get(pattern)
@get ||= begin
- return redis.lrange(pattern, 0, -1) if %w[rspeed rspeed_tmp].include?(pattern)
+ return redis.lrange(pattern, 0, -1) if [RSpeed::Variable.result, RSpeed::Variable.tmp].include?(pattern)
- keys(pattern).map { |key| JSON.parse(redis.get(key)) }
+ RSpeed::Redis.keys(pattern).map { |key| ::JSON.parse(redis.get(key)) }
end
end
- def keys(pattern = DEFAULT_PATTERN)
- cursor = 0
- result = []
+ def pipe_files
+ return unless RSpeed::Redis.result?
- loop do
- cursor, results = redis.scan(cursor, match: pattern)
- result += results
-
- break if cursor.to_i.zero?
- end
-
- result
+ split[RSpeed::Variable.key(RSpeed::Env.pipe)][:files].map { |item| item[:file] }.join(' ')
end
- def last_pipe?
- pipe == pipes
+ def redundant_run?
+ !first_pipe? && !exists?(RSpeed::Env.result_key)
end
- def pipe
- ENV.fetch('RSPEED_PIPE') { 1 }.to_i
- end
-
- def pipes
- result? ? ENV.fetch('RSPEED_PIPES') { 1 }.to_i : 1
- end
-
def rename
- redis.rename('rspeed_tmp', 'rspeed')
+ redis.rename(RSpeed::Env.tmp_key, RSpeed::Env.result_key)
end
- def result?
- !keys('rspeed').empty?
- end
-
- def save(data = diff)
- split(data).each { |key, value| redis.set(key, value.to_json) }
- end
-
- def split(data)
+ def split(data = diff)
json = {}
- pipes.times do |index|
- json["rspeed_#{index + 1}".to_sym] ||= []
- json["rspeed_#{index + 1}".to_sym] = { total: 0, files: [], number: index + 1 }
+ RSpeed::Env.pipes.times do |index|
+ json[RSpeed::Variable.key(index + 1)] ||= []
+ json[RSpeed::Variable.key(index + 1)] = { total: 0, files: [], number: index + 1 }
end
sorted_data = data.sort_by { |item| item[:time] }.reverse
sorted_data.each do |record|
selected_pipe_data = json.min_by { |pipe| pipe[1][:total] }
- selected_pipe = json["rspeed_#{selected_pipe_data[1][:number]}".to_sym]
+ selected_pipe = json[RSpeed::Variable.key(selected_pipe_data[1][:number])]
time = record[:time].to_f
selected_pipe[:total] += time
selected_pipe[:files] << { file: record[:file], time: time }
end
@@ -122,32 +94,37 @@
@added_examples ||= begin
(actual_examples - rspeed_examples).tap { |examples| stream(:added_examples, examples) }
end
end
+ # TODO: exists? does not work: undefined method `>' for false:FalseClass
+ def exists?(key)
+ redis.keys.include?(key)
+ end
+
def redis
- @redis ||= ::Redis.new(db: ENV['RSPEED_DB'], host: ENV['RSPEED_HOST'], port: ENV.fetch('RSPEED_PORT') { 6379 })
+ @redis ||= ::RSpeed::Redis.client
end
def removed_examples
@removed_examples ||= begin
(rspeed_examples - actual_examples).tap { |examples| stream(:removed_examples, examples) }
end
end
def removed_time
- removed_examples.map { |item| item[0].to_f }.sum
+ removed_examples.sum { |item| item[0].to_f }
end
def rspeed_data
- @rspeed_data ||= get('rspeed').map { |item| JSON.parse(item, symbolize_names: true) }
+ @rspeed_data ||= get(RSpeed::Env.result_key).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}"
+ puts "PIPE: #{RSpeed::Env.pipe} with #{type}: #{data}"
end
end
end