Class: RspecN::Formatters::FileFormatter

Inherits:
Object
  • Object
show all
Includes:
TimeHelpers
Defined in:
lib/rspec_n/formatters/file_formatter.rb

Constant Summary collapse

BASE_FILE_NAME =
"rspec_n_iteration".freeze

Instance Method Summary collapse

Methods included from TimeHelpers

#convert_seconds_to_hms

Constructor Details

#initialize(runner:) ⇒ FileFormatter

Returns a new instance of FileFormatter.



8
9
10
11
12
# File 'lib/rspec_n/formatters/file_formatter.rb', line 8

def initialize(runner:)
  @runner = runner
  @format = "%m/%d %l:%M:%S %p"
  delete_all_files
end

Instance Method Details

#delete_all_filesObject



14
15
16
17
# File 'lib/rspec_n/formatters/file_formatter.rb', line 14

def delete_all_files
  log_directory = Pathname.new(@runner.input.log_path)
  Dir.glob(log_directory.join("#{BASE_FILE_NAME}.**")).each { |file| File.delete(file) }
end

#write(run, command) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec_n/formatters/file_formatter.rb', line 19

def write(run, command)
  return if run.skipped?
  return unless @runner.input.write_files?

  log_directory = Pathname.new(@runner.input.log_path)
  FileUtils.mkdir_p(log_directory)
  file_path = log_directory.join("#{BASE_FILE_NAME}.#{run.iteration}")

  File.open(file_path, "w") do |f|
    f.write("Iteration: #{run.iteration}\n")
    f.write("Start Time: #{run.formatted_start_time(@format)}\n")
    f.write("Finish Time: #{run.formatted_finish_time(@format)}\n")
    f.write("Duration: #{convert_seconds_to_hms(run.duration_seconds)}\n")
    f.write("Command: #{command}\n\n")
    f.write(run.rspec_stdout)
    f.write(run.rspec_stderr)
  end
end