Class: Semaphore::RspecBooster

Inherits:
Object
  • Object
show all
Defined in:
lib/test_boosters/rspec_booster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_index) ⇒ RspecBooster

Returns a new instance of RspecBooster.



12
13
14
15
16
17
# File 'lib/test_boosters/rspec_booster.rb', line 12

def initialize(thread_index)
  @thread_index = thread_index
  @rspec_split_configuration_path = ENV["RSPEC_SPLIT_CONFIGURATION_PATH"] || "#{ENV["HOME"]}/rspec_split_configuration.json"
  @report_path = ENV["REPORT_PATH"] || "#{ENV["HOME"]}/rspec_report.json"
  @spec_path = ENV["SPEC_PATH"] || "spec"
end

Instance Attribute Details

#report_pathObject (readonly)

Returns the value of attribute report_path.



10
11
12
# File 'lib/test_boosters/rspec_booster.rb', line 10

def report_path
  @report_path
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/test_boosters/rspec_booster.rb', line 19

def run
  exit_code = true
  begin
    specs_to_run = select

    if specs_to_run.empty?
      puts "No spec files in this thread!"
    else
      exit_code = run_command(specs_to_run.join(" "))
    end
  rescue StandardError => e
    if @thread_index == 0
      exit_code = run_command(@spec_path)
    end
  end
  exit_code
end

#run_command(specs) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/test_boosters/rspec_booster.rb', line 37

def run_command(specs)
  options = "--format documentation --format json --out #{@report_path}"
  puts "Rspec options: #{options}"
  puts
  puts "========================= Running Rspec =========================="
  puts

  Semaphore.execute("bundle exec rspec #{options} #{specs}")
end

#selectObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/test_boosters/rspec_booster.rb', line 47

def select
  with_fallback do
    file_distribution = JSON.parse(File.read(@rspec_split_configuration_path))
    thread_count = file_distribution.count
    thread = file_distribution[@thread_index]

    all_specs = Dir["#{@spec_path}/**/*_spec.rb"].sort
    all_known_specs = file_distribution.map { |t| t["files"] }.flatten.sort

    all_leftover_specs = all_specs - all_known_specs
    thread_leftover_specs = LeftoverFiles.select(all_leftover_specs, thread_count, @thread_index)
    thread_specs = all_specs & thread["files"].sort
    specs_to_run = thread_specs + thread_leftover_specs

    Semaphore.display_files("This thread specs:", thread_specs)
    Semaphore.display_title_and_count("All leftover specs:", all_leftover_specs)
    Semaphore.display_files("This thread leftover specs:", thread_leftover_specs)

    specs_to_run
  end
end

#with_fallbackObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/test_boosters/rspec_booster.rb', line 70

def with_fallback
  yield
rescue StandardError => e
  error = %{
    WARNING: An error detected while parsing the test boosters report file.
    WARNING: All tests will be executed on the first thread.\n
  }

  error += %{Exception: #{e.message}\n#{e.backtrace.join("\n")}}

  puts error
  Semaphore.log(error)

  raise
end