Class: TestSuiteSplitter::RspecHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/test_suite_splitter/rspec_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(groups:, group_number:, only_types: nil, tags: nil) ⇒ RspecHelper

Returns a new instance of RspecHelper.



4
5
6
7
8
9
10
# File 'lib/test_suite_splitter/rspec_helper.rb', line 4

def initialize(groups:, group_number:, only_types: nil, tags: nil)
  @groups = groups
  @group_number = group_number
  @example_data_exists = File.exist?("spec/examples.txt")
  @only_types = only_types
  @tags = tags
end

Instance Attribute Details

#only_typesObject (readonly)

Returns the value of attribute only_types.



2
3
4
# File 'lib/test_suite_splitter/rspec_helper.rb', line 2

def only_types
  @only_types
end

#tagsObject (readonly)

Returns the value of attribute tags.



2
3
4
# File 'lib/test_suite_splitter/rspec_helper.rb', line 2

def tags
  @tags
end

Instance Method Details

#example_dataObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/test_suite_splitter/rspec_helper.rb', line 16

def example_data
  @example_data ||= begin
    raw_data = File.read("spec/examples.txt")

    result = []
    raw_data.scan(/^\.\/(.+)\[(.+?)\]\s+\|\s+(.+?)\s+\|\s+((.+?) seconds|)\s+\|$/) do |match|
      file_path = match[0]
      spec_result = match[1]
      seconds = match[4]&.to_f

      spec_data = {
        file_path: file_path,
        spec_result: spec_result,
        seconds: seconds
      }

      result << spec_data
    end

    result
  end
end

#example_data_exists?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/test_suite_splitter/rspec_helper.rb', line 12

def example_data_exists?
  @example_data_exists
end

#example_file(path) ⇒ Object



55
56
57
# File 'lib/test_suite_splitter/rspec_helper.rb', line 55

def example_file(path)
  example_files[path]
end

#example_filesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/test_suite_splitter/rspec_helper.rb', line 39

def example_files
  @example_files ||= begin
    files = {}
    example_data.each do |spec_data|
      file_path = spec_data.fetch(:file_path)
      seconds = spec_data.fetch(:seconds)

      files[file_path] ||= {examples: 0, seconds: 0.0}
      files[file_path][:examples] += 1
      files[file_path][:seconds] += seconds if seconds
    end

    files
  end
end

#group_filesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/test_suite_splitter/rspec_helper.rb', line 59

def group_files
  return @group_files if @group_files

  sorted_files.each do |file|
    file_path = file.fetch(:path)
    file_data = example_file(file_path) if example_data_exists?

    if file_data
      examples = file_data.fetch(:examples)
      seconds = file_data.fetch(:seconds)
    else
      examples = file.fetch(:examples)
    end

    group = group_with_least
    group[:examples] += examples
    group[:files] << file
    group[:seconds] += seconds if seconds
  end

  @group_files = group_orders[@group_number - 1].fetch(:files)
end

#group_ordersObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/test_suite_splitter/rspec_helper.rb', line 82

def group_orders
  @group_orders ||= begin
    group_orders = []
    @groups.times do
      group_orders << {
        examples: 0,
        files: [],
        seconds: 0.0
      }
    end
    group_orders
  end
end

#group_with_leastObject



96
97
98
99
100
101
102
103
104
# File 'lib/test_suite_splitter/rspec_helper.rb', line 96

def group_with_least
  group_orders.min do |group1, group2|
    if example_data_exists? && group1.fetch(:seconds) != 0.0 && group2.fetch(:seconds) != 0.0
      group1.fetch(:seconds) <=> group2.fetch(:seconds)
    else
      group1.fetch(:examples) <=> group2.fetch(:examples)
    end
  end
end

#sorted_filesObject

Sort them so that they are sorted by file path in three groups so each group have an equal amount of controller specs, features specs and so on



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/test_suite_splitter/rspec_helper.rb', line 107

def sorted_files
  files.values.sort do |file1, file2|
    file1_path = file1.fetch(:path)
    file2_path = file2.fetch(:path)

    file1_data = example_file(file1_path) if example_data_exists?
    file2_data = example_file(file2_path) if example_data_exists?

    if file1_data && file2_data && file1_data.fetch(:seconds) != 0.0 && file2_data.fetch(:seconds) != 0.0
      value1 = file1_data[:seconds]
    else
      value1 = file1.fetch(:points)
    end

    if file2_data && file1_data && file2_data.fetch(:seconds) != 0.0 && file2_data.fetch(:seconds) != 0.0
      value2 = file2_data[:seconds]
    else
      value2 = file2.fetch(:points)
    end

    if value1 == value2
      value2 = file1_path
      value1 = file2_path
    end

    value2 <=> value1
  end
end