Class: RspecStarter::PrepareDatabaseStep

Inherits:
Step
  • Object
show all
Defined in:
lib/rspec_starter/legacy/steps/prepare_database_step.rb

Overview

The steps that destorys and rebuilds the DB before running RSpec.

Instance Attribute Summary collapse

Attributes inherited from Step

#relevant_options

Instance Method Summary collapse

Methods inherited from Step

#should_skip?

Constructor Details

#initialize(defaults, runner) ⇒ PrepareDatabaseStep

Returns a new instance of PrepareDatabaseStep.



6
7
8
9
10
11
12
13
14
# File 'lib/rspec_starter/legacy/steps/prepare_database_step.rb', line 6

def initialize(defaults, runner)
  super(runner)

  @prepare_database = defaults.fetch(:prepare_db, true)
  @relevant_options << '--no-prep-db'
  @user_wants_to_skip = ARGV.any? { |option| option.include?("--no-prep-db") }
  @success_or_skipped = nil # Will be updated once step executes
  @exit_status = 0 # Will be updated once step executes
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



4
5
6
# File 'lib/rspec_starter/legacy/steps/prepare_database_step.rb', line 4

def exit_status
  @exit_status
end

Instance Method Details

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec_starter/legacy/steps/prepare_database_step.rb', line 27

def execute
  return @success_or_skipped = true if should_skip?

  rebuild_cmd = rebuild_command
  print "[#{@runner.step_num}] Preparing the test database with '#{rebuild_cmd.colorize(:light_blue)}' ... "
  _stdout, stderr, exit_status = Open3.capture3(rebuild_cmd)
  @exit_status = exit_status.exitstatus
  @success_or_skipped = successful?(stderr)

  if @success_or_skipped
    puts "Success".colorize(:green)
  else
    puts "Fail".colorize(:red) + "\n\n"
    puts stderr
    puts "\n\nThere was an error rebuilding the test database.  See the output above for details.".colorize(:red)
    puts "or manually run '#{rebuild_cmd}' for more information.".colorize(:red)
  end
end

#failed?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rspec_starter/legacy/steps/prepare_database_step.rb', line 16

def failed?
  !@success_or_skipped
end

#should_execute?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/rspec_starter/legacy/steps/prepare_database_step.rb', line 20

def should_execute?
  return false if @user_wants_to_skip
  return false unless @prepare_database

  @runner.project_is_rails_app? || @runner.project_is_rails_engine?
end