Class: AllureRubyAdaptorApi::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/allure-ruby-adaptor-api/builder.rb

Constant Summary collapse

MUTEX =
Mutex.new
HOSTNAME =
Socket.gethostname

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.suitesObject

Returns the value of attribute suites.



11
12
13
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 11

def suites
  @suites
end

Class Method Details

.add_attachment(suite, test, opts = {:step => nil, :file => nil, :mime_type => nil}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 74

def add_attachment(suite, test, opts = {:step => nil, :file => nil, :mime_type => nil})
  raise "File cannot be nil!" if opts[:file].nil?
  step = opts[:step]
  file = opts[:file]
  title = opts[:title] || file.basename
  puts "Adding attachment #{opts[:title]} to #{suite}.#{test}#{step.nil? ? "" : ".#{step}"}"
  dir = Pathname.new(Dir.pwd).join(config.output_dir)
  FileUtils.mkdir_p(dir)
  file_extname = File.extname(file.path.downcase)
  mime_type = opts[:mime_type] || MimeMagic.by_path(file.path) || "text/plain"
  attachment = dir.join("#{Digest::SHA256.file(file.path).hexdigest}-attachment#{(file_extname.empty?) ? '' : file_extname}")
  puts "Copying attachment to '#{attachment}'..."
  FileUtils.cp(file.path, attachment)
  attach = {
      :type => mime_type,
      :title => title,
      :source => attachment.basename,
      :file => attachment.basename,
      :target => attachment.basename,
      :size => File.stat(attachment).size
  }
  if step.nil?
    self.suites[suite][:tests][test][:attachments] << attach
  else
    self.suites[suite][:tests][test][:steps][step][:attachments] << attach
  end
end

.build!(opts = {}, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 118

def build!(opts = {}, &block)
  suites_xml = []
  (self.suites || []).each do |suite_title, suite|
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send "ns2:test-suite", :start => suite[:start] || 0, :stop => suite[:stop] || 0, 'xmlns' => '', "xmlns:ns2" => "urn:model.allure.qatools.yandex.ru" do
        xml.send :name, suite_title
        xml.send :title, suite_title
        xml.send "test-cases" do
          suite[:tests].each do |test_title, test|
            xml.send "test-case", :start => test[:start] || 0, :stop => test[:stop] || 0, :status => test[:status] do
              xml.send :name, test_title
              xml.send :title, test_title
              unless test[:failure].nil?
                xml.failure do
                  xml.message test[:failure][:message]
                  xml.send "stack-trace", test[:failure][:stacktrace]
                end
              end
              xml.steps do
                test[:steps].each do |step_title, step_obj|
                  xml.step(:start => step_obj[:start] || 0, :stop => step_obj[:stop] || 0, :status => step_obj[:status]) do
                    xml.send :name, step_title
                    xml.send :title, step_title
                    xml_attachments(xml, step_obj[:attachments])
                  end
                end
              end
              xml_attachments(xml, test[:attachments])
              xml_labels(xml, suite[:labels].merge(test[:labels]))
              xml.parameters
            end
          end
        end
        xml_labels(xml, suite[:labels])
      end
    end
    unless suite[:tests].empty?
      xml = builder.to_xml
      xml = yield suite, xml if block_given?
      dir = Pathname.new(config.output_dir)
      FileUtils.mkdir_p(dir)
      out_file = dir.join("#{UUID.new.generate}-testsuite.xml")
      puts "Writing file '#{out_file}'..."
      File.open(out_file, 'w+') do |file|
        file.write(validate_xml(xml))
      end
      suites_xml << xml
    end
  end
  suites_xml
end

.start_step(suite, test, step) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 63

def start_step(suite, test, step)
  MUTEX.synchronize do
    puts "Starting step #{suite}.#{test}.#{step}"
    self.suites[suite][:tests][test][:steps][step] = {
        :title => step,
        :start => timestamp,
        :attachments => []
    }
  end
end

.start_suite(suite, labels = {:severity => :normal}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 15

def start_suite(suite, labels = {:severity => :normal})
  init_suites
  MUTEX.synchronize do
    puts "Starting case_or_suite #{suite} with labels #{labels}"
    self.suites[suite] = {
        :title => suite,
        :start => timestamp,
        :tests => {},
        :labels => add_default_labels(labels)
    }
  end
end

.start_test(suite, test, labels = {:severity => :normal}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 28

def start_test(suite, test, labels = {:severity => :normal})
  MUTEX.synchronize do
    puts "Starting test #{suite}.#{test} with labels #{labels}"
    self.suites[suite][:tests][test] = {
        :title => test,
        :start => timestamp,
        :failure => nil,
        :steps => {},
        :attachments => [],
        :labels => add_default_labels(labels),
    }
  end
end

.stop_step(suite, test, step, status = :passed) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 102

def stop_step(suite, test, step, status = :passed)
  MUTEX.synchronize do
    puts "Stopping step #{suite}.#{test}.#{step}"
    self.suites[suite][:tests][test][:steps][step][:stop] = timestamp
    self.suites[suite][:tests][test][:steps][step][:status] = status
  end
end

.stop_suite(title) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 110

def stop_suite(title)
  init_suites
  MUTEX.synchronize do
    puts "Stopping case_or_suite #{title}"
    self.suites[title][:stop] = timestamp
  end
end

.stop_test(suite, test, result = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/allure-ruby-adaptor-api/builder.rb', line 42

def stop_test(suite, test, result = {})
  self.suites[suite][:tests][test][:steps].each do |step_title, step|
    if step[:stop].nil? || step[:stop] == 0
      stop_step(suite, test, step_title, result[:status])
    end
  end
  MUTEX.synchronize do
    puts "Stopping test #{suite}.#{test}"
    self.suites[suite][:tests][test][:stop] = timestamp(result[:finished_at])
    self.suites[suite][:tests][test][:start] = timestamp(result[:started_at]) if result[:started_at]
    self.suites[suite][:tests][test][:status] = result[:status]
    if (result[:status].to_sym != :passed)
      self.suites[suite][:tests][test][:failure] = {
          :stacktrace => ((result[:exception] && result[:exception].backtrace) || []).map { |s| s.to_s }.join("\r\n"),
          :message => result[:exception].to_s,
      }
    end

  end
end