Created
June 4, 2015 21:19
-
-
Save bolandrm/b1e0ad677a78e67a93eb to your computer and use it in GitHub Desktop.
output errors from logs for feature specs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/formatters/feature_error_progress_formatter.rb | |
require 'rspec/core/formatters/progress_formatter.rb' | |
class FeatureErrorProgressFormatter < RSpec::Core::Formatters::ProgressFormatter | |
LINES_OF_CONTEXT = 15 | |
TEXT_TO_SEARCH = "Internal Server Error" | |
FILE_TO_SEARCH = "log/test.log" | |
private | |
def dump_failure(example, index) | |
super | |
dump_log_error(example) if log_error_desired?(example) | |
end | |
def dump_log_error(example) | |
log_error = attempt_to_fetch_log_error(example) | |
if log_error.present? | |
output_formatted_log_error(log_error) | |
else | |
output_unable_to_fetch | |
end | |
end | |
def output_formatted_log_error(error) | |
output.puts | |
output.puts color(" Server error (from logs):", log_error_color) | |
output.puts color(" #{error.gsub(/\e\[(\d+)m/, '').gsub("\n", "\n ")}", log_error_color) | |
end | |
def log_error_desired?(example) | |
example.exception && | |
example.metadata[:type] == :feature && | |
example.metadata[:js] | |
end | |
def attempt_to_fetch_log_error(example) | |
command = log_fetching_commands(example) | |
stdin, stdout, stderr = Open3.popen3 *command | |
result = stdout.gets(nil) | |
return nil unless result | |
error_match = result.match(/(Internal Server Error.*)Ending: /m) | |
if error_match | |
error_match[1] | |
else | |
webmock_error_match = result.match(/(WebMock::NetConnectNotAllowedError.*)=========.*Ending: /m) | |
webmock_error_match ? webmock_error_match[1] : nil | |
end | |
end | |
def log_fetching_commands(example) | |
trigger = "#{example.metadata[:file_path]}:#{example.metadata[:line_number]}" | |
trigger += "-#{example.metadata[:execution_result][:started_at]}" | |
starting = "Starting: '#{trigger}'" | |
ending = "Ending: '#{trigger}'" | |
starting = starting.gsub("/", "\\/") | |
ending = ending.gsub("/", "\\/") | |
regex = "/#{starting}/,/#{ending}/p" | |
["sed", "-n", "-e", regex, FILE_TO_SEARCH] | |
end | |
def log_error_color | |
:blue | |
end | |
def output_unable_to_fetch | |
output.puts | |
output.puts color(" Unable to fetch errors from log.", log_error_color) | |
output.puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment