cmds 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/Rakefile +2 -2
- data/lib/cmds/debug.rb +2 -2
- data/lib/cmds/erb_context.rb +4 -2
- data/lib/cmds/io_handler.rb +2 -2
- data/lib/cmds/pipe.rb +1 -1
- data/lib/cmds/result.rb +1 -1
- data/lib/cmds/shell_eruby.rb +3 -3
- data/lib/cmds/spawn.rb +1 -1
- data/lib/cmds/sugar.rb +10 -10
- data/lib/cmds/util/defaults.rb +1 -1
- data/lib/cmds/util/params.rb +1 -1
- data/lib/cmds/util/tokenize_option.rb +1 -1
- data/lib/cmds/util/tokenize_options.rb +1 -1
- data/lib/cmds/util.rb +24 -14
- data/lib/cmds/version.rb +2 -2
- data/lib/cmds.rb +406 -1
- data/scratch/proxy.rb +1 -1
- data/spec/cmds/assert_spec.rb +1 -1
- data/spec/cmds/capture_spec.rb +7 -7
- data/spec/cmds/chomp_spec.rb +5 -5
- data/spec/cmds/curry_spec.rb +1 -1
- data/spec/cmds/env_spec.rb +3 -3
- data/spec/cmds/err_spec.rb +2 -2
- data/spec/cmds/error_spec.rb +2 -2
- data/spec/cmds/ok_spec.rb +2 -2
- data/spec/cmds/out_spec.rb +5 -5
- data/spec/cmds/prepare_spec.rb +10 -2
- data/spec/cmds/stream_spec.rb +4 -4
- data/spec/cmds_spec.rb +1 -1
- metadata +1 -2
- data/lib/cmds/cmd.rb +0 -411
data/lib/cmds.rb
CHANGED
@@ -1,3 +1,408 @@
|
|
1
|
+
# deps
|
2
|
+
require 'nrser'
|
3
|
+
|
4
|
+
# project
|
1
5
|
require 'cmds/version'
|
2
|
-
require 'cmds/
|
6
|
+
require 'cmds/debug'
|
7
|
+
require 'cmds/util'
|
8
|
+
require 'cmds/spawn'
|
9
|
+
require 'cmds/erb_context'
|
10
|
+
require 'cmds/shell_eruby'
|
11
|
+
require 'cmds/result'
|
3
12
|
require 'cmds/sugar'
|
13
|
+
|
14
|
+
class Cmds
|
15
|
+
# ERB stirng template (with Cmds-specific extensions) for the command.
|
16
|
+
#
|
17
|
+
# @return [String]
|
18
|
+
attr_reader :template
|
19
|
+
|
20
|
+
# base/common positional parameters to render into the command
|
21
|
+
# template.
|
22
|
+
#
|
23
|
+
# defaults to `[]`.
|
24
|
+
#
|
25
|
+
# {#prepare} and the methods that invoke it (like {#capture},
|
26
|
+
# {#stream}, etc.) accept `*args`, which will be appended to
|
27
|
+
# these values to create the final array for rendering.
|
28
|
+
#
|
29
|
+
# @return [Array<Object>]
|
30
|
+
attr_reader :args
|
31
|
+
|
32
|
+
# base/common keyword parameters to render into the command template.
|
33
|
+
#
|
34
|
+
# defaults to `{}`.
|
35
|
+
#
|
36
|
+
# {#prepare} and the methods that invoke it (like {#capture},
|
37
|
+
# {#stream}, etc.) accept `**kwds`, which will be merged on top of
|
38
|
+
# these values to create the final hash for rendering.
|
39
|
+
#
|
40
|
+
# @return [Hash{Symbol => Object}]
|
41
|
+
attr_reader :kwds
|
42
|
+
|
43
|
+
# string or readable IO-like object to use as default input to the
|
44
|
+
# command.
|
45
|
+
#
|
46
|
+
# {#prepare} and the methods that invoke it (like {#capture},
|
47
|
+
# {#stream}, etc.) accept an optional block that will override this
|
48
|
+
# value if present.
|
49
|
+
#
|
50
|
+
# @return [String | #read]
|
51
|
+
attr_reader :input
|
52
|
+
|
53
|
+
# if `true`, will execution will raise an error on non-zero exit code.
|
54
|
+
#
|
55
|
+
# defaults to `false`.
|
56
|
+
#
|
57
|
+
# @return [Boolean]
|
58
|
+
attr_reader :assert
|
59
|
+
|
60
|
+
# environment variables to set for command execution.
|
61
|
+
#
|
62
|
+
# defaults to `{}`.
|
63
|
+
#
|
64
|
+
# @return [Hash{String | Symbol => String}]
|
65
|
+
attr_reader :env
|
66
|
+
|
67
|
+
# format specifier symbol:
|
68
|
+
#
|
69
|
+
# - `:squish`
|
70
|
+
# - collapse rendered command string to one line.
|
71
|
+
# - `:pretty`
|
72
|
+
# - clean up and backslash suffix line endings.
|
73
|
+
#
|
74
|
+
# defaults to `:squish`.
|
75
|
+
#
|
76
|
+
# @return [:squish | :pretty]
|
77
|
+
attr_reader :format
|
78
|
+
|
79
|
+
|
80
|
+
# directory to run the command in.
|
81
|
+
#
|
82
|
+
# @return [nil | String]
|
83
|
+
attr_reader :chdir
|
84
|
+
|
85
|
+
|
86
|
+
# construct a Cmd.
|
87
|
+
#
|
88
|
+
# @param [String] template
|
89
|
+
# sets the {#template} attribute.
|
90
|
+
#
|
91
|
+
# @param [Hash] opts
|
92
|
+
#
|
93
|
+
# @option opts [Array<Object>] :args
|
94
|
+
# sets the {#args} attribute.
|
95
|
+
#
|
96
|
+
# @option opts [Hash{Symbol => Object}] :kwds
|
97
|
+
# sets the {#kwds} attribute.
|
98
|
+
#
|
99
|
+
# @option opts [String | #read] :input
|
100
|
+
# sets the {#input} attribute.
|
101
|
+
#
|
102
|
+
# @option opts [Hash{Symbol => String}] :env
|
103
|
+
# sets the {#env} attribute.
|
104
|
+
#
|
105
|
+
# @option opts [:squish, :pretty] :format
|
106
|
+
# sets the {#format} attribute.
|
107
|
+
#
|
108
|
+
# @option opts [nil | String] :chdir
|
109
|
+
# sets the {#chdir} attribute.
|
110
|
+
#
|
111
|
+
def initialize template, **opts
|
112
|
+
Cmds.debug "Cmd constructing...",
|
113
|
+
template: template,
|
114
|
+
opts: opts
|
115
|
+
|
116
|
+
@template = template
|
117
|
+
@args = (opts[:args] || []).freeze
|
118
|
+
@kwds = (opts[:kwds] || {}).freeze
|
119
|
+
@input = opts[:input] || nil
|
120
|
+
@assert = opts[:assert] || false
|
121
|
+
@env = (opts[:env] || {}).freeze
|
122
|
+
@format = opts[:format] || :squish
|
123
|
+
@env_mode = opts[:env_mode] || :inline
|
124
|
+
@chdir = opts[:chdir] || nil
|
125
|
+
end # #initialize
|
126
|
+
|
127
|
+
|
128
|
+
# returns a new {Cmd} with the parameters and input merged in
|
129
|
+
def curry *args, **kwds, &input_block
|
130
|
+
self.class.new @template, {
|
131
|
+
args: (@args + args),
|
132
|
+
kwds: (@kwds.merge kwds),
|
133
|
+
input: (input ? input.call : @input),
|
134
|
+
assert: @assert,
|
135
|
+
env: @env,
|
136
|
+
format: @format,
|
137
|
+
chdir: @chdir,
|
138
|
+
}
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
# render parameters into `@template`.
|
143
|
+
#
|
144
|
+
# @note the returned string is **not** formatted for shell execution.
|
145
|
+
# Cmds passes this string through {Cmds.format} before execution,
|
146
|
+
# which addresses newlines in the rendered string through "squishing"
|
147
|
+
# everything down to one line or adding `\` to line ends.
|
148
|
+
#
|
149
|
+
# @param args (see #capture)
|
150
|
+
# @param kwds (see #capture)
|
151
|
+
#
|
152
|
+
# @return [String]
|
153
|
+
# the rendered command string.
|
154
|
+
#
|
155
|
+
def render *args, **kwds
|
156
|
+
context = Cmds::ERBContext.new((@args + args), @kwds.merge(kwds))
|
157
|
+
erb = Cmds::ShellEruby.new Cmds.replace_shortcuts(@template)
|
158
|
+
rendered = NRSER.dedent erb.result(context.get_binding)
|
159
|
+
|
160
|
+
if @env_mode == :inline && !@env.empty?
|
161
|
+
rendered = @env.sort_by {|name, value|
|
162
|
+
name
|
163
|
+
}.map {|name, value|
|
164
|
+
"#{ name }=#{ Cmds.esc value }"
|
165
|
+
}.join("\n\n") + "\n\n" + rendered
|
166
|
+
end
|
167
|
+
|
168
|
+
rendered
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
# prepare a shell-safe command string for execution.
|
173
|
+
#
|
174
|
+
# @param args (see #capture)
|
175
|
+
# @param kwds (see #capture)
|
176
|
+
#
|
177
|
+
# @return [String]
|
178
|
+
# the prepared command string.
|
179
|
+
#
|
180
|
+
def prepare *args, **kwds
|
181
|
+
Cmds.format render(*args, **kwds), @format
|
182
|
+
end # #prepare
|
183
|
+
|
184
|
+
|
185
|
+
def stream *args, **kwds, &io_block
|
186
|
+
Cmds.debug "entering Cmd#stream",
|
187
|
+
args: args,
|
188
|
+
kwds: kwds,
|
189
|
+
io_block: io_block
|
190
|
+
|
191
|
+
Cmds.spawn prepare(*args, **kwds),
|
192
|
+
input: @input,
|
193
|
+
# include env if mode is spawn argument
|
194
|
+
env: (@env_mode == :spawn_arg ? @env : {}),
|
195
|
+
chdir: @chdir,
|
196
|
+
&io_block
|
197
|
+
end # #stream
|
198
|
+
|
199
|
+
|
200
|
+
# executes the command and returns a {Cmds::Result} with the captured
|
201
|
+
# outputs.
|
202
|
+
#
|
203
|
+
# @param [Array<Object>] args
|
204
|
+
# positional parameters to append to those in `@args` for rendering
|
205
|
+
# into the command string.
|
206
|
+
#
|
207
|
+
# @param [Hash{Symbol => Object}] kwds
|
208
|
+
# keyword parameters that override those in `@kwds` for rendering
|
209
|
+
# into the command string.
|
210
|
+
#
|
211
|
+
# @param [#call] input_block
|
212
|
+
# optional block that returns a string or readable object to override
|
213
|
+
# `@input`.
|
214
|
+
#
|
215
|
+
# @return [Cmds::Result]
|
216
|
+
# result of execution with command string, status, stdout and stderr.
|
217
|
+
#
|
218
|
+
def capture *args, **kwds, &input_block
|
219
|
+
Cmds.debug "entering Cmds#capture",
|
220
|
+
args: args,
|
221
|
+
kwds: kwds,
|
222
|
+
input: input
|
223
|
+
|
224
|
+
# prepare the command string
|
225
|
+
cmd = prepare *args, **kwds
|
226
|
+
|
227
|
+
# extract input from block via `call` if one is provided,
|
228
|
+
# otherwise default to instance variable (which may be `nil`)
|
229
|
+
input = input_block.nil? ? @input : input_block.call
|
230
|
+
|
231
|
+
Cmds.debug "prepared",
|
232
|
+
cmd: cmd,
|
233
|
+
input: input
|
234
|
+
|
235
|
+
# strings output will be concatenated onto
|
236
|
+
out = ''
|
237
|
+
err = ''
|
238
|
+
|
239
|
+
Cmds.debug "calling Cmds.spawn..."
|
240
|
+
|
241
|
+
status = Cmds.spawn(
|
242
|
+
cmd,
|
243
|
+
# include env if mode is spawn argument
|
244
|
+
env: (@env_mode == :spawn_arg ? @env : {}),
|
245
|
+
chdir: @chdir
|
246
|
+
) do |io|
|
247
|
+
# send the input to stream, which sends it to spawn
|
248
|
+
io.in = input
|
249
|
+
|
250
|
+
# and concat the output lines as they come in
|
251
|
+
io.on_out do |line|
|
252
|
+
out += line
|
253
|
+
end
|
254
|
+
|
255
|
+
io.on_err do |line|
|
256
|
+
err += line
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
Cmds.debug "Cmds.spawn completed",
|
261
|
+
status: status
|
262
|
+
|
263
|
+
# build a Result
|
264
|
+
# result = Cmds::Result.new cmd, status, out_reader.value, err_reader.value
|
265
|
+
result = Cmds::Result.new cmd, status, out, err
|
266
|
+
|
267
|
+
# tell the Result to assert if the Cmds has been told to, which will
|
268
|
+
# raise a SystemCallError with the exit status if it was non-zero
|
269
|
+
result.assert if @assert
|
270
|
+
|
271
|
+
return result
|
272
|
+
end # #capture
|
273
|
+
|
274
|
+
|
275
|
+
alias_method :call, :capture
|
276
|
+
|
277
|
+
|
278
|
+
# execute command and return `true` if it exited successfully.
|
279
|
+
#
|
280
|
+
# @param *args (see #capture)
|
281
|
+
# @param **kwds (see #capture)
|
282
|
+
# @param &input_block (see #capture)
|
283
|
+
#
|
284
|
+
# @return [Boolean]
|
285
|
+
# `true` if exit code was `0`.
|
286
|
+
#
|
287
|
+
def ok? *args, **kwds, &io_block
|
288
|
+
stream(*args, **kwds, &io_block) == 0
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
# execute command and return `true` if it failed.
|
293
|
+
#
|
294
|
+
# @param *args (see #capture)
|
295
|
+
# @param **kwds (see #capture)
|
296
|
+
# @param &input_block (see #capture)
|
297
|
+
#
|
298
|
+
# @return [Boolean]
|
299
|
+
# `true` if exit code was not `0`.
|
300
|
+
#
|
301
|
+
def error? *args, **kwds, &io_block
|
302
|
+
stream(*args, **kwds, &io_block) != 0
|
303
|
+
end
|
304
|
+
|
305
|
+
|
306
|
+
# def assert
|
307
|
+
# capture.raise_error
|
308
|
+
# end
|
309
|
+
|
310
|
+
|
311
|
+
def proxy
|
312
|
+
stream do |io|
|
313
|
+
io.in = $stdin
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
|
318
|
+
# captures and returns stdout
|
319
|
+
# (sugar for `#capture(*args, **kwds, &input_block).out`).
|
320
|
+
#
|
321
|
+
# @see #capture
|
322
|
+
# @see Result#out
|
323
|
+
#
|
324
|
+
# @param *args (see #capture)
|
325
|
+
# @param **kwds (see #capture)
|
326
|
+
# @param &input_block (see #capture)
|
327
|
+
#
|
328
|
+
# @return [String]
|
329
|
+
# the command's stdout.
|
330
|
+
#
|
331
|
+
def out *args, **kwds, &input_block
|
332
|
+
capture(*args, **kwds, &input_block).out
|
333
|
+
end
|
334
|
+
|
335
|
+
|
336
|
+
# captures and returns stdout
|
337
|
+
# (sugar for `#capture(*args, **kwds, &input_block).out`).
|
338
|
+
#
|
339
|
+
# @see #capture
|
340
|
+
# @see Result#out
|
341
|
+
#
|
342
|
+
# @param args [Array] see {.capture}.
|
343
|
+
# @param kwds [Proc] see {.capture}.
|
344
|
+
#
|
345
|
+
# @return [String] the command's stdout.
|
346
|
+
#
|
347
|
+
# @raise [SystemCallError] if the command fails (non-zero exit status).
|
348
|
+
#
|
349
|
+
def out! *args, **kwds, &input
|
350
|
+
capture(*args, **kwds, &input).assert.out
|
351
|
+
end
|
352
|
+
|
353
|
+
|
354
|
+
# captures and chomps stdout
|
355
|
+
# (sugar for `#out(*subs, &input_block).chomp`).
|
356
|
+
#
|
357
|
+
# @see #out
|
358
|
+
#
|
359
|
+
# @param *args (see #capture)
|
360
|
+
# @param **kwds (see #capture)
|
361
|
+
# @param &input_block (see #capture)
|
362
|
+
#
|
363
|
+
# @return [String]
|
364
|
+
# the command's chomped stdout.
|
365
|
+
#
|
366
|
+
def chomp *args, **kwds, &input_block
|
367
|
+
out(*args, **kwds, &input_block).chomp
|
368
|
+
end
|
369
|
+
|
370
|
+
|
371
|
+
# captures and chomps stdout, raising an error if the command failed.
|
372
|
+
# (sugar for `#out!(*subs, &input_block).chomp`).
|
373
|
+
#
|
374
|
+
# @see #capture
|
375
|
+
# @see Result#out
|
376
|
+
#
|
377
|
+
# @param *args (see #capture)
|
378
|
+
# @param **kwds (see #capture)
|
379
|
+
# @param &input_block (see #capture)
|
380
|
+
#
|
381
|
+
# @return [String]
|
382
|
+
# the command's chomped stdout.
|
383
|
+
#
|
384
|
+
# @raise [SystemCallError]
|
385
|
+
# if the command fails (non-zero exit status).
|
386
|
+
#
|
387
|
+
def chomp! *args, **kwds, &input
|
388
|
+
out!(*args, **kwds, &input).chomp
|
389
|
+
end
|
390
|
+
|
391
|
+
|
392
|
+
# captures and returns stdout
|
393
|
+
# (sugar for `#capture(*subs, &input_block).err`).
|
394
|
+
#
|
395
|
+
# @param *args (see #capture)
|
396
|
+
# @param **kwds (see #capture)
|
397
|
+
# @param &input_block (see #capture)
|
398
|
+
#
|
399
|
+
# @see #capture
|
400
|
+
# @see Result#err
|
401
|
+
#
|
402
|
+
# @return [String]
|
403
|
+
# the command's stderr.
|
404
|
+
#
|
405
|
+
def err *args, **kwds, &input_block
|
406
|
+
capture(*args, **kwds, &input_block).err
|
407
|
+
end
|
408
|
+
end # Cmds
|
data/scratch/proxy.rb
CHANGED
data/spec/cmds/assert_spec.rb
CHANGED
@@ -11,6 +11,6 @@ describe "Cmds::assert" do
|
|
11
11
|
|
12
12
|
it "should be chainable when the command is ok" do
|
13
13
|
expect( Cmds!("echo hey").out ).to eq "hey\n"
|
14
|
-
expect( Cmds
|
14
|
+
expect( Cmds.new("echo hey").capture.assert.out ).to eq "hey\n"
|
15
15
|
end
|
16
16
|
end # Cmds::run
|
data/spec/cmds/capture_spec.rb
CHANGED
@@ -3,13 +3,13 @@ require 'spec_helper'
|
|
3
3
|
describe "Cmds::capture" do
|
4
4
|
it "captures stdout" do
|
5
5
|
expect(
|
6
|
-
Cmds
|
6
|
+
Cmds.new(%{ruby -e '$stdout.puts "hey"'}).capture.out
|
7
7
|
).to eq "hey\n"
|
8
8
|
end
|
9
9
|
|
10
10
|
it "captures stderr" do
|
11
11
|
expect(
|
12
|
-
Cmds
|
12
|
+
Cmds.new(%{ruby -e '$stderr.puts "ho"'}).capture.err
|
13
13
|
).to eq "ho\n"
|
14
14
|
end
|
15
15
|
|
@@ -42,8 +42,8 @@ describe "Cmds::capture" do
|
|
42
42
|
end # context echo_cmd.rb 'hello world!'
|
43
43
|
|
44
44
|
it "is reusable" do
|
45
|
-
args_cmd = Cmds
|
46
|
-
kwds_cmd = Cmds
|
45
|
+
args_cmd = Cmds.new "./test/echo_cmd.rb <%= arg %>"
|
46
|
+
kwds_cmd = Cmds.new "./test/echo_cmd.rb <%= s %>"
|
47
47
|
|
48
48
|
args = ["arg one", "arg two", "arg three"]
|
49
49
|
|
@@ -70,12 +70,12 @@ describe "Cmds::capture" do
|
|
70
70
|
}
|
71
71
|
|
72
72
|
it "accepts input via options" do
|
73
|
-
cmd = Cmds
|
73
|
+
cmd = Cmds.new(ECHO_CMD, input: input)
|
74
74
|
expect( echo_cmd_stdin cmd.capture ).to eq input
|
75
75
|
end
|
76
76
|
|
77
77
|
it "accepts input via block" do
|
78
|
-
cmd = Cmds
|
78
|
+
cmd = Cmds.new ECHO_CMD
|
79
79
|
expect( echo_cmd_stdin cmd.capture { input } ).to eq input
|
80
80
|
end
|
81
81
|
|
@@ -84,7 +84,7 @@ describe "Cmds::capture" do
|
|
84
84
|
input = f.read
|
85
85
|
f.rewind
|
86
86
|
|
87
|
-
cmd = Cmds
|
87
|
+
cmd = Cmds.new ECHO_CMD
|
88
88
|
expect( echo_cmd_stdin cmd.capture { f } ).to eq input
|
89
89
|
end
|
90
90
|
end
|
data/spec/cmds/chomp_spec.rb
CHANGED
@@ -34,12 +34,12 @@ end # Cmds.chomp!
|
|
34
34
|
|
35
35
|
describe "Cmds#chomp" do
|
36
36
|
it "gets echo output" do
|
37
|
-
expect( Cmds
|
37
|
+
expect( Cmds.new("echo %s").chomp "hey there!" ).to eq "hey there!"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "reads input" do
|
41
41
|
expect(
|
42
|
-
Cmds
|
42
|
+
Cmds.new("ruby -e %{script}").chomp(script: "puts STDIN.read") {
|
43
43
|
"hey there!"
|
44
44
|
}
|
45
45
|
).to eq "hey there!"
|
@@ -48,18 +48,18 @@ end # Cmds#chomp
|
|
48
48
|
|
49
49
|
describe "Cmds#chomp!" do
|
50
50
|
it "gets echo output" do
|
51
|
-
expect( Cmds
|
51
|
+
expect( Cmds.new("echo %s").chomp! "hey there!" ).to eq "hey there!"
|
52
52
|
end
|
53
53
|
|
54
54
|
it "reads input" do
|
55
55
|
expect(
|
56
|
-
Cmds
|
56
|
+
Cmds.new("ruby -e %{script}").chomp!(script: "puts STDIN.read") {
|
57
57
|
"hey there!"
|
58
58
|
}
|
59
59
|
).to eq "hey there!"
|
60
60
|
end
|
61
61
|
|
62
62
|
it "errors when the command fails" do
|
63
|
-
expect { Cmds
|
63
|
+
expect { Cmds.new("false").chomp! }.to raise_error SystemCallError
|
64
64
|
end
|
65
65
|
end # Cmds#chomp!
|
data/spec/cmds/curry_spec.rb
CHANGED
data/spec/cmds/env_spec.rb
CHANGED
@@ -4,17 +4,17 @@ describe "Cmds ENV vars" do
|
|
4
4
|
r_echo_cmd = %{ruby -e "puts ENV['BLAH']"}
|
5
5
|
|
6
6
|
it "sets basic (path-like) string ENV var" do
|
7
|
-
cmd = Cmds
|
7
|
+
cmd = Cmds.new r_echo_cmd, env: {BLAH: "x:y:z"}
|
8
8
|
expect(cmd.chomp!).to eq "x:y:z"
|
9
9
|
end
|
10
10
|
|
11
11
|
it "sets a string with spaces in it correctly" do
|
12
|
-
cmd = Cmds
|
12
|
+
cmd = Cmds.new r_echo_cmd, env: {BLAH: "hey there"}
|
13
13
|
expect(cmd.chomp!).to eq "hey there"
|
14
14
|
end
|
15
15
|
|
16
16
|
it "accepts string keys" do
|
17
|
-
cmd = Cmds
|
17
|
+
cmd = Cmds.new r_echo_cmd, env: {
|
18
18
|
'BLAH' => [
|
19
19
|
"/usr/local/bin",
|
20
20
|
"/usr/bin",
|
data/spec/cmds/err_spec.rb
CHANGED
@@ -17,13 +17,13 @@ end # Cmds.err
|
|
17
17
|
describe "Cmds#err" do
|
18
18
|
it "gets echo error output" do
|
19
19
|
expect(
|
20
|
-
Cmds
|
20
|
+
Cmds.new("echo %s 1>&2").err "hey there!"
|
21
21
|
).to eq "hey there!\n"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "reads input" do
|
25
25
|
expect(
|
26
|
-
Cmds
|
26
|
+
Cmds.new("ruby -e %{script}").
|
27
27
|
err(script: "$stderr.puts STDIN.read") {
|
28
28
|
"hey there!"
|
29
29
|
}
|
data/spec/cmds/error_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds.error?" do
|
4
4
|
it "works through instance method" do
|
5
|
-
expect( Cmds
|
6
|
-
expect( Cmds
|
5
|
+
expect( Cmds.new("true").error? ).to be false
|
6
|
+
expect( Cmds.new("false").error? ).to be true
|
7
7
|
end
|
8
8
|
|
9
9
|
it "works through class method" do
|
data/spec/cmds/ok_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Cmds::ok?" do
|
4
4
|
it "works through instance method" do
|
5
|
-
expect( Cmds
|
6
|
-
expect( Cmds
|
5
|
+
expect( Cmds.new("true").ok? ).to be true
|
6
|
+
expect( Cmds.new("false").ok? ).to be false
|
7
7
|
end
|
8
8
|
|
9
9
|
it "works through class method" do
|
data/spec/cmds/out_spec.rb
CHANGED
@@ -34,12 +34,12 @@ end # Cmds.out!
|
|
34
34
|
|
35
35
|
describe "Cmds#out" do
|
36
36
|
it "gets echo output" do
|
37
|
-
expect( Cmds
|
37
|
+
expect( Cmds.new("echo %s").out "hey there!" ).to eq "hey there!\n"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "reads input" do
|
41
41
|
expect(
|
42
|
-
Cmds
|
42
|
+
Cmds.new("ruby -e %{script}").out(script: "puts STDIN.read") {
|
43
43
|
"hey there!"
|
44
44
|
}
|
45
45
|
).to eq "hey there!\n"
|
@@ -48,18 +48,18 @@ end # Cmds#out
|
|
48
48
|
|
49
49
|
describe "Cmds#out!" do
|
50
50
|
it "gets echo output" do
|
51
|
-
expect( Cmds
|
51
|
+
expect( Cmds.new("echo %s").out! "hey there!" ).to eq "hey there!\n"
|
52
52
|
end
|
53
53
|
|
54
54
|
it "reads input" do
|
55
55
|
expect(
|
56
|
-
Cmds
|
56
|
+
Cmds.new("ruby -e %{script}").out!(script: "puts STDIN.read") {
|
57
57
|
"hey there!"
|
58
58
|
}
|
59
59
|
).to eq "hey there!\n"
|
60
60
|
end
|
61
61
|
|
62
62
|
it "errors when the command fails" do
|
63
|
-
expect { Cmds
|
63
|
+
expect { Cmds.new("false").out! }.to raise_error SystemCallError
|
64
64
|
end
|
65
65
|
end # Cmds#out!
|
data/spec/cmds/prepare_spec.rb
CHANGED
@@ -17,8 +17,8 @@ describe "Cmds.prepare" do
|
|
17
17
|
it "should work with a keyword substitutions with String keys" do
|
18
18
|
expect(
|
19
19
|
# NOTE since we use **kwds in #prepare which only accepts symbol keys,
|
20
|
-
# have to load kwds with string keys in through
|
21
|
-
Cmds
|
20
|
+
# have to load kwds with string keys in through Cmds.new
|
21
|
+
Cmds.new(
|
22
22
|
"psql <%= opts %> <%= database %> < <%= filepath %>", {
|
23
23
|
kwds: {
|
24
24
|
'database' => "blah",
|
@@ -199,4 +199,12 @@ describe "Cmds.prepare" do
|
|
199
199
|
end
|
200
200
|
end # % proceeded by =
|
201
201
|
end # shortcuts
|
202
|
+
|
203
|
+
context "tokenize multiple args as shell list" do
|
204
|
+
it "should expand args as space-separated list" do
|
205
|
+
expect(
|
206
|
+
Cmds.prepare "git add <%= *args %>", "x", "y", "z"
|
207
|
+
).to eq "git add x y z"
|
208
|
+
end
|
209
|
+
end
|
202
210
|
end # ::sub
|
data/spec/cmds/stream_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe "Cmds::stream" do
|
|
5
5
|
|
6
6
|
it "writes to $stdout and $stderr by default" do
|
7
7
|
out, err = temp_outs do
|
8
|
-
Cmds
|
8
|
+
Cmds.new('./test/tick.rb <%= times %>').stream times: times
|
9
9
|
end
|
10
10
|
|
11
11
|
expect(out).to eq times.times.map{|_| "#{_}\n"}.join
|
@@ -15,7 +15,7 @@ describe "Cmds::stream" do
|
|
15
15
|
it "handles writes in blocks" do
|
16
16
|
out_count = 0
|
17
17
|
err_count = 0
|
18
|
-
Cmds
|
18
|
+
Cmds.new('./test/tick.rb <%= times %>').stream(times: times) do |io|
|
19
19
|
io.on_out do |line|
|
20
20
|
out_count += 1
|
21
21
|
end
|
@@ -32,7 +32,7 @@ describe "Cmds::stream" do
|
|
32
32
|
it "accepts string value input from a block" do
|
33
33
|
|
34
34
|
out, err = temp_outs do
|
35
|
-
Cmds
|
35
|
+
Cmds.new("wc -l").stream do
|
36
36
|
<<-BLOCK
|
37
37
|
one
|
38
38
|
two
|
@@ -47,7 +47,7 @@ describe "Cmds::stream" do
|
|
47
47
|
|
48
48
|
it "accepts stream value input from a block" do
|
49
49
|
out, err = temp_outs do
|
50
|
-
Cmds
|
50
|
+
Cmds.new("wc -l").stream do
|
51
51
|
File.open "./test/lines.txt"
|
52
52
|
end
|
53
53
|
end
|