#!/usr/bin/env ruby # frozen_string_literal: true require 'thor' $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'savon' require 'soaspec' require 'soaspec/virtual_server' require 'soaspec/wsdl_generator' module Soaspec # Common executable for Soaspec class Exe < Thor include Soaspec::ExeHelpers include Soaspec::WsdlGenerator long_desc <<-LONGDESC `soaspec new` will generate the initial files and folders for starting a testing project using soaspec \x5 `soaspec new soap` will create example files testing against a SOAP service \x5 `soaspec new rest` will create example files testing against a REST service LONGDESC desc 'new [type]', 'Initialize soaspec repository' option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :virtual, type: :boolean, default: true, banner: 'Whether to set things up for a virtual server' def new(type = 'initial') @virtual = options[:virtual] @type = type puts "Creating files for soaspec. options are #{options}" create_file(filename: 'Gemfile') create_file(filename: 'Rakefile') create_file(filename: '.rspec') create_file(filename: 'README.md') create_file(filename: '.travis.yml') if options[:ci] == 'travis' create_folder 'lib' create_files_for type create_file(filename: 'config/data/default.yml') # Example of data file create_file(filename: 'spec/spec_helper.rb') puts "Run 'bundle install' to install necessary gems" puts "Run 'rake spec' to run the tests" end long_desc <<-LONGDESC `soaspec add rest` will generate the initial files and folders for starting a testing project using soaspec \x5 `soaspec add soap` will create example files testing against a virtual SOAP service \x5 LONGDESC desc 'add [type] [name]', 'Add new ExchangeHandler' def add(type = 'rest', name = 'TestService') raise "Type '#{type}' is not available" unless %w[rest soap].include? type @name = name # Use instance variable for ERB create_file filename: File.join('lib', "#{name.snakecase}.rb"), content: retrieve_contents(File.join('lib', "new_#{type}_service.rb")) end long_desc <<-LONGDESC `soaspec cucumber` will add generic steps file within step definitions folder. It will also add Gemfile, and other Cucumber setup files if not already added \x5 LONGDESC desc 'cucumber', 'Add cucumber generic steps template within step_definitions folder' def cucumber @cucumber = true create_file(filename: 'Gemfile', ignore_if_present: true) create_file(filename: 'features/support/env.rb', ignore_if_present: true) create_file filename: File.join('features', 'step_definitions', 'generic_steps.rb'), content: retrieve_contents(File.join('../cucumber', 'generic_steps.rb'), false) end desc 'generate', 'Generate initial test code from wsdl' long_desc <<-LONGDESC If no wsdl is given a server will be started from which code to generate Exchanges `soaspec generate wsdl=wsdl name=ServiceName ` will generate the initial files and folders to test each operation in a wsdl \x5 Additionally the auth parameter can be used to use basic authentication to retrieve the WSDL. To do use the following `soaspec generate --auth=basic` Note: This is still a work in progress and will only work for a very simple wsdl LONGDESC option :wsdl, default: nil, aliases: :w option :name, default: 'Service', aliases: :n option :ci, default: 'jenkins', banner: 'What Continuous Integration is used' option :auth option :open_browser, default: 'true' option :string_default, default: 'test string' option :port, default: nil, banner: 'What port to run generate server. Default is random' def generate if options[:wsdl] generate_from_wsdl(options) else require 'launchy' require 'soaspec/generate_server' require 'random-port' port = options[:port] || RandomPort::Pool.new.acquire.to_s puts "Using random port '#{port}' for hosting generate server" Soaspec::GenerateServer.run!(port: port) do Launchy.open "http://localhost:#{port}" if options[:open_browser] == 'true' end end end long_desc <<-LONGDESC Run virtual web services on localhost. See root for documentation of web services provided. LONGDESC desc 'virtual_server [port]', 'Run virtual web service on localhost' def virtual_server(port = '4999') ENV['port'] = port require 'soaspec/virtual_server' Soaspec::VirtualServer.run!(port: port) end long_desc <<-LONGDESC Run pry with Soaspec and other relevant libraries loaded. Pass either the relative path to files desired with a '.rb' extension or the path to the folder to be loaded with 'require_all' LONGDESC desc 'pry [required_files]', 'Run pry with Soaspec and other relevant libraries loaded' def pry(required_files = nil) require 'soaspec' require 'require_all' require_all required_files if required_files require 'pry' Pry.start end end end Soaspec::Exe.start(ARGV)