rspec_extensions.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. # Add basic example group slicing functionality to RSpec.
  3. #
  4. # To load it, use: rspec --require ./spec/rspec_extensions.rb
  5. #
  6. # This uses the file size as a rough measurement of its expected runtime,
  7. # which is certainly not perfect, but a sufficient estimate.
  8. module RSpec
  9. module Core
  10. class World
  11. SLICES = ENV.fetch('RSPEC_SLICES', 1).to_i
  12. CURRENT_SLICE = ENV.fetch('RSPEC_CURRENT_SLICE', 1).to_i
  13. if !method_defined?(:orig_ordered_example_groups)
  14. alias orig_ordered_example_groups ordered_example_groups
  15. # Override ordered_example_groups to only return top-level
  16. # example groups of the current slice, based on the size of
  17. # their containing file.
  18. def ordered_example_groups
  19. return orig_ordered_example_groups if SLICES == 1
  20. start_size = 0
  21. slice_size = total_size / SLICES
  22. current_slice_start_size = slice_size * (CURRENT_SLICE - 1)
  23. current_slice_end_size = current_slice_start_size + slice_size
  24. orig_ordered_example_groups.select do |group|
  25. (start_size >= current_slice_start_size && start_size < current_slice_end_size).tap do
  26. start_size += File.size(group.file_path)
  27. end
  28. end
  29. end
  30. # Get the total file size of all (unfiltered) example groups.
  31. def total_size
  32. example_groups.inject(0) do |sum, group|
  33. sum + File.size(group.file_path)
  34. end
  35. end
  36. end
  37. end
  38. end
  39. end