diff --git a/lib/tmj_create_test_formatter.rb b/lib/tmj_create_test_formatter.rb index 793b940..8bf72d5 100644 --- a/lib/tmj_create_test_formatter.rb +++ b/lib/tmj_create_test_formatter.rb @@ -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) @@ -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. \ No newline at end of file diff --git a/lib/tmj_formatter.rb b/lib/tmj_formatter.rb index d9adf43..d822a8b 100755 --- a/lib/tmj_formatter.rb +++ b/lib/tmj_formatter.rb @@ -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' diff --git a/lib/tmj_formatter/helpers/base_formatter.rb b/lib/tmj_formatter/helpers/base_formatter.rb new file mode 100644 index 0000000..89cf9ef --- /dev/null +++ b/lib/tmj_formatter/helpers/base_formatter.rb @@ -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 \ No newline at end of file diff --git a/lib/tmj_formatter/tasks/test_manipulation_tasks.rb b/lib/tmj_formatter/tasks/test_manipulation_tasks.rb index 985c607..148dee7 100644 --- a/lib/tmj_formatter/tasks/test_manipulation_tasks.rb +++ b/lib/tmj_formatter/tasks/test_manipulation_tasks.rb @@ -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 diff --git a/lib/tmj_formatter/version.rb b/lib/tmj_formatter/version.rb index 73f65fa..b4e7a12 100755 --- a/lib/tmj_formatter/version.rb +++ b/lib/tmj_formatter/version.rb @@ -1,3 +1,3 @@ module TMJFormatter - VERSION = '0.1.11'.freeze + VERSION = '0.1.18'.freeze end diff --git a/lib/tmj_update_test_formatter.rb b/lib/tmj_update_test_formatter.rb new file mode 100644 index 0000000..6687427 --- /dev/null +++ b/lib/tmj_update_test_formatter.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5434965..68716a6 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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| diff --git a/spec/test_spec.rb b/spec/test_spec.rb index 235a273..14ed1b2 100644 --- a/spec/test_spec.rb +++ b/spec/test_spec.rb @@ -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 \ No newline at end of file +end + diff --git a/spec/with_steps_spec.rb b/spec/with_steps_spec.rb new file mode 100644 index 0000000..99682bc --- /dev/null +++ b/spec/with_steps_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +RSpec.describe 'Tasks with steps', skip: true do + it 'TODO' do |e| + end +end \ No newline at end of file diff --git a/spec/without_steps_spec.rb b/spec/without_steps_spec.rb new file mode 100644 index 0000000..62b0f62 --- /dev/null +++ b/spec/without_steps_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +RSpec.describe 'Tasks without steps', skip: true do + it 'TODO' do |e| + end +end