Skip to content

Commit

Permalink
Add method for defining custom categories (#65)
Browse files Browse the repository at this point in the history
* Add method for categories definition

* Fix rubocop warnings

* Bump ruby version
  • Loading branch information
andrcuns authored Jan 7, 2020
1 parent 72de146 commit fe6c810
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 10 deletions.
10 changes: 5 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Bundler/InsecureProtocolSource:
Bundler/OrderedGems:
Enabled: true

Layout/LineLength:
Enabled: true
Max: 120

Layout/AccessModifierIndentation:
Enabled: true
EnforcedStyle: indent
Expand Down Expand Up @@ -636,10 +640,6 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Enabled: false

Metrics/LineLength:
Enabled: true
Max: 120

Metrics/MethodLength:
Enabled: true
Max: 12
Expand Down Expand Up @@ -1439,7 +1439,7 @@ Style/TrivialAccessors:
AllowPredicates: true
AllowDSLWriters: false
IgnoreClassMethods: false
AllowedMethod:
AllowedMethods:
- to_ary
- to_a
- to_c
Expand Down
2 changes: 1 addition & 1 deletion ALLURE_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.13.2
2.13.3
1 change: 1 addition & 0 deletions allure-ruby-commons/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Allure Ruby Adaptor API

[![Yard Docs](https://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/allure-ruby-commons)

This is a helper library containing the basics for any ruby-based Allure adaptor.
Expand Down
9 changes: 8 additions & 1 deletion allure-ruby-commons/lib/allure-ruby-commons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,20 @@ def add_attachment(name:, source:, type:, test_case: false)
lifecycle.add_attachment(name: name, source: source, type: type, test_case: test_case)
end

# Write allure report environment info
# Add allure report environment info
# @param [Hash<Symbol, String>] environment
# @return [void]
def add_environment(environment)
lifecycle.write_environment(environment)
end

# Add categories info
# @param [File, Array<Allure::Category>] categories
# @return [void]
def add_categories(categories)
lifecycle.write_categories(categories)
end

# Add step with provided name and optional status to current test step, fixture or test case
# @param [String] name
# @param [Symbol] status <Allure::Status>, <Allure::Status::PASSED> by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize
@step_context = []
end

def_delegators :file_writer, :write_attachment, :write_environment
def_delegators :file_writer, :write_attachment, :write_environment, :write_categories

# Start test result container
# @param [Allure::TestResultContainer] test_result_container
Expand Down
17 changes: 16 additions & 1 deletion allure-ruby-commons/lib/allure_ruby_commons/file_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class FileWriter
TEST_RESULT_CONTAINER_SUFFIX = "-container.json"
# @return [String] attachment file suffix
ATTACHMENT_FILE_SUFFIX = "-attachment"
# @return [String] environment info file
ENVIRONMENT_FILE = "environment.properties"
# @return [String] categories definition json
CATEGORIES_FILE = "categories.json"

# Write test result
# @param [Allure::TestResult] test_result
Expand Down Expand Up @@ -37,7 +41,18 @@ def write_attachment(source, attachment)
# @return [void]
def write_environment(environment)
environment.reduce("") { |e, (k, v)| e + "#{k}=#{v}\n" }.tap do |env|
write("environment.properties", env)
write(ENVIRONMENT_FILE, env)
end
end

# Write categories info
# @param [File, Array<Allure::Category>] categories
# @return [void]
def write_categories(categories)
if categories.is_a?(File)
copy(categories.path, CATEGORIES_FILE)
else
write(CATEGORIES_FILE, categories.to_json)
end
end

Expand Down
19 changes: 19 additions & 0 deletions allure-ruby-commons/lib/allure_ruby_commons/model/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require_relative "jsonable"

module Allure
class Category < JSONable
# Defects category
# @param [String] name
# @param [Array<Allure::Status>] matched_statuses
# @param [String, Regexp] message_regex
# @param [String, Regexp] trace_regex
def initialize(name:, matched_statuses: nil, message_regex: nil, trace_regex: nil)
@name = name
@matched_statuses = matched_statuses
@message_regex = message_regex
@trace_regex = trace_regex
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class Status
BROKEN = :broken
PASSED = :passed
SKIPPED = :skipped
UNKNOWN = :unknown
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "jsonable"

module Allure
# Allure model step result container
class TestResultContainer < JSONable
Expand Down
8 changes: 8 additions & 0 deletions allure-ruby-commons/spec/fixtures/categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name": "Ignored test",
"matchedStatuses": [
"skipped"
]
}
]
7 changes: 7 additions & 0 deletions allure-ruby-commons/spec/unit/allure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
end
end

it "adds categories" do
[Allure::Category.new(name: "Ignored test", matched_statuses: [Allure::Status::SKIPPED])].tap do |cat|
expect(file_writer).to receive(:write_categories).with(cat)
Allure.add_categories(cat)
end
end

it "adds custom step" do
test_step = Allure.step(name: "Custom step", status: Allure::Status::FAILED)
expect(@test_case.steps.last).to eq(test_step)
Expand Down
19 changes: 18 additions & 1 deletion allure-ruby-commons/spec/unit/file_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
expect(File.exist?(attachment_file)).to be_truthy, "Expected #{attachment_file} to exist"
end

it "write environment properties" do
it "writes environment properties" do
environment_file = File.join(Allure::Config.results_directory, "environment.properties")
file_writer.write_environment(PROP_1: "test", PROP_2: "test_2")

Expand All @@ -58,4 +58,21 @@
PROP_2=test_2
FILE
end

it "writes categories from argument" do
categories_file = File.join(Allure::Config.results_directory, "categories.json")
file_writer.write_categories(
[Allure::Category.new(name: "Ignored test", matched_statuses: [Allure::Status::SKIPPED])],
)

expect(File.exist?(categories_file)).to be_truthy, "Expected #{categories_file} to exist"
expect(File.read(categories_file)).to eq('[{"name":"Ignored test","matchedStatuses":["skipped"]}]')
end

it "writes categories from file" do
categories_file = File.join(Allure::Config.results_directory, "categories.json")
file_writer.write_categories(File.new(File.join(Dir.pwd, "spec", "fixtures", "categories.json")))

expect(File.exist?(categories_file)).to be_truthy, "Expected #{categories_file} to exist"
end
end

0 comments on commit fe6c810

Please sign in to comment.