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 # frozen_string_literal: true
require 'faraday' module Provider
module Environments
class Base
attr_writer :base_url, :merge_request
module Environments def call(env)
class Base @payload
attr_writer :base_url, :merge_request end
def call(env)
@payload
end
def http(endpoint) def http(endpoint)
Faraday.default_adapter = :net_http Faraday.default_adapter = :net_http
response = Faraday.get(@base_url + endpoint) response = Faraday.get(@base_url + endpoint)
@payload = [response.status, response.headers, [response.body]] @payload = [response.status, response.headers, [response.body]]
self self
end end
def merge_request(endpoint) def merge_request(endpoint)
if endpoint.include? '.json' http(@merge_request + endpoint) if endpoint.include? '.json'
http(@merge_request + endpoint)
end end
end end
end end
......
# frozen_string_literal: true # frozen_string_literal: true
require_relative './base' module Provider
module Environments
module Environments class Local < Base
class Local < Base def initialize
def initialize @base_url = ENV['CONTRACT_HOST']
@base_url = ENV['CONTRACT_HOST'] @merge_request = ENV['CONTRACT_MR']
@merge_request = ENV['CONTRACT_MR'] end
end end
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../environments/local' require_relative '../spec_helper'
module DiffsHelper module Provider
local = Environments::Local.new module DiffsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Diffs Endpoint" do Pact.service_provider "Merge Request Diffs Endpoint" do
app { local.merge_request('/diffs_batch.json?page=0') } app { local.merge_request('/diffs_batch.json?page=0') }
honours_pact_with 'Merge Request Page' do honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json' pact_uri '../contracts/merge_request_page-merge_request_diffs_endpoint.json'
end
end end
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../environments/local' require_relative '../spec_helper'
module DiscussionsHelper module Provider
local = Environments::Local.new module DiscussionsHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Discussions Endpoint" do Pact.service_provider "Merge Request Discussions Endpoint" do
app { local.merge_request('/discussions.json') } app { local.merge_request('/discussions.json') }
honours_pact_with 'Merge Request Page' do honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json' pact_uri '../contracts/merge_request_page-merge_request_discussions_endpoint.json'
end
end end
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
require_relative '../environments/local' require_relative '../spec_helper'
module MetadataHelper module Provider
local = Environments::Local.new module MetadataHelper
local = Environments::Local.new
Pact.service_provider "Merge Request Metadata Endpoint" do Pact.service_provider "Merge Request Metadata Endpoint" do
app { local.merge_request('/diffs_metadata.json') } app { local.merge_request('/diffs_metadata.json') }
honours_pact_with 'Merge Request Page' do honours_pact_with 'Merge Request Page' do
pact_uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json' pact_uri '../contracts/merge_request_page-merge_request_metadata_endpoint.json'
end
end end
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