diff --git a/features/steps/laravel_steps.rb b/features/steps/laravel_steps.rb index ba9c9eaa..f561563e 100644 --- a/features/steps/laravel_steps.rb +++ b/features/steps/laravel_steps.rb @@ -1,5 +1,3 @@ -require 'net/http' - Given(/^I enable session tracking$/) do steps %{ When I set environment variable "BUGSNAG_CAPTURE_SESSIONS" to "true" @@ -15,32 +13,27 @@ When(/^I start the laravel fixture$/) do steps %{ - When I start the service "#{fixture}" - And I wait for the host "localhost" to open port "#{fixture_port}" + When I start the service "#{Laravel.fixture}" + And I wait for the host "localhost" to open port "#{Laravel.fixture_port}" } end When("I navigate to the route {string}") do |route| - navigate_to(route) + Laravel.navigate_to(route) end -Then("the exception {string} matches one of the following:") do |path, values| - desired_value = read_key_path(Server.current_request[:body], "events.0.exceptions.0.#{path}") - assert_includes(values.raw.flatten, desired_value) -end +Then("the Laravel response matches {string}") do |regex| + wait = Maze::Wait.new(timeout: 10) + success = wait.until { Laravel.last_response != nil } + + raise 'No response from the Laravel fixture!' unless success -def fixture - ENV['LARAVEL_FIXTURE'] || 'laravel56' + assert_match(Regexp.new(regex), Laravel.last_response) end -def fixture_port - case fixture - when 'laravel-latest' then 61299 - when 'laravel66' then 61266 - when 'laravel58' then 61258 - when 'laravel56' then 61256 - else raise "Unknown laravel fixture '#{ENV['LARAVEL_FIXTURE']}'!" - end +Then("the exception {string} matches one of the following:") do |path, values| + desired_value = read_key_path(Server.current_request[:body], "events.0.exceptions.0.#{path}") + assert_includes(values.raw.flatten, desired_value) end def current_ip @@ -50,12 +43,3 @@ def current_ip ip_list = /((?:[0-9]*\.){3}[0-9]*)/.match(ip_addr) ip_list.captures.first end - -def navigate_to(route, attempts = 0) - Net::HTTP.get('localhost', route, fixture_port) -rescue => e - raise "Failed to navigate to #{route} (#{e})" if attempts > 15 - - sleep 1 - navigate_to(route, attempts + 1) -end diff --git a/features/support/env.rb b/features/support/env.rb index b8711b61..d0238797 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -3,31 +3,39 @@ # Copy bugsnag-laravel into fixture directories FIXTURE_DIR = 'features/fixtures' -VENDORED_LIB = FIXTURE_DIR + '/bugsnag-laravel.zip' +VENDORED_LIB = "#{FIXTURE_DIR}/bugsnag-laravel.zip" +FILES_TO_CLEANUP = [VENDORED_LIB] `composer archive -f zip --dir=#{File.dirname(VENDORED_LIB)} --file=#{File.basename(VENDORED_LIB, '.zip')}` -Dir.glob(FIXTURE_DIR + '/laravel*').each do |directory| + +Dir.glob("#{FIXTURE_DIR}/laravel*").each do |directory| next if directory.end_with?('laravel-latest') - FileUtils.cp(VENDORED_LIB, directory + '/bugsnag-laravel.zip') + zip = "#{directory}/bugsnag-laravel.zip" + FILES_TO_CLEANUP << zip + + FileUtils.cp(VENDORED_LIB, zip) + # Remove any locally installed composer deps - FileUtils.rm_rf(directory + '/vendor') + FileUtils.rm_rf("#{directory}/vendor") end - # Copy current requirements into fixture requirements File.open('composer.json', 'r') do |source| parsed_composer = JSON.parse(source.read) requirements = parsed_composer["require"] - Dir.glob(FIXTURE_DIR + '/laravel*').each do |directory| + Dir.glob("#{FIXTURE_DIR}/laravel*").each do |directory| next if directory.end_with?('laravel-latest') File.open(directory + '/composer.json.template', 'r') do |template| parsed_template = JSON.parse template.read parsed_template["repositories"][0]["package"]["require"] = requirements - File.open(directory + '/composer.json', 'w') do |target| + composer_json = "#{directory}/composer.json" + FILES_TO_CLEANUP << composer_json + + File.open(composer_json, 'w') do |target| target.write(JSON.pretty_generate(parsed_template)) end end @@ -37,10 +45,15 @@ Before do ENV["BUGSNAG_API_KEY"] = $api_key ENV["BUGSNAG_ENDPOINT"] = "http://#{current_ip}:9339" + Laravel.reset! end at_exit do - FileUtils.rm_rf(VENDORED_LIB) + project_root = File.dirname(File.dirname(__dir__)) + + FILES_TO_CLEANUP.each do |file| + FileUtils.rm_rf("#{project_root}/#{file}") + end end AfterConfiguration do |_config| diff --git a/features/support/laravel.rb b/features/support/laravel.rb new file mode 100644 index 00000000..c37081e8 --- /dev/null +++ b/features/support/laravel.rb @@ -0,0 +1,34 @@ +require 'net/http' + +class Laravel + class << self + attr_reader :last_response + + def reset! + @last_response = nil + end + + def navigate_to(route, attempts = 0) + @last_response = Net::HTTP.get('localhost', route, fixture_port) + rescue => e + raise "Failed to navigate to #{route} (#{e})" if attempts > 15 + + sleep 1 + navigate_to(route, attempts + 1) + end + + def fixture + ENV.fetch('LARAVEL_FIXTURE', 'laravel56') + end + + def fixture_port + case fixture + when 'laravel-latest' then 61299 + when 'laravel66' then 61266 + when 'laravel58' then 61258 + when 'laravel56' then 61256 + else raise "Unknown laravel fixture '#{ENV['LARAVEL_FIXTURE']}'!" + end + end + end +end