Class: Pacto::RakeTask
- Inherits:
-
Object
- Object
- Pacto::RakeTask
- Includes:
- Rake::DSL
- Defined in:
- lib/pacto/rake_task.rb
Instance Method Summary collapse
-
#generate_contracts(input_dir, output_dir, host) ⇒ Object
FIXME: generate_contracts is a big method =(. Needs refactoring rubocop:disable MethodLength.
- #generate_task ⇒ Object
-
#initialize ⇒ RakeTask
constructor
A new instance of RakeTask.
- #install ⇒ Object
-
#meta_validate ⇒ Object
FIXME: meta_validate is a big method =(. Needs refactoring rubocop:disable MethodLength.
- #validate_contracts(host, dir) ⇒ Object
- #validate_task ⇒ Object
Constructor Details
#initialize ⇒ RakeTask
Returns a new instance of RakeTask.
9 10 11 |
# File 'lib/pacto/rake_task.rb', line 9 def initialize @exit_with_error = false end |
Instance Method Details
#generate_contracts(input_dir, output_dir, host) ⇒ Object
FIXME: generate_contracts is a big method =(. Needs refactoring rubocop:disable MethodLength
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pacto/rake_task.rb', line 95 def generate_contracts(input_dir, output_dir, host) WebMock.allow_net_connect! generator = Pacto::Generator.new puts "Generating contracts from partial contracts in #{input_dir} and recording to #{output_dir}\n\n" failed_contracts = [] each_contract(input_dir) do |contract_file| begin contract = generator.generate(contract_file, host) output_file = File.(File.basename(contract_file), output_dir) output_file = File.open(output_file, 'wb') output_file.write contract output_file.flush output_file.close rescue InvalidContract => e failed_contracts << contract_file puts Pacto::UI.red(e.) end end if failed_contracts.empty? puts Pacto::UI.green('Successfully generated all contracts') else fail Pacto::UI.red("The following contracts could not be generated: #{failed_contracts.join ','}") end end |
#generate_task ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pacto/rake_task.rb', line 33 def generate_task desc 'Generates contracts from partial contracts' task :generate, :input_dir, :output_dir, :host do |t, args| if args.to_a.size < 3 fail Pacto::UI.yellow('USAGE: rake pacto:generate[<request_contract_dir>, <output_dir>, <record_host>]') end generate_contracts(args[:input_dir], args[:output_dir], args[:host]) end end |
#install ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/pacto/rake_task.rb', line 13 def install desc 'Tasks for Pacto gem' namespace :pacto do validate_task generate_task end end |
#meta_validate ⇒ Object
FIXME: meta_validate is a big method =(. Needs refactoring rubocop:disable MethodLength
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pacto/rake_task.rb', line 46 def desc 'Validates a directory of contract definitions' task :meta_validate, :dir do |t, args| if args.to_a.size < 1 fail Pacto::UI.yellow('USAGE: rake pacto:meta_validate[<contract_dir>]') end each_contract(args[:dir]) do |contract_file| fail unless Pacto.validate_contract contract_file end puts 'All contracts successfully meta-validated' end end |
#validate_contracts(host, dir) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pacto/rake_task.rb', line 60 def validate_contracts(host, dir) WebMock.allow_net_connect! puts "Validating contracts in directory #{dir} against host #{host}\n\n" total_failed = 0 contracts = [] each_contract(dir) do |contract_file| contracts << contract_file print "#{contract_file.split('/').last}:" contract = Pacto.load_contract(contract_file, host) errors = contract.validate_provider if errors.empty? puts Pacto::UI.green(' OK!') else @exit_with_error = true total_failed += 1 puts Pacto::UI.red(' FAILED!') errors.each do |error| puts Pacto::UI.red("\t* #{error}") end puts '' end end if @exit_with_error fail Pacto::UI.red("#{total_failed} of #{contracts.size} failed. Check output for detailed error messages.") else puts Pacto::UI.green("#{contracts.size} valid contract#{contracts.size > 1 ? 's' : nil}") end end |
#validate_task ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pacto/rake_task.rb', line 22 def validate_task desc 'Validates all contracts in a given directory against a given host' task :validate, :host, :dir do |t, args| if args.to_a.size < 2 fail Pacto::UI.yellow('USAGE: rake pacto:validate[<host>, <contract_dir>]') end validate_contracts(args[:host], args[:dir]) end end |