Class: RSpeed::Splitter
- Inherits:
-
Object
- Object
- RSpeed::Splitter
- Defined in:
- lib/rspeed/splitter.rb
Instance Method Summary collapse
- #actual_examples ⇒ Object
- #append(files = CSV.read(RSpeed::Variable::CSV)) ⇒ Object
- #diff ⇒ Object
- #first_pipe? ⇒ Boolean
- #get(pattern) ⇒ Object
-
#initialize(specs_path: './spec/**/*_spec.rb') ⇒ Splitter
constructor
A new instance of Splitter.
- #pipe_files ⇒ Object
- #redundant_run? ⇒ Boolean
- #rename ⇒ Object
- #split(data = diff) ⇒ Object
Constructor Details
#initialize(specs_path: './spec/**/*_spec.rb') ⇒ Splitter
Returns a new instance of Splitter.
7 8 9 |
# File 'lib/rspeed/splitter.rb', line 7 def initialize(specs_path: './spec/**/*_spec.rb') @specs_path = specs_path end |
Instance Method Details
#actual_examples ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rspeed/splitter.rb', line 11 def actual_examples @actual_examples ||= begin [].tap do |examples| 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 /^it/.match?(item.gsub(/\s+/, '')) end end stream(:actual_examples, examples) end end end |
#append(files = CSV.read(RSpeed::Variable::CSV)) ⇒ Object
28 29 30 31 32 |
# File 'lib/rspeed/splitter.rb', line 28 def append(files = CSV.read(RSpeed::Variable::CSV)) files.each do |time, file| redis.lpush(RSpeed::Env.tmp_key, { file: file, time: time.to_f }.to_json) end end |
#diff ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/rspeed/splitter.rb', line 34 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 |
#first_pipe? ⇒ Boolean
43 44 45 |
# File 'lib/rspeed/splitter.rb', line 43 def first_pipe? RSpeed::Env.pipe == 1 end |
#get(pattern) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/rspeed/splitter.rb', line 47 def get(pattern) @get ||= begin return redis.lrange(pattern, 0, -1) if [RSpeed::Variable.result, RSpeed::Variable.tmp].include?(pattern) RSpeed::Redis.keys(pattern).map { |key| ::JSON.parse(redis.get(key)) } end end |
#pipe_files ⇒ Object
55 56 57 58 59 |
# File 'lib/rspeed/splitter.rb', line 55 def pipe_files return unless RSpeed::Redis.result? split[RSpeed::Variable.key(RSpeed::Env.pipe)][:files].map { |item| item[:file] }.join(' ') end |
#redundant_run? ⇒ Boolean
61 62 63 |
# File 'lib/rspeed/splitter.rb', line 61 def redundant_run? !first_pipe? && !exists?(RSpeed::Env.result_key) end |
#rename ⇒ Object
65 66 67 |
# File 'lib/rspeed/splitter.rb', line 65 def rename redis.rename(RSpeed::Env.tmp_key, RSpeed::Env.result_key) end |
#split(data = diff) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rspeed/splitter.rb', line 69 def split(data = diff) json = {} 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::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 json end |