-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
run-curl-tests.rb
114 lines (84 loc) · 1.66 KB
/
run-curl-tests.rb
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
require 'yaml'
require 'pp'
require 'erb'
require 'systemu'
module ErbBinding
extend self
## create_binding_object
# create a throw-away object to hold parameters as methods
def create_binding_object(params)
o = Object.new
o.instance_eval do
klass = class << self; self; end
# fake
params.each do |key, value|
klass.send(:define_method, key) do value end
end
end
def o.context
self.instance_eval { binding }
end
o
end
## erb
def erb(template, params = { })
o = create_binding_object(params)
ERB.new(template, nil, '%<>').result(o.context)
end
end
$APP_DEBUG = ARGV.delete("--debug")
def dbg(*a)
STDERR.puts a.pretty_inspect if $APP_DEBUG
end
tests = YAML.load(ARGF.read)
dbg tests
want_org = ARGV.delete("--org")
def indent(txt, level = 4)
txt.split(/\n/).map{ |line| (" " * level) + line }.join("\n")
end
if want_org
header = <<EOT
#+BEGIN_HEADER
#+TITLE: curl tests of REST API
#+SETUPFILE: ~/org/setup.org
#+END_HEADER
EOT
template = <<EOT
** <%= title %>
: <%= url %>
#+BEGIN_SRC sh
<%= cmd %>
#+END_SRC
*** Response
#+BEGIN_EXAMPLE
<%= stdout %>
#+END_EXAMPLE
EOT
else
# markdown template
header = <<EOT
# REST API example
The REST API to the example app is described below.
EOT
template = <<EOT
## <%= title %>
### Request
`<%= url %>`
<%= indent(cmd) %>
### Response
<%= indent(stdout) %>
EOT
end
puts header
tests["tests"].each do |title, url, cmd|
status, stdout, stderr = systemu(cmd)
params = {
:title => title,
:url => url,
:cmd => cmd,
:stdout => stdout,
:stderr => stderr,
}
dbg params
puts ErbBinding.erb(template, params)
end