Class: Maze::MazeOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/maze_output.rb

Overview

Responsible for writing files to the maze_output directory

Instance Method Summary collapse

Constructor Details

#initialize(scenario) ⇒ MazeOutput

Returns a new instance of MazeOutput.

Parameters:

  • scenario

    The Cucumber scenario



6
7
8
# File 'lib/maze/maze_output.rb', line 6

def initialize(scenario)
  @scenario = scenario
end

Instance Method Details

#output_folderObject

Determines the output folder for the scenario



107
108
109
110
111
112
113
# File 'lib/maze/maze_output.rb', line 107

def output_folder
  folder1 = File.join(Dir.pwd, 'maze_output')
  folder2 = @scenario.failed? ? 'failed' : 'passed'
  folder3 = Maze::Helper.to_friendly_filename(@scenario.name)

  File.join(folder1, folder2, folder3)
end

#write_device_logs(logs) ⇒ Object

Pulls the logs from the device if the scenario fails

Parameters:

  • logs (Array<String>)

    The lines of log to be written



95
96
97
98
99
100
101
102
103
104
# File 'lib/maze/maze_output.rb', line 95

def write_device_logs(logs)

  dir = output_folder
  FileUtils.makedirs(dir)
  filepath = File.join(dir, 'device.log')

  File.open(filepath, 'w+') do |file|
    logs.each { |line| file.puts line }
  end
end

#write_requestsObject

Writes each list of requests to a separate file under, e.g: maze_output/failed/scenario_name/errors.log



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/maze/maze_output.rb', line 12

def write_requests
  path = output_folder
  FileUtils.makedirs(path)

  request_types = %w[errors sessions builds uploads logs sourcemaps traces invalid reflections]
  request_types << 'sampling requests'

  request_types.each do |request_type|
    list = Maze::Server.list_for(request_type).all
    next if list.empty?

    filename = "#{request_type.gsub(' ', '_')}.log"
    filepath = File.join(path, filename)

    counter = 1
    File.open(filepath, 'w+') do |file|
      list.each do |request|
        file.puts "=== Request #{counter} of #{list.size} ==="
        file.puts

        if request[:invalid]
          invalid_request = true
          uri = request[:request][:request_uri]
          headers = request[:request][:header]
          body = request[:request][:body]
        else
          invalid_request = false
          uri = request[:request].request_uri
          headers = request[:request].header
          body = request[:body]
        end

        file.puts "URI: #{uri}"
        file.puts

        # Request
        file.puts "Request:"
        headers.each do |key, values|
          file.puts "  #{key}: #{values.map {|v| "'#{v}'"}.join(' ')}"
        end
        file.puts

        if request.include?(:query)
          file.puts "Query:"
          file.puts JSON.pretty_generate(request[:query])
        end

        file.puts "Request body:"
        if !invalid_request && headers["content-type"].first == 'application/json'
          file.puts JSON.pretty_generate(body)
        else
          file.puts body
        end
        file.puts
        if request.include?(:reason)
          file.puts "REASON:"
          file.puts request[:reason]
          file.puts
        end
        file.puts

        file.puts "Request digests:"
        file.puts JSON.pretty_generate(request[:digests])
        file.puts

        # Response
        response = request[:response]
        file.puts "Response headers:"
        file.puts JSON.pretty_generate(response.header)
        file.puts

        file.puts "Response body: #{response.body}"
        file.puts
        file.puts

        counter += 1
      end
    end
  end
end