From 7847cbf84f38c94b03152ec52ff2d9ed10992f1f Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 28 Jan 2021 15:37:53 +0000 Subject: [PATCH 1/3] Refactor Laravel specific MR helpers --- features/steps/laravel_steps.rb | 31 +++--------------------------- features/support/env.rb | 1 + features/support/laravel.rb | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 28 deletions(-) create mode 100644 features/support/laravel.rb diff --git a/features/steps/laravel_steps.rb b/features/steps/laravel_steps.rb index ba9c9eaa..d1a1424d 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,13 +13,13 @@ 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| @@ -29,20 +27,6 @@ assert_includes(values.raw.flatten, desired_value) end -def fixture - ENV['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 - def current_ip return 'host.docker.internal' if OS.mac? @@ -50,12 +34,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..a39311dc 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -37,6 +37,7 @@ Before do ENV["BUGSNAG_API_KEY"] = $api_key ENV["BUGSNAG_ENDPOINT"] = "http://#{current_ip}:9339" + Laravel.reset! end at_exit do 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 From 8de4f252d4b0f3a4f9719ee591543f4b5e3fd301 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 28 Jan 2021 15:38:15 +0000 Subject: [PATCH 2/3] Add a step to check the Laravel response --- features/steps/laravel_steps.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/features/steps/laravel_steps.rb b/features/steps/laravel_steps.rb index d1a1424d..f561563e 100644 --- a/features/steps/laravel_steps.rb +++ b/features/steps/laravel_steps.rb @@ -22,6 +22,15 @@ Laravel.navigate_to(route) 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 + + assert_match(Regexp.new(regex), Laravel.last_response) +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) From d92387e9477f5fa0fbf2864a008496137c7ae6fd Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Thu, 28 Jan 2021 15:48:17 +0000 Subject: [PATCH 3/3] Clean up more thoroughly after running MR --- features/support/env.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/features/support/env.rb b/features/support/env.rb index a39311dc..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 @@ -41,7 +49,11 @@ 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|