class RSpecLog
Class that allows for displaying logs or general messages at that are consistent across rspec tests
Public Class Methods
add_to_log(name, value)
click to toggle source
@name - Name of the log message @value - Content of log message
# File lib/rspec_log.rb, line 39 def self.add_to_log(name, value) log_hash[current_node] = [] if log_hash[current_node].nil? log_hash[current_node] << "#{name}, #{value}" log_hash_set(log_hash) end
new(filename: DEFAULT_LOG_FILE, newfile: false)
click to toggle source
# File lib/rspec_log.rb, line 8 def initialize(filename: DEFAULT_LOG_FILE, newfile: false) raise 'RSpec must be defined to create RSpec log' if (defined? RSpec).nil? @filename = filename RSpecLog.write_hash_to_file({}, @filename) if newfile || RSpecLog.load_yaml_log(filename).nil? RSpecLog.log_hash_set(YAML.load_file(@filename)) at_exit { RSpecLog.print_logs_from_file(filename: @filename) } end
print_logs_from_file(filename: DEFAULT_LOG_FILE)
click to toggle source
Parse the YAML log file and print it out in a nice manner
# File lib/rspec_log.rb, line 25 def self.print_logs_from_file(filename: DEFAULT_LOG_FILE) file_contents = RSpecLog.load_yaml_log(filename) return if file_contents.nil? || file_contents.empty? puts 'RSpecLogs:'.blue.underline file_contents.each do |key, value| puts key value.each { |v| puts " - #{v.to_s.yellow}" } end end
Private Class Methods
current_node()
click to toggle source
# File lib/rspec_log.rb, line 66 def self.current_node ENV.fetch('TARGET_HOST', "#{RSpec.current_example.description}:".blue.bold) end
load_yaml_log(filename)
click to toggle source
# File lib/rspec_log.rb, line 47 def self.load_yaml_log(filename) YAML.load_file(filename) rescue StandardError => e puts e.to_s.yellow nil end
log_hash()
click to toggle source
# File lib/rspec_log.rb, line 58 def self.log_hash RSpec.instance_variable_get(:@log_hash) end
log_hash_set(item)
click to toggle source
# File lib/rspec_log.rb, line 62 def self.log_hash_set(item) RSpec.instance_variable_set(:@log_hash, item) end
write_hash_to_file(log_hash, filename = DEFAULT_LOG_FILE)
click to toggle source
# File lib/rspec_log.rb, line 54 def self.write_hash_to_file(log_hash, filename = DEFAULT_LOG_FILE) File.open(filename, 'w') { |f| f.write(YAML.dump(log_hash, line_width: -1)) } end
Public Instance Methods
write_file(filename: @filename)
click to toggle source
Writes log_hash
to, by default, currently set log file or custom file passed to it
# File lib/rspec_log.rb, line 19 def write_file(filename: @filename) raise 'Filename is not set, you need to initialize RSpecLog before writing' if filename.nil? RSpecLog.write_hash_to_file(RSpecLog.log_hash, filename) end