Sha256: 2a8a5a009f25396103daabe9ba7e22569d31dd087cc2755710a0cd0447af91f1

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require_relative "finds_bin"
require_relative "config"
require_relative "initializer_hooks"
require_relative "manages_transactions"
require_relative "starts_rails_server"

module CypressRails
  class LaunchesCypress
    def initialize
      @initializer_hooks = InitializerHooks.instance
      @manages_transactions = ManagesTransactions.instance
      @starts_rails_server = StartsRailsServer.new
      @finds_bin = FindsBin.new
    end

    def call(command, config)
      puts config
      @initializer_hooks.run(:before_server_start)
      if config.transactional_server
        @manages_transactions.begin_transaction
      end
      server = @starts_rails_server.call(
        host: config.host,
        port: config.port,
        transactional_server: config.transactional_server
      )
      bin = @finds_bin.call(config.cypress_dir)

      set_exit_hooks!(config)

      command = <<~EXEC
        CYPRESS_BASE_URL="http://#{server.host}:#{server.port}#{config.base_path}" "#{bin}" #{command} --project "#{config.cypress_dir}" #{config.cypress_cli_opts}
      EXEC

      puts "\nLaunching Cypress…\n$ #{command}\n"
      system command
    end

    private

    def set_exit_hooks!(config)
      at_exit do
        run_exit_hooks_if_necessary!(config)
      end
      Signal.trap("INT") do
        puts "Exiting cypress-rails…"
        exit
      end
    end

    def run_exit_hooks_if_necessary!(config)
      @at_exit_hooks_have_fired ||= false # avoid warning
      return if @at_exit_hooks_have_fired

      if config.transactional_server
        @manages_transactions.rollback_transaction
      end
      @initializer_hooks.run(:before_server_stop)

      @at_exit_hooks_have_fired = true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cypress-rails-0.8.0.rc1 lib/cypress-rails/launches_cypress.rb
cypress-rails-0.7.1 lib/cypress-rails/launches_cypress.rb