Commit d024806e authored by Andrejs Cunskis's avatar Andrejs Cunskis

Use main Rakefile for contracts test definition

Handle task errors

Remove pact from the main test task name
parent af46b2c9
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'rake'
host = ARGV.shift
ENV['CONTRACT_HOST'] ||= host
list = []
loop do
keyword = ARGV.shift
case keyword
when '--mr'
ENV['CONTRACT_MR'] ||= ARGV.shift
list.push 'test:merge_request'
else
break
end
end
app = Rake.application
Dir.chdir('contracts/provider') do
app.init
app.add_import 'Rakefile'
app.load_rakefile
list.each do |element|
app[element].invoke
end
end
# frozen_string_literal: true
require 'pact/tasks/verification_task'
Pact::VerificationTask.new(:metadata) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json', pact_helper: './spec/metadata_helper.rb'
end
Pact::VerificationTask.new(:discussions) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json', pact_helper: './spec/discussions_helper.rb'
end
Pact::VerificationTask.new(:diffs) do |pact|
pact.uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json', pact_helper: './spec/diffs_helper.rb'
end
task 'test:merge_request' => ['pact:verify:metadata', 'pact:verify:discussions', 'pact:verify:diffs']
# frozen_string_literal: true
require 'faraday'
module Environments
module Provider
module Environments
class Base
attr_writer :base_url, :merge_request
......@@ -18,8 +17,7 @@ module Environments
end
def merge_request(endpoint)
if endpoint.include? '.json'
http(@merge_request + endpoint)
http(@merge_request + endpoint) if endpoint.include? '.json'
end
end
end
......
# frozen_string_literal: true
require_relative './base'
module Environments
module Provider
module Environments
class Local < Base
def initialize
@base_url = ENV['CONTRACT_HOST']
@merge_request = ENV['CONTRACT_MR']
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module DiffsHelper
module Provider
module DiffsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Diffs Endpoint" do
......@@ -12,4 +13,5 @@ module DiffsHelper
pact_uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json'
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module DiscussionsHelper
module Provider
module DiscussionsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Discussions Endpoint" do
......@@ -12,4 +13,5 @@ module DiscussionsHelper
pact_uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json'
end
end
end
end
# frozen_string_literal: true
require_relative '../environments/local'
require_relative '../spec_helper'
module MetadataHelper
module Provider
module MetadataHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Metadata Endpoint" do
......@@ -12,4 +13,5 @@ module MetadataHelper
pact_uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json'
end
end
end
end
# frozen_string_literal: true
module SpecHelper
unless ENV['CONTRACT_HOST']
raise(ArgumentError, 'Contract tests require CONTRACT_HOST environment variable to be set!')
end
require_relative '../../../config/bundler_setup'
Bundler.require(:default)
root = File.expand_path('../', __dir__)
loader = Zeitwerk::Loader.new
loader.push_dir(root)
loader.ignore("#{root}/consumer")
loader.ignore("#{root}/contracts")
loader.collapse("#{root}/provider/spec")
loader.setup
end
# frozen_string_literal: true
require 'pact/tasks/verification_task'
contracts = File.expand_path('../contracts', __dir__)
provider = File.expand_path('provider', contracts)
# rubocop:disable Rails/RakeEnvironment
namespace :contracts do
namespace :mr do
Pact::VerificationTask.new(:metadata) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_metadata_endpoint.json",
pact_helper: "#{provider}/spec/metadata_helper.rb"
)
end
Pact::VerificationTask.new(:discussions) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_discussions_endpoint.json",
pact_helper: "#{provider}/spec/discussions_helper.rb"
)
end
Pact::VerificationTask.new(:diffs) do |pact|
pact.uri(
"#{contracts}/contracts/merge_request_page-merge_request_diffs_endpoint.json",
pact_helper: "#{provider}/spec/diffs_helper.rb"
)
end
desc 'Run all merge request contract tests'
task 'test:merge_request', :contract_mr do |_t, arg|
raise(ArgumentError, 'Merge request contract tests require contract_mr to be set') unless arg[:contract_mr]
ENV['CONTRACT_MR'] = arg[:contract_mr]
errors = %w[metadata discussions diffs].each_with_object([]) do |task, err|
Rake::Task["contracts:mr:pact:verify:#{task}"].execute
rescue StandardError, SystemExit
err << "contracts:mr:pact:verify:#{task}"
end
raise StandardError, "Errors in tasks #{errors.join(', ')}" unless errors.empty?
end
end
end
# rubocop:enable Rails/RakeEnvironment
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment