Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require:
plugins:
- rubocop-performance

AllCops:
Expand Down
22 changes: 22 additions & 0 deletions allure-ruby-commons/lib/allure-ruby-commons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "require_all"
require "digest"
require "securerandom"

require_rel "allure_ruby_commons/**/*rb"
Expand Down Expand Up @@ -161,6 +162,27 @@ def add_attachment(name:, source:, type:, test_case: false)
lifecycle.add_attachment(name: name, source: source, type: type, test_case: test_case)
end

# Add run-level attachment not bound to a test or fixture
# @param [String] name Attachment name
# @param [File, String] source File or string to save as attachment
# @param [String] type attachment type defined in {ContentType} or any other valid mime type
# @return [void]
def add_global_attachment(name:, source:, type:)
lifecycle.add_global_attachment(name: name, source: source, type: type)
end

# Add run-level error not bound to a test or fixture
# @param [Hash] details
# @option details [Boolean] :known
# @option details [Boolean] :muted
# @option details [Boolean] :flaky
# @option details [String] :message
# @option details [String] :trace
# @return [void]
def add_global_error(**details)
lifecycle.add_global_error(**details)
end

# Manually create environment.properties file
# if this method is called before test run started and
# option clean_results_directory is enabled, the file will be deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(config = Config.instance)

attr_reader :config

def_delegators :file_writer, :write_attachment
def_delegators :file_writer, :write_attachment, :write_globals

# Start test result container
# @param [Allure::TestResultContainer] test_result_container
Expand Down Expand Up @@ -220,6 +220,35 @@ def add_attachment(name:, source:, type:, test_case: false)
write_attachment(source, attachment)
end

# Add run-level attachment
# @param [String] name Attachment name
# @param [File, String] source File or string to save as attachment
# @param [String] type attachment type defined in {Allure::ContentType} or any other valid mime type
# @return [void]
def add_global_attachment(name:, source:, type:)
attachment = ResultUtils.prepare_global_attachment(name, type, timestamp: ResultUtils.timestamp)
return logger.error { "Can't add global attachment, unrecognized mime type: #{type}" } unless attachment

logger.debug { "Adding global attachment '#{name}'" }
write_attachment(source, attachment)
write_globals(Globals.new(attachments: [attachment]))
end

# Add run-level error
# @param [Hash] details
# @option details [Boolean] :known
# @option details [Boolean] :muted
# @option details [Boolean] :flaky
# @option details [String] :message
# @option details [String] :trace
# @return [void]
def add_global_error(**details)
error = ResultUtils.prepare_global_error(timestamp: ResultUtils.timestamp, **details)

logger.debug { "Adding global error '#{error.message}'" }
write_globals(Globals.new(errors: [error]))
end

# Add environment.properties file
#
# @param [Hash, Proc] env
Expand Down
9 changes: 9 additions & 0 deletions allure-ruby-commons/lib/allure_ruby_commons/file_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class FileWriter
TEST_RESULT_CONTAINER_SUFFIX = "-container.json"
# @return [String] attachment file suffix
ATTACHMENT_FILE_SUFFIX = "-attachment"
# @return [String] globals chunk suffix
GLOBALS_SUFFIX = "-globals.json"
# @return [String] environment info file
ENVIRONMENT_FILE = "environment.properties"
# @return [String] categories definition json
Expand Down Expand Up @@ -45,6 +47,13 @@ def write_attachment(source, attachment)
source.is_a?(File) ? copy(source.path, attachment.source) : write(attachment.source, source)
end

# Write allure globals chunk
# @param [Allure::Globals] globals
# @return [void]
def write_globals(globals)
write("#{SecureRandom.uuid}#{GLOBALS_SUFFIX}", dump_json(globals))
end

# Write allure report environment info
# @param [Hash<Symbol, String>] environment
# @return [void]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Allure
# Allure model global attachment object
class GlobalAttachment < Attachment
# @param [Number] timestamp
# @param [Hash] options
# @option options [String] :name attachment name
# @option options [String] :type attachment type, {Allure::ContentType}
# @option options [String] :source attachment file name
def initialize(timestamp:, **options)
super(**options)

@timestamp = timestamp
end

attr_accessor :timestamp
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative "status_details"

module Allure
# Allure model global error object
class GlobalError < StatusDetails
# @param [Number] timestamp
# @param [Hash] options
# @option options [Boolean] :known
# @option options [Boolean] :muted
# @option options [Boolean] :flaky
# @option options [String] :message
# @option options [String] :trace
def initialize(timestamp:, **options)
super(**options)

@timestamp = timestamp
end

attr_accessor :timestamp
end
end
17 changes: 17 additions & 0 deletions allure-ruby-commons/lib/allure_ruby_commons/model/globals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Allure
# Allure model run-level globals chunk
class Globals < JSONable
# @param [Array<Allure::GlobalAttachment>] attachments
# @param [Array<Allure::GlobalError>] errors
def initialize(attachments: [], errors: [])
super()

@attachments = attachments
@errors = errors
end

attr_accessor :attachments, :errors
end
end
29 changes: 29 additions & 0 deletions allure-ruby-commons/lib/allure_ruby_commons/result_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ def prepare_attachment(name, type)
Attachment.new(name: name, source: file_name, type: type)
end

# Allure global attachment object
# @param [String] name
# @param [String] type
# @param [Number] timestamp
# @return [Allure::GlobalAttachment]
def prepare_global_attachment(name, type, timestamp: self.timestamp)
attachment = prepare_attachment(name, type) || return

GlobalAttachment.new(
name: attachment.name,
source: attachment.source,
type: attachment.type,
timestamp: timestamp
)
end

# Allure global error object
# @param [Number] timestamp
# @param [Hash] options
# @option options [Boolean] :known
# @option options [Boolean] :muted
# @option options [Boolean] :flaky
# @option options [String] :message
# @option options [String] :trace
# @return [Allure::GlobalError]
def prepare_global_error(timestamp: self.timestamp, **options)
GlobalError.new(timestamp: timestamp, **options)
end

private

# Check if value is full url
Expand Down
1 change: 1 addition & 0 deletions allure-ruby-commons/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
instance_double(
"FileWriter",
write_attachment: nil,
write_globals: nil,
write_categories: nil,
write_environment: nil,
write_test_result: nil,
Expand Down
23 changes: 23 additions & 0 deletions allure-ruby-commons/spec/unit/allure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ def lifecycle
expect(file_writer).to have_received(:write_attachment).with(args[:source], kind_of(Allure::Attachment))
end

it "adds global attachment" do
args = { name: "Global attach", source: "Some string", type: Allure::ContentType::TXT }
allure.add_global_attachment(**args)

expect(file_writer).to have_received(:write_attachment).with(args[:source], kind_of(Allure::GlobalAttachment))
expect(file_writer).to have_received(:write_globals).with(kind_of(Allure::Globals))
end

it "adds global error" do
allure.add_global_error(message: "Global failure", trace: "trace line")

expect(file_writer).to have_received(:write_globals).with(kind_of(Allure::Globals)) do |globals|
error = globals.errors.first

aggregate_failures do
expect(globals.attachments).to eq([])
expect(error.message).to eq("Global failure")
expect(error.trace).to eq("trace line")
expect(error.timestamp).to be_a(Integer)
end
end
end

it "adds environment" do
env = { PROP1: "test", PROP2: "test" }
allure.add_environment(env)
Expand Down
20 changes: 20 additions & 0 deletions allure-ruby-commons/spec/unit/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@
)
end
end

it "adds global attachment as a globals chunk" do
lifecycle.add_global_attachment(**attach_opts)

aggregate_failures "Global attachment should be written" do
expect(@test_case.attachments).to be_empty
expect(file_writer).to have_received(:write_attachment).with(
"string attachment",
kind_of(Allure::GlobalAttachment)
)
expect(file_writer).to have_received(:write_globals).with(kind_of(Allure::Globals)) do |globals|
attachment = globals.attachments.first

expect(globals.errors).to eq([])
expect(attachment.name).to eq("Test Attachment")
expect(attachment.type).to eq(Allure::ContentType::TXT)
expect(attachment.timestamp).to be_a(Integer)
end
end
end
end
69 changes: 69 additions & 0 deletions allure-ruby-commons/spec/unit/file_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,75 @@
expect(File.exist?(attachment_file)).to be_truthy, "Expected #{attachment_file} to exist"
end

it "writes globals chunk" do
globals = Allure::Globals.new(
attachments: [
Allure::GlobalAttachment.new(
name: "Global attachment",
type: Allure::ContentType::TXT,
source: "#{SecureRandom.uuid}-attachment.txt",
timestamp: 123
)
],
errors: []
)
existing_files = Dir.glob(File.join(results_dir, "*-globals.json"))
file_writer.write_globals(globals)

globals_file = (Dir.glob(File.join(results_dir, "*-globals.json")) - existing_files).first

aggregate_failures "Expected globals chunk to exist with correct content" do
expect(globals_file).to be_a(String)
expect(File.exist?(globals_file)).to be_truthy, "Expected #{globals_file} to exist"
expect(file_writer.load_json(globals_file)).to eq(
attachments: [
{
name: "Global attachment",
type: Allure::ContentType::TXT,
source: globals.attachments.first.source,
timestamp: 123
}
],
errors: []
)
end
end

it "writes globals chunk with error" do
globals = Allure::Globals.new(
attachments: [],
errors: [
Allure::GlobalError.new(
message: "Global failure",
trace: "trace line",
timestamp: 456
)
]
)
existing_files = Dir.glob(File.join(results_dir, "*-globals.json"))
file_writer.write_globals(globals)

globals_file = (Dir.glob(File.join(results_dir, "*-globals.json")) - existing_files).first

aggregate_failures "Expected globals error chunk to exist with correct content" do
expect(globals_file).to be_a(String)
expect(File.exist?(globals_file)).to be_truthy, "Expected #{globals_file} to exist"
expect(file_writer.load_json(globals_file)).to eq(
attachments: [],
errors: [
{
known: false,
muted: false,
flaky: false,
message: "Global failure",
trace: "trace line",
timestamp: 456
}
]
)
end
end

it "writes environment properties" do
environment_file = File.join(results_dir, "environment.properties")
file_writer.write_environment(PROP1: "test", PROP2: "test_2")
Expand Down
Loading
Loading