Sha256: 21b5bdf5542075d82f55db4ae36de57082aa0b2aa5a6ea9dc52b67986983c3dd

Contents?: true

Size: 1.7 KB

Versions: 23

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module KnapsackPro
  class TestFlatDistributor
    DIR_TYPES = [
      :feature,
      :integration,
      :e2e,
      :models,
      :views,
      :controllers,
    ]

    def initialize(test_files, node_total)
      @test_files = test_files
      @node_total = node_total
      set_default_nodes_hash
      set_grouped_test_files
    end

    def nodes
      group_test_files_by_directory
      generate_nodes
    end

    def test_files_for_node(node_index)
      nodes[node_index]
    end

    private

    attr_reader :test_files,
      :node_total,
      :nodes_hash,
      :grouped_test_files

    def set_default_nodes_hash
      @nodes_hash = {}
      node_total.times do |index|
        nodes_hash[index] = []
      end
    end

    def set_grouped_test_files
      @grouped_test_files = {}
      DIR_TYPES.each do |type|
        grouped_test_files[type] = []
      end
      grouped_test_files[:other] = []
    end

    def sorted_test_files
      test_files.sort_by { |t| t['path'] }
    end

    def group_test_files_by_directory
      sorted_test_files.each do |test_file|
        found = false
        DIR_TYPES.each do |type|
          if test_file['path'].match(/#{type}/)
            grouped_test_files[type] << test_file
            found = true
            break
          end
        end

        unless found
          grouped_test_files[:other] << test_file
        end
      end
    end

    def generate_nodes
      node_index = 0
      grouped_test_files.each do |_type, test_files|
        test_files.each do |test_file|
          nodes_hash[node_index] << test_file

          node_index += 1
          node_index %= node_total
        end
      end
      nodes_hash
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
knapsack_pro-7.13.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.12.1 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.12.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.11.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.10.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.9.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.8.2 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.8.1 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.8.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.7.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.6.2 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.6.1 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.6.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.1.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.0.1 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-7.0.0 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-6.0.4 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-6.0.3 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-6.0.2 lib/knapsack_pro/test_flat_distributor.rb
knapsack_pro-6.0.1 lib/knapsack_pro/test_flat_distributor.rb