1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 91 92 93 94 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/ruby.ruby2.5
require 'optparse'
require 'rubygems'
require_gem 'active_support'
require 'erb'
include ERB::Util
require "ftools"
$LOAD_PATH << File.dirname( $0 )
require "rest.rb"
require "rest_test.rb"
class Options
attr_accessor :verbose, :filename, :html, :outline, :output_dir,
:create_template, :testfile, :force, :request, :show_all, :include_dir,
:output_html
def initialize
verbose = false
html = false
end
def self.parse( args )
options = Options.new
opt = OptionParser.new
opt.banner = "Usage: rest_test [options] filename"
opt.on( "-h", "--help", "Print this message" ) do
puts opt
exit
end
opt.on( "-v", "--verbose", "Verbose mode" ) do
options.verbose = true
end
opt.on( "--create-test", "Create test template" ) do
options.create_template = true
end
opt.on( "--test=testfile", "Select test file" ) do |val|
options.testfile = val
end
opt.on( "--force", "Force overwriting of test file" ) do
options.force = true
end
opt.on( "--request=name", "Select request to be tested" ) do |val|
options.request = val
end
opt.on( "--show-all", "Show all requests, not only non-passed." ) do
options.show_all = true
end
opt.on( "--output-html", "Put out results formatted as HTML." ) do
options.output_html = true
end
opt.on( "-I", "=DIRECTORY", "Include directory as search path for XML files") do |val|
options.include_dir = val
end
begin
opt.parse!( args )
rescue OptionParser::InvalidOption
STDERR.puts $!
STDERR.puts opt
exit 1
end
if ( ARGV.size > 1 )
STDERR.puts "Too many arguments"
STDERR.puts opt
exit 1
elsif ( ARGV.size < 1 )
STDERR.puts "Too few arguments"
STDERR.puts opt
exit 1
end
options.filename = ARGV[0]
options
end
end
options = Options.parse( ARGV )
begin
XmlFile.include_dir = options.include_dir
if ( !options.testfile )
options.testfile = options.filename + ".test"
end
document = Document.new
document.parse_args
@requests = document.all_children Request
if ( !File.exists?( options.testfile ) || options.create_template )
if ( File.exists?( options.testfile ) )
if ( !options.force )
STDERR.puts "Test file '#{options.testfile} already exists." +
" Use '--force' to overwrite it. Exiting."
return
end
puts "Overwriting test file '#{options.testfile}'."
else
puts "Creating test file '#{options.testfile}'."
end
File.open( options.testfile, "w" ) do |file|
@requests.each do |r|
file.puts "request \"#{r}\""
end
end
end
puts "Working on testfile #{options.testfile}."
runner = TestRunner.new @requests
configfile = options.testfile + ".config"
if ( File.exists? configfile )
File.open configfile do |file|
eval file.read, runner.context.get_binding
end
end
if ( options.verbose )
runner.context.show_xmlbody = true
end
runner.context.show_passed = options.show_all
runner.context.request_filter = options.request
runner.context.output_html = options.output_html
runner.run options.testfile
rescue Errno::ENOENT
puts $!
end
|