Skip to content

Commit

Permalink
Add update test formatter and update tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
frbk committed May 31, 2017
1 parent 14c7678 commit 8f8d749
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 43 deletions.
37 changes: 4 additions & 33 deletions lib/tmj_create_test_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
require 'rspec/core/formatters/base_formatter'
require_relative 'tmj_formatter/helpers/base_formatter'

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(TMJFormatter.config.create_test_formatter_options)
@client = TMJ::Client.new(TMJFormatter.config.to_hash)
end
class TMJCreateTestFormatter < TMJFormatter::BaseFormatter
RSpec::Core::Formatters.register self, *NOTIFICATIONS

def example_started(notification)
if (notification.example.metadata.has_key?(:test_id) && !notification.example.metadata[:test_id].strip.empty?) || !notification.example.metadata.has_key?(:test_id)
Expand All @@ -28,34 +21,12 @@ def example_started(notification)

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}'")
lines[line_number(example)].gsub!(/test_id:(\s+)?('|")(\s+)?('|")/, "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": "#{TMJFormatter.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.
1 change: 1 addition & 0 deletions lib/tmj_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
require_relative 'tmj_output_formatter'
require_relative 'tmj_result_formatter'
require_relative 'tmj_create_test_formatter'
require_relative 'tmj_update_test_formatter'
45 changes: 45 additions & 0 deletions lib/tmj_formatter/helpers/base_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'rspec/core/formatters/base_formatter'
module TMJFormatter
class BaseFormatter < RSpec::Core::Formatters::BaseFormatter
DEFAULT_OPTIONS = { update_existing_tests: false, test_owner: nil, custom_labels: nil}.freeze

NOTIFICATIONS = %i[start, example_started].freeze

def start(_notification)
@options = DEFAULT_OPTIONS.merge(TMJFormatter.config.create_test_formatter_options)
@client = TMJ::Client.new(TMJFormatter.config.to_hash)
end

private

def process_example(example)
{
"projectKey": configure_project_key,
"name": example.metadata[:full_description],
"objective": example.metadata[:objective],
"precondition": example.metadata[:precondition],
"folder": example.metadata[:folder],
"status": example.metadata[:status],
"priority": example.metadata[:priority],
"owner": @options[:test_owner],
"labels": @options[:custom_labels],
"testScript": process_steps(example.metadata[:steps])
}.delete_if { |k, v| v.nil? || v.empty?}
end


def configure_project_key
self.class.to_s == 'TMJUpdateTestFormatter' ? nil : TMJFormatter.config.project_id
end

def process_steps(example)
return unless example
arr = []
example.each { |s| arr << {"description": s[:step_name]} }
{
"type": "STEP_BY_STEP",
"steps": arr
}
end
end
end
16 changes: 13 additions & 3 deletions lib/tmj_formatter/tasks/test_manipulation_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ def install_tasks

def install
namespace 'tmj' do
desc 'Create test cases with steps on Test Managment For JIRA'
task :create_with_steps, [:path] do |t, args|
exec("bundle exec rspec #{args[:path] if args[:path]} --format TMJCreateTestFormatter --dry-run -r tmj_formatter/example")
end

desc 'Create test cases on Test Managment For JIRA'
task :create, [:path] do |t, args|
exec("bundle exec rspec #{args[:path] if args[:path]} --format TMJCreateTestFormatter --dry-run -r tmj_formatter/example")
exec("bundle exec rspec #{args[:path] if args[:path]} --format TMJCreateTestFormatter --dry-run")
end

desc 'Update test cases with steps on Test Managment For JIRA'
task :update_with_steps, [:path] do |t, args|
exec("bundle exec rspec #{args[:path] if args[:path]} --format TMJUpdateTestFormatter --dry-run -r tmj_formatter/example")
end

desc 'Update test cases on Test Managment For JIRA'
task :update do
puts 'not done'
task :update, [:path] do |t, args|
exec("bundle exec rspec #{args[:path] if args[:path]} --format TMJUpdateTestFormatter --dry-run")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tmj_formatter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TMJFormatter
VERSION = '0.1.11'.freeze
VERSION = '0.1.18'.freeze
end
17 changes: 17 additions & 0 deletions lib/tmj_update_test_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'tmj_formatter/helpers/base_formatter'

class TMJUpdateTestFormatter < TMJFormatter::BaseFormatter
RSpec::Core::Formatters.register self, *NOTIFICATIONS

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

response = @client.TestCase.update(notification.example.metadata[:test_id], process_example(notification.example))
if response.code != 200
puts TMJ::TestCaseError.new(response).message
exit
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
config.include TMJFormatter::Adaptor
config.formatter = 'TMJResultFormatter'
config.formatter = 'TMJResultFormatter' unless RSpec.configuration.dry_run?
config.formatter = 'TMJOutputFormatter'

config.expect_with :rspec do |c|
Expand Down
11 changes: 6 additions & 5 deletions spec/test_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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
OBJECTIVE = 'JUST A TEST'
RSpec.describe 'Test Case:', folder: '/Test' do
it 'Test Example From Local', objective: OBJECTIVE, precondition: PRECONDITION, test_id: 'CC-T1613' do |e|
e.step 'test 1' do
end

e.step 'test 2' do
end
end
end
end

6 changes: 6 additions & 0 deletions spec/with_steps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

RSpec.describe 'Tasks with steps', skip: true do
it 'TODO' do |e|
end
end
6 changes: 6 additions & 0 deletions spec/without_steps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

RSpec.describe 'Tasks without steps', skip: true do
it 'TODO' do |e|
end
end

0 comments on commit 8f8d749

Please sign in to comment.