Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedor Barannik committed May 16, 2017
1 parent c2ebd20 commit f47b21f
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--color


13 changes: 11 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

require 'pry'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec

desc 'Post test case structure to the test management for jira'
task :create_tests

RSpec::Core::RakeTask.new(:create_tests) do |t|
t.rspec_opts = ['--dry-run', '-r ./rspec_config']
end
63 changes: 63 additions & 0 deletions lib/tmj_create_test_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rspec/core/formatters/base_formatter'

RSpec.configuration.add_setting :tmj_create_test_formatter_options, default: {}

class TMJCreateTestFormatter < RSpec::Core::Formatters::BaseFormatter
DEFAULT_CREATE_TEST_FORMATTER_OPTIONS = { update_existing_tests: false, test_owner: nil, custom_labels: nil}.freeze

RSpec::Core::Formatters.register self, :start, :example_started

def start(_notification)
@options = DEFAULT_CREATE_TEST_FORMATTER_OPTIONS.merge(RSpec.configuration.tmj_create_test_formatter_options)
@client = TMJ::Client.new
end

def example_started(notification)
return if notification.example.metadata.has_key?(:test_id) && !notification.example.metadata[:test_id].empty?

begin
response = @client.TestCase.create(process_example(notification.example))
raise TMJ::TestCaseError, response unless response.code == 201
rescue => e
puts e, e.message
exit
end

update_local_test(notification.example, response['key'])
end

private

def update_local_test(example, test_key)
lines = File.readlines(example.metadata[:file_path])
lines[line_number(example)].gsub!("test_id: ''", "test_id: '#{test_key}'")
File.open(example.metadata[:file_path], 'w') { |f| f.write(lines.join) }
end

def line_number(example)
example.metadata[:line_number]-1
end

def process_example(example)
{
"projectKey": "#{TMJ.config.project_id}",
"name": "#{example.metadata[:description]}",
"precondition": "#{example.metadata[:precondition]}",
"owner": "#{@options[:test_owner]}",
"labels": @options[:custom_labels],
"testScript": {
"type": "STEP_BY_STEP",
"steps": process_steps(example.metadata[:steps])
}
}.delete_if { |k, v| v.nil? || v.empty?}
end

def process_steps(examole)
arr = []
examole.each { |s| arr << {"description": s[:step_name]} }
arr
end

end

# TODO: be able to update test case.
5 changes: 3 additions & 2 deletions lib/tmj_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'tmj_ruby'
require 'rspec'

require 'tmj_formatter/version'
require 'tmj_formatter/version'
require_relative 'tmj_formatter/steps'
require_relative 'tmj_formatter/adapter'

require_relative 'tmj_output_formatter'
require_relative 'tmj_result_formatter'
require_relative 'tmj_result_formatter'
require_relative 'tmj_create_test_formatter'
62 changes: 62 additions & 0 deletions lib/tmj_formatter/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# TODO: Figure out a better way to patch this

module RSpec
module Core
class Example
def run(example_group_instance, reporter)
@example_group_instance = example_group_instance
@reporter = reporter
RSpec.configuration.configure_example(self, hooks)
RSpec.current_example = self

if RSpec.configuration.dry_run?
@metadata[:step_index] = 0
@example_group_instance.instance_exec(self, &@example_block)
end

start(reporter)
Pending.mark_pending!(self, pending) if pending?
begin
if skipped?
Pending.mark_pending! self, skip
elsif !RSpec.configuration.dry_run?
with_around_and_singleton_context_hooks do
begin
run_before_example
@example_group_instance.instance_exec(self, &@example_block)

if pending?
Pending.mark_fixed! self

raise Pending::PendingExampleFixedError,
'Expected example to fail since it is pending, but it passed.',
[location]
end
rescue Pending::SkipDeclaredInExample => _
# The "=> _" is normally useless but on JRuby it is a workaround
# for a bug that prevents us from getting backtraces:
# https://github.com/jruby/jruby/issues/4467
#
# no-op, required metadata has already been set by the `skip`
# method.
rescue AllExceptionsExcludingDangerousOnesOnRubiesThatAllowIt => e
set_exception(e)
ensure
run_after_example
end
end
end
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e
set_exception(e)
ensure
@example_group_instance = nil # if you love something... let it go
end

finish(reporter)
ensure
execution_result.ensure_timing_set(clock)
RSpec.current_example = nil
end
end
end
end
20 changes: 12 additions & 8 deletions lib/tmj_formatter/steps.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module TMJFormatter
module Example

def step(step, _options = {}, &block)
@metadata[:steps] = [] if @metadata[:steps].nil?
begin
yield block
@metadata[:steps].push(step_name: step, index: @metadata[:step_index], status: 'Pass')
rescue Exception => e
@metadata[:steps].push(step_name: step, index: @metadata[:step_index], status: 'Fail', comment: process_exception(e))
raise
ensure
@metadata[:step_index] += 1 if @metadata.key?(:step_index)
if RSpec.configuration.dry_run?
@metadata[:steps].push(step_name: step, index: @metadata[:step_index])
else
begin
yield block
@metadata[:steps].push(step_name: step, index: @metadata[:step_index], status: 'Pass')
rescue => e
@metadata[:steps].push(step_name: step, index: @metadata[:step_index], status: 'Fail', comment: process_exception(e))
raise
end
end
ensure @metadata[:step_index] += 1 if @metadata.key?(:step_index)
end

private
Expand Down
26 changes: 14 additions & 12 deletions lib/tmj_result_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.configuration.add_setting :tmj_result_formatter_options, default: {}

class TMJResultFormatter < RSpec::Core::Formatters::BaseFormatter
DEFAULT_PROGRESS_BAR_OPTIONS = { run_only_found_tests: false, post_results: false }.freeze
DEFAULT_RESULT_FORMATTER_OPTIONS = { run_only_found_tests: false, post_results: false }.freeze

RSpec::Core::Formatters.register self, :start, :example_started, :example_passed, :example_failed

Expand All @@ -12,7 +12,7 @@ def initialize(output)
end

def start(_notification)
@options = DEFAULT_PROGRESS_BAR_OPTIONS.merge(RSpec.configuration.tmj_result_formatter_options)
@options = DEFAULT_RESULT_FORMATTER_OPTIONS.merge(RSpec.configuration.tmj_result_formatter_options)
if @options[:run_only_found_tests]
@client = TMJ::Client.new
test_run_data = @client.TestRun.find(TMJ.config.test_run_id)
Expand All @@ -38,17 +38,19 @@ def example_failed(notification)
private

def post_result(example)
return unless @options[:post_results] # example.metadata.key?(:test_id) && !test_id(example).empty?
if TMJ.config.test_run_id
response = @client.TestRun.create_new_test_run_result(test_id(example), with_steps(example))
raise TMJ::TestRunError, response unless response.code == 201
else
response = @client.TestCase.create_new_test_result(example.metadata)
raise TMJ::TestCaseError, response unless response.code == 201
return unless @options[:post_results]
begin
if TMJ.config.test_run_id
response = @client.TestRun.create_new_test_run_result(test_id(example), with_steps(example))
raise TMJ::TestRunError, response unless response.code == 201
else
response = @client.TestCase.create_new_test_result(example.metadata)
raise TMJ::TestCaseError, response unless response.code == 201
end
rescue => e
puts e, e.message
exit
end
rescue => e
puts e, e.message
exit
end

def with_steps(example) # TODO: Make this better
Expand Down
10 changes: 10 additions & 0 deletions rspec_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'tmj_formatter'
require_relative 'lib/tmj_formatter/example'

class RspecConfig
RSpec.configure do |c|
c.color = true
c.formatter = 'TMJCreateTestFormatter'
c.tmj_create_test_formatter_options = { update_existing_tests: true, test_owner: 'test', custom_labels: ['automated'] }
end
end
6 changes: 4 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'tmj_formatter'
require 'pry'


TMJ.configure do |c|
c.base_url = 'https://localhost'
c.auth_type = :basic
Expand All @@ -20,10 +21,11 @@
config.example_status_persistence_file_path = '.rspec_status'

config.include TMJFormatter::Adaptor
config.formatter = 'TMJResultFormatter'
# config.formatter = 'TMJResultFormatter'
config.formatter = 'TMJOutputFormatter'
# config.formatter = 'TMJCreateTestFormatter'

config.tmj_result_formatter_options = { run_only_found_tests: true, post_results: true }
config.tmj_result_formatter_options = { run_only_found_tests: false, post_results: true }

config.expect_with :rspec do |c|
c.syntax = :expect
Expand Down
13 changes: 13 additions & 0 deletions spec/test_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "spec_helper"

PRECONDITION = 'PROVIDE SOME DATA HERE'

RSpec.describe 'Test Case' do
it 'Test Example From Local', precondition: PRECONDITION, test_id: 'CC-T1444' do |e|
e.step 'test' do
end

e.step 'test 2' do
end
end
end
2 changes: 1 addition & 1 deletion spec/tmj_result_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"

RSpec.describe 'TMJResultFormatter' do
RSpec.describe 'TMJResultFormatter', skip: true do
it "example", test_id: 'CC-T913', environment: 'stuff' do |e|
e.step 'check version' do
expect(TMJFormatter::VERSION).not_to be nil
Expand Down

0 comments on commit f47b21f

Please sign in to comment.