cmd-utils 1.0.6 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cmd-utils.rb +151 -61
  3. data/tests/test-cmd-utils.rb +239 -134
  4. metadata +8 -10
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21f1afe5b217d494c957c641883697faae639685
4
+ data.tar.gz: f333b84392e66181fad89bbd6672bef55fd05bc4
5
+ SHA512:
6
+ metadata.gz: b482a92e33b183d96ef1748cbd84cf3a95885cc8d1273a56e9aa2b2a17f7e30b68f4bae5173343bc5a770530e50bc67637059ec2d4ae688f749b29e0a92569d1
7
+ data.tar.gz: 026d9fdcb50239a8bc05a698ad9756f9905bf6bc6fb15cc7ad1beee7f17ec9b7680632576ee5f2f7d3afe2c0cffc6978c7b103136bd573a0a1657bf64a170cc3
data/lib/cmd-utils.rb CHANGED
@@ -5,13 +5,14 @@
5
5
  #
6
6
  # require 'cmd-utils'
7
7
  #
8
- # Utilities for output, and running commands.
8
+ # Utilities for option-controlled output, and running commands.
9
9
  #
10
10
  # The output and run methods rely on some external variables:
11
11
  #
12
- # $verbose -- causes certain commands to talk more
13
- # $norun -- causes the "run" command to print its argument, but not actually run it.
14
- # $quiet -- causes certain commands to talk less
12
+ # $verbose -- enables vtalk(f) output
13
+ # $norun -- enables nrtalk(f) output and controls the "run" command
14
+ # $quiet -- enables qtalk(f) output, and disables talk(f) output
15
+ # $debug -- enables dtalk(f) output
15
16
  #
16
17
  # These routines provide conditional output. The arguments can be given as part of the
17
18
  # the function calls, or, can be provided as the return value of a block. The advantage
@@ -23,24 +24,68 @@
23
24
  # talk - Print msg on STDERR unless `$quiet` is set
24
25
  #
25
26
  # :call-seq:
26
- # talk msg
27
- # talk { msg }
27
+ # talk msg ..
28
+ # talk { msg .. }
28
29
  # talkf fmt, args ...
29
30
  # talkf fmt { [ args ... ] }
30
31
 
31
- def talk msg=nil
32
- if !$quiet && (msg || block_given?)
33
- $stderr.puts(msg || yield)
32
+ def talk *args
33
+ if !$quiet && (args.size > 0 || block_given?)
34
+ $stderr.puts(*_msgargs(args, block_given?) { yield })
34
35
  end
35
36
  end
36
37
 
37
- def talkf fmt='%s', *args
38
- args = yield if args.size == 0 && block_given?
39
- talk { sprintf(fmt, *args) }
38
+ def talkf *args
39
+ if !$quiet && (args.size > 0 || block_given?)
40
+ $stderr.printf(*_fmtargs(args, block_given?) { yield } )
41
+ end
42
+ end
43
+
44
+ # _msgargs(args, block_given?) { yield }
45
+ #
46
+ # merge args with any block results
47
+
48
+ def _msgargs args, flag
49
+ args.concat([yield].flatten) if flag
50
+ args
51
+ end
52
+
53
+ # _fmtargs(args, block_given?) { yield }
54
+ #
55
+ # merge args with any block results
56
+ # provide default format string if only one argument
57
+
58
+ def _fmtargs args, flag
59
+ args.concat([yield].flatten) if flag
60
+ args.unshift('%s') if args.size < 2
61
+ args
62
+ end
63
+
64
+ ##
65
+ # dtalk - "debug talk"
66
+ # Print msg on STDERR only if `$debug` is set
67
+ #
68
+ # :call-seq:
69
+ # dtalk msg
70
+ # dtalk { msg }
71
+ # dtalkf fmt, args ..
72
+ # dtalkf fmt { [ args .. ] }
73
+
74
+ def dtalk *args
75
+ if $debug && (args.size > 0 || block_given?)
76
+ $stderr.puts(*_msgargs(args, block_given?) { yield })
77
+ end
78
+ end
79
+
80
+ def dtalkf *args
81
+ if $debug && (args.size> 0 || block_given?)
82
+ $stderr.printf(*_fmtargs(args, block_given?) { yield })
83
+ end
40
84
  end
41
85
 
42
86
  ##
43
- # qtalk - Print msg on STDERR only if `$quiet` is set
87
+ # qtalk - "quiet talk"
88
+ # print msg on STDERR only if `$quiet` is set
44
89
  #
45
90
  # :call-seq:
46
91
  # qtalk msg
@@ -48,19 +93,21 @@ end
48
93
  # qtalkf fmt, args ..
49
94
  # qtalkf fmt { [ args .. ] }
50
95
 
51
- def qtalk msg=nil
52
- if $quiet && (msg || block_given?)
53
- $stderr.puts(msg || yield)
96
+ def qtalk *args
97
+ if $quiet && (args.size > 0 || block_given?)
98
+ $stderr.puts(*_msgargs(args, block_given?) { yield })
54
99
  end
55
100
  end
56
101
 
57
- def qtalkf fmt='%s', *args
58
- args = yield if args.size == 0 && block_given?
59
- qtalk { sprintf(fmt, *args) }
102
+ def qtalkf *args
103
+ if $quiet && (args.size > 0 || block_given?)
104
+ $stderr.printf(*_fmtargs(args, block_given?) { yield } )
105
+ end
60
106
  end
61
107
 
62
108
  ##
63
- # vtalk -- Print msg on STDERR if `$verbose` is set
109
+ # vtalk - "verbose talk"
110
+ # Print msg on STDERR if `$verbose` is set
64
111
  #
65
112
  # :call-seq:
66
113
  # vtalk msg
@@ -68,37 +115,41 @@ end
68
115
  # vtalkf fmt, args ..
69
116
  # vtalkf fmt { args .. }
70
117
 
71
- def vtalk msg=nil
72
- if $verbose && (msg || block_given?)
73
- $stderr.puts(msg || yield)
118
+ def vtalk *args
119
+ if $verbose && (args.size > 0 || block_given?)
120
+ $stderr.puts(*_msgargs(args, block_given?) { yield })
74
121
  end
75
122
  end
76
123
 
77
- def vtalkf fmt='%s', *args
78
- args = yield if args.size == 0 && block_given?
79
- vtalk { sprintf(fmt, *args) }
124
+ def vtalkf *args
125
+ if $verbose && (args.size > 0 || block_given?)
126
+ $stderr.printf(*_fmtargs(args, block_given?) { yield } )
127
+ end
80
128
  end
81
129
 
82
130
  ##
83
- # nvtalk -- Print msg on STDERR unless `$verbose` is set
131
+ # nvtalk -- "non-verbose" talk
132
+ # Print msg on STDERR unless `$verbose` is set
84
133
  #
85
134
  # :call-seq:
86
135
  # nvtalk msg
87
136
  # nvtalk { msg }
88
137
 
89
- def nvtalk msg=nil
90
- unless $verbose && (msg || block_given?)
91
- $stderr.puts(msg || yield)
138
+ def nvtalk *args
139
+ unless $verbose && (args.size > 0 || block_given?)
140
+ $stderr.puts(*_msgargs(args, block_given?) { yield })
92
141
  end
93
142
  end
94
143
 
95
- def nvtalkf fmt='%s', *args
96
- args = yield if args.size == 0 && block_given?
97
- nvtalk { sprintf(fmt, *args) }
144
+ def nvtalkf *args
145
+ unless $verbose && (args.size > 0 || block_given?)
146
+ $stderr.printf(*_fmtargs(args, block_given?) { yield } )
147
+ end
98
148
  end
99
149
 
100
150
  ##
101
- # nrtalk -- Print msg on STDERR only if `$norun` is set
151
+ # nrtalk -- "no run" talk
152
+ # Print msg, prefixed with "(norun) ", on STDERR only if `$norun` is set
102
153
  #
103
154
  # :call-seq:
104
155
  # nrtalk msg
@@ -106,35 +157,51 @@ end
106
157
  # nrtalkf fmt, msg
107
158
  # nrtalkf fmt { msg }
108
159
 
109
- def nrtalk msg=nil
110
- if $norun
111
- $stderr.puts(msg || yield)
160
+ def nrtalk *args
161
+ if $norun && (args.size > 0 || block_given?)
162
+ newargs = _msgargs(args, block_given?) { yield }
163
+ newargs[0] = '(norun) ' + newargs[0] unless newargs.size == 0 || newargs[0].nil? || newargs[0].include?('(norun)')
164
+ $stderr.puts(*newargs)
112
165
  end
113
166
  end
114
167
 
115
168
  def nrtalkf *args
116
- args = yield if args.size == 0 && block_given?
117
- nrtalk { sprintf(*args) }
169
+ if $norun && (args.size > 0 || block_given?)
170
+ newargs = _fmtargs(args, block_given?) { yield }
171
+ newargs[0] = '(norun) ' + newargs[0] unless newargs.size == 0 || newargs[0].nil? || newargs[0].include?('(norun)')
172
+ $stderr.printf(*newargs)
173
+ end
118
174
  end
119
175
 
120
176
  ##
121
177
  # error -- print an error message on STDERR, and then exit.
122
178
  # :call-seq:
123
- # error [code], msg
124
- # errof [code], fmt, args
179
+ # error [code], msg
180
+ # error( [code]) { msg }
181
+ # error {[[code], msg ] }
125
182
  #
126
183
  # Code defaults to 1 if not given.
127
184
 
128
185
  def error *args
129
- args = yield if args.size == 0 && block_given?
186
+ args = _msgargs(args, block_given?) { yield }
130
187
  code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
131
188
  $stderr.puts(*args)
132
189
  $stderr.flush
133
190
  exit code
134
191
  end
135
192
 
193
+ ##
194
+ # errorf -- print a formatted message on STDERR, and then exit
195
+ #
196
+ # :call-seq:
197
+ # errorf [code], fmt, args ..
198
+ # errorf( [code], fmt) { args .. }
199
+ # errorf( [code]) { fmt, args .. }
200
+ # errorf {[[code], fmt, args .. ] }
201
+
136
202
  def errorf *args
137
- args = yield if args.size == 0 && block_given?
203
+ args = _fmtargs(args, block_given?) { yield }
204
+ # default the error code to 1 unless the first argument is a Fixnum
138
205
  code = args.size > 0 && args[0].class == Fixnum ? args.shift : 1
139
206
  $stderr.printf(*args)
140
207
  $stderr.flush
@@ -145,11 +212,22 @@ end
145
212
  # run -- run a command with support for testing, diagnostics and verbosity
146
213
  # safe_run -- run a command with support for diagnostics and verbosity
147
214
  #
215
+ # Both may be given optional `errmsg` and `okmsg`, which are printed if given
216
+ # for the corresponding condition.
217
+ #
148
218
  # :call-seq:
149
- # run cmd
150
- # run { cmd }
151
- # safe_run cmd
152
- # safe_run { cmd }
219
+ # run cmd
220
+ # run { cmd }
221
+ # run cmd, errmsg
222
+ # run { [cmd, errmsg] }
223
+ # run { [cmd, errmsg, okmg] }
224
+ # run cmd, errmsg, okmsg
225
+ # safe_run cmd
226
+ # safe_run cmd, errmsg
227
+ # safe_run cmd, errmsg, okmsg
228
+ # safe_run { cmd }
229
+ # safe_run { [cmd, errmsg] }
230
+ # safe_run { [cmd, errmsg, okmsg] }
153
231
  #
154
232
  # if `$norun` is set, print `(norun) ` followed by `cmd` on `STDERR`, and
155
233
  # return.
@@ -159,27 +237,39 @@ end
159
237
  # Invoke the `cmd` with the `system()` call.
160
238
  #
161
239
  # If there is an error, show the command (preceded by `>> `) if `$verbose` is
162
- # not set, then show the error code.
240
+ # not set, then show the error code, followed by the given `errmsg` or the
241
+ # default error message.
163
242
  #
164
243
  # The `cmd` can be given either as an argument, or as the returned value from a
165
- # block.
244
+ # block. Important: the block should return a string value to be passed to
245
+ # the system call.
166
246
 
167
- def run cmd=nil
168
- cmd ||= block_given? && yield
247
+ def cmd_run *args
248
+ args = _msgargs(args, block_given?) { yield }
169
249
  if $norun
170
- $stderr.printf "(norun) %s\n", cmd
171
- else
172
- safe_run cmd
250
+ nrtalk(args.first)
251
+ elsif args.size > 0
252
+ safe_run(*args)
173
253
  end
174
254
  end
175
255
 
176
- def safe_run cmd=nil
177
- cmd ||= block_given? && yield
178
- vtalkf { [ ">> %s\n", cmd ] }
179
- system cmd
180
- if $? > 0
181
- qtalkf { [ ">> %s\n", cmd ] }
182
- errorf $?, "Command failed with code %d!\n", $?
256
+ alias run cmd_run
257
+
258
+ def safe_run *args
259
+ args = _msgargs(args, block_given?) { yield }
260
+ cmd, errmsg, okmsg = args
261
+ vtalkf ">> %s\n", cmd
262
+ if cmd
263
+ if system(cmd) # invoke the command
264
+ $stderr.puts okmsg if okmsg
265
+ return true
266
+ else # an error occured
267
+ qtalkf ">> %s\n", cmd
268
+ erm = sprintf(errmsg ? errmsg : "Command failed with code %d", $?>>8)
269
+ $stderr.puts erm
270
+ $stderr.flush
271
+ raise SystemCallError, erm # instead of exit, use raise
272
+ end
183
273
  end
184
274
  end
185
275
 
@@ -2,7 +2,7 @@
2
2
  # test-cmd-utils.rb -- simple tester for cmd-utils
3
3
  #
4
4
  require 'rubygems'
5
- require 'minitest'
5
+ #require 'minitest'
6
6
  require 'minitest/autorun'
7
7
  require 'cmd-utils'
8
8
 
@@ -10,197 +10,302 @@ require 'cmd-utils'
10
10
 
11
11
  class TestCmdUtils < MiniTest::Test
12
12
 
13
- def gen_test name, norun, verbose, quiet, output
14
- $norun = norun > 0
15
- $verbose = verbose > 0
16
- $quiet = quiet > 0
13
+ #def gen_test name, norun, verbose, quiet, debug, output
14
+ def gen_test name, flags, output
15
+ $norun = flags.include?('n')
16
+ $verbose = flags.include?('v')
17
+ $quiet = flags.include?('q')
18
+ $debug = flags.include?('d')
17
19
  out, err = capture_io { yield }
18
20
  assert_empty(out, "#{name} $stdout should be empty")
19
- case output
20
- when TrueClass
21
- refute_empty(err, "#{name} $stderr should not be empty")
22
- when FalseClass
23
- assert_empty(err, "#{name} $stderr should be empty")
24
- when String
25
- assert_match(out, output, "#{name} $stderr should match #{output}")
21
+ io_assert :stderr, name, err, output
22
+ end
23
+
24
+ def run_test name, flags, output=nil, errput=nil
25
+ $norun = flags.include?('n')
26
+ $verbose = flags.include?('v')
27
+ $quiet = flags.include?('q')
28
+ $debug = flags.include?('d')
29
+ begin
30
+ out, err = capture_subprocess_io do
31
+ begin
32
+ yield
33
+ rescue
34
+ end
35
+ end
36
+ rescue
26
37
  end
38
+ io_assert :stdout, name, out, output
39
+ io_assert :stderr, name, err, errput
27
40
  true
28
41
  end
29
42
 
43
+ def io_assert ioname, name, actual, expected
44
+ if expected
45
+ case expected
46
+ when TrueClass then refute_empty(actual, "#{name} $#{ioname} should not be empty")
47
+ when FalseClass then assert_empty(actual, "#{name} $#{ioname} should be empty")
48
+ when String then assert_match(actual, expected, "#{name} $#{ioname}: expected '#{expected}', got: '#{actual}'")
49
+ end
50
+ end
51
+ true
52
+ end
30
53
 
31
54
  def test_talk_arg
32
- # nr, verb, quiet, output?
33
- gen_test('talk_arg', 0, 0, 0, true) { talk "hello" }
34
- gen_test('talk_arg', 0, 0, 1, false) { talk "hello" }
35
- gen_test('talk_arg', 0, 1, 0, true) { talk "hello" }
36
- gen_test('talk_arg', 0, 1, 1, false) { talk "hello" }
37
- gen_test('talk_arg', 1, 0, 0, true) { talk "hello" }
38
- gen_test('talk_arg', 1, 0, 1, false) { talk "hello" }
39
- gen_test('talk_arg', 1, 1, 0, true) { talk "hello" }
40
- gen_test('talk_arg', 1, 1, 1, false) { talk "hello" }
55
+ gen_test('talk_arg', ' ', true) { talk "hello" }
56
+ gen_test('talk_arg', ' q ', false) { talk "hello" }
57
+ gen_test('talk_arg', ' v ', true) { talk "hello" }
58
+ gen_test('talk_arg', ' vq ', false) { talk "hello" }
59
+ gen_test('talk_arg', 'n ', true) { talk "hello" }
60
+ gen_test('talk_arg', 'n q ', false) { talk "hello" }
61
+ gen_test('talk_arg', 'nv ', true) { talk "hello" }
62
+ gen_test('talk_arg', 'nvq ', false) { talk "hello" }
41
63
  end
42
64
 
43
65
  def test_talk_content
44
- gen_test('talk_content', 0, 0, 0, "hello") { talk "hello" }
45
- gen_test('talk_content_block', 0, 0, 0, "hello") { talk { "hello" } }
66
+ gen_test('talk_content', ' ', "hello\n") { talk "hello" }
67
+ gen_test('talk_content_block', ' ', "hello\n") { talk { "hello" } }
46
68
  end
47
69
 
48
70
  def test_talk_block
49
- # nr, verb, quiet, output?
50
- gen_test('talk', 0, 0, 0, true) { talk { "hello" } }
51
- gen_test('talk', 0, 0, 1, false) { talk { "hello" } }
52
- gen_test('talk', 0, 1, 0, true) { talk { "hello" } }
53
- gen_test('talk', 0, 1, 1, false) { talk { "hello" } }
54
- gen_test('talk', 1, 0, 0, true) { talk { "hello" } }
55
- gen_test('talk', 1, 0, 1, false) { talk { "hello" } }
56
- gen_test('talk', 1, 1, 0, true) { talk { "hello" } }
57
- gen_test('talk', 1, 1, 1, false) { talk { "hello" } }
71
+ gen_test('talk', ' ', "hello\n") { talk { "hello" } }
72
+ gen_test('talk', ' q ', false) { talk { "hello" } }
73
+ gen_test('talk', ' v ', "hello\n") { talk { "hello" } }
74
+ gen_test('talk', ' vq ', false) { talk { "hello" } }
75
+ gen_test('talk', 'n ', "hello\n") { talk { "hello" } }
76
+ gen_test('talk', 'n q ', false) { talk { "hello" } }
77
+ gen_test('talk', 'nv ', "hello\n") { talk { "hello" } }
78
+ gen_test('talk', 'nvq ', false) { talk { "hello" } }
58
79
  end
59
80
 
60
81
  def test_talkf_arg
61
82
  # nr, verb, quiet, output?
62
- gen_test('talkf_arg', 0, 0, 0, true) { talkf "%s", "hello" }
63
- gen_test('talkf_arg', 0, 0, 1, false) { talkf "%s", "hello" }
64
- gen_test('talkf_arg', 0, 1, 0, true) { talkf "%s", "hello" }
65
- gen_test('talkf_arg', 0, 1, 1, false) { talkf "%s", "hello" }
66
- gen_test('talkf_arg', 1, 0, 0, true) { talkf "%s", "hello" }
67
- gen_test('talkf_arg', 1, 0, 1, false) { talkf "%s", "hello" }
68
- gen_test('talkf_arg', 1, 1, 0, true) { talkf "%s", "hello" }
69
- gen_test('talkf_arg', 1, 1, 1, false) { talkf "%s", "hello" }
83
+ gen_test('talkf_arg', ' ', true) { talkf "%s", "hello" }
84
+ gen_test('talkf_arg', ' q ', false) { talkf "%s", "hello" }
85
+ gen_test('talkf_arg', ' v ', true) { talkf "%s", "hello" }
86
+ gen_test('talkf_arg', ' vq ', false) { talkf "%s", "hello" }
87
+ gen_test('talkf_arg', 'n ', true) { talkf "%s", "hello" }
88
+ gen_test('talkf_arg', 'n q ', false) { talkf "%s", "hello" }
89
+ gen_test('talkf_arg', 'nv ', true) { talkf "%s", "hello" }
90
+ gen_test('talkf_arg', 'nvq ', false) { talkf "%s", "hello" }
70
91
  end
71
92
 
72
93
  def test_talkf_block
73
94
  # nr, verb, quiet, output?
74
- gen_test('talkf_block', 0, 0, 0, true) { talkf("%s") { "hello" } }
75
- gen_test('talkf_block', 0, 0, 1, false) { talkf("%s") { "hello" } }
76
- gen_test('talkf_block', 0, 1, 0, true) { talkf("%s") { "hello" } }
77
- gen_test('talkf_block', 0, 1, 1, false) { talkf("%s") { "hello" } }
78
- gen_test('talkf_block', 1, 0, 0, true) { talkf("%s") { "hello" } }
79
- gen_test('talkf_block', 1, 0, 1, false) { talkf("%s") { "hello" } }
80
- gen_test('talkf_block', 1, 1, 0, true) { talkf("%s") { "hello" } }
81
- gen_test('talkf_block', 1, 1, 1, false) { talkf("%s") { "hello" } }
95
+ gen_test('talkf_block', ' ', true) { talkf("%s") { "hello" } }
96
+ gen_test('talkf_block', ' q ', false) { talkf("%s") { "hello" } }
97
+ gen_test('talkf_block', ' v ', true) { talkf("%s") { "hello" } }
98
+ gen_test('talkf_block', ' vq ', false) { talkf("%s") { "hello" } }
99
+ gen_test('talkf_block', 'n ', true) { talkf("%s") { "hello" } }
100
+ gen_test('talkf_block', 'n q ', false) { talkf("%s") { "hello" } }
101
+ gen_test('talkf_block', 'nv ', true) { talkf("%s") { "hello" } }
102
+ gen_test('talkf_block', 'nvq ', false) { talkf("%s") { "hello" } }
82
103
  end
83
104
 
84
105
  def test_talkf_content
85
- gen_test('talkf_content', 0, 0, 0, "-hello-") { talkf "-%s-", "hello" }
86
- gen_test('talkf_content', 0, 0, 0, "-hello-") { talkf("-%s-") { "hello" } }
87
- gen_test('talkf_default_content', 0, 0, 0, 'hello') { talkf "hello" }
88
- gen_test('talkf_default_content', 0, 0, 0, 'hello') { talkf { "hello" } }
106
+ gen_test('talkf_content', ' ', "-hello-") { talkf "-%s-", "hello" }
107
+ gen_test('talkf_content', ' ', "-hello-") { talkf("-%s-") { "hello" } }
108
+ gen_test('talkf_content', ' ', "-hello-") { talkf { ["-%s-", "hello" ] } }
109
+ gen_test('talkf_default_content', ' ', 'hello') { talkf "hello" }
110
+ gen_test('talkf_default_content', ' ', 'hello') { talkf { "hello" } }
89
111
  end
90
112
 
91
113
  def test_qtalk_arg
92
- # nr,vrb,q, out?
93
- gen_test('qtalk', 0, 0, 0, false) { qtalk "hello" }
94
- gen_test('qtalk', 0, 0, 1, true) { qtalk "hello" }
95
- gen_test('qtalk', 0, 1, 0, false) { qtalk "hello" }
96
- gen_test('qtalk', 0, 1, 1, true) { qtalk "hello" }
97
- gen_test('qtalk', 1, 0, 0, false) { qtalk "hello" }
98
- gen_test('qtalk', 1, 0, 1, true) { qtalk "hello" }
99
- gen_test('qtalk', 1, 1, 0, false) { qtalk "hello" }
100
- gen_test('qtalk', 1, 1, 1, true) { qtalk "hello" }
114
+ gen_test('qtalk', ' ', false) { qtalk "hello" }
115
+ gen_test('qtalk', ' q ', "hello\n") { qtalk "hello" }
116
+ gen_test('qtalk', ' v ', false) { qtalk "hello" }
117
+ gen_test('qtalk', ' vq ', "hello\n") { qtalk "hello" }
118
+ gen_test('qtalk', 'n ', false) { qtalk "hello" }
119
+ gen_test('qtalk', 'n q ', "hello\n") { qtalk "hello" }
120
+ gen_test('qtalk', 'nv ', false) { qtalk "hello" }
121
+ gen_test('qtalk', 'nvq ', "hello\n") { qtalk "hello" }
101
122
  end
102
123
 
103
124
  def test_qtalk_block
104
- # nr, verb, quiet, output?
105
- gen_test('qtalk', 0, 0, 0, false) { qtalk { "hello" } }
106
- gen_test('qtalk', 0, 0, 1, true) { qtalk { "hello" } }
107
- gen_test('qtalk', 0, 1, 0, false) { qtalk { "hello" } }
108
- gen_test('qtalk', 0, 1, 1, true) { qtalk { "hello" } }
109
- gen_test('qtalk', 1, 0, 0, false) { qtalk { "hello" } }
110
- gen_test('qtalk', 1, 0, 1, true) { qtalk { "hello" } }
111
- gen_test('qtalk', 1, 1, 0, false) { qtalk { "hello" } }
112
- gen_test('qtalk', 1, 1, 1, true) { qtalk { "hello" } }
125
+ gen_test('qtalk', ' ', false) { qtalk { "hello" } }
126
+ gen_test('qtalk', ' q ', "hello\n") { qtalk { "hello" } }
127
+ gen_test('qtalk', ' v ', false) { qtalk { "hello" } }
128
+ gen_test('qtalk', ' vq ', "hello\n") { qtalk { "hello" } }
129
+ gen_test('qtalk', 'n ', false) { qtalk { "hello" } }
130
+ gen_test('qtalk', 'n q ', "hello\n") { qtalk { "hello" } }
131
+ gen_test('qtalk', 'nv ', false) { qtalk { "hello" } }
132
+ gen_test('qtalk', 'nvq ', "hello\n") { qtalk { "hello" } }
113
133
  end
114
134
 
115
135
  def test_qtalk_content
116
- gen_test('qtalk_content', 0, 0, 1, "-hello-") { qtalk "hello" }
117
- gen_test('qtalk_content', 0, 0, 1, "-hello-") { qtalk { "hello" } }
136
+ gen_test('qtalk_content', 'q', "hello\n") { qtalk "hello" }
137
+ gen_test('qtalk_content', ' ', false) { qtalk "hello" }
138
+ gen_test('qtalk_content', 'q', "hello\n") { qtalk { "hello" } }
139
+ gen_test('qtalk_content', ' ', false) { qtalk { "hello" } }
118
140
  end
119
141
 
120
142
  def test_qtalkf_content
121
- gen_test('qtalkf_content', 0, 0, 1, "-hello-") { qtalkf "-%s-", "hello" }
122
- gen_test('qtalkf_content', 0, 0, 1, "-hello-") { qtalkf("-%s-") { "hello" } }
123
- gen_test('qtalkf_default_content', 0, 0, 1, "-hello-") { qtalkf "hello" }
124
- gen_test('qtalkf_default_content', 0, 0, 1, "-hello-") { qtalkf { "hello" } }
143
+ gen_test('qtalkf_content', 'q', "-hello-") { qtalkf "-%s-", "hello" }
144
+ gen_test('qtalkf_content', 'q', "-hello-") { qtalkf("-%s-") { "hello" } }
145
+ gen_test('qtalkf_content', 'q', "-hello-") { qtalkf { [ "-%s-", "hello" ] } }
146
+ gen_test('qtalkf_default_content', 'q', "hello") { qtalkf "hello" }
147
+ gen_test('qtalkf_default_content', 'q', "hello") { qtalkf { "hello" } }
148
+ gen_test('qtalkf_content', ' ', false) { qtalkf "-%s-", "hello" }
149
+ gen_test('qtalkf_content', ' ', false) { qtalkf("-%s-") { "hello" } }
150
+ gen_test('qtalkf_default_content', ' ', false) { qtalkf "hello" }
151
+ gen_test('qtalkf_default_content', ' ', false) { qtalkf { "hello" } }
125
152
  end
126
153
 
127
154
  def test_vtalk_arg
128
- # nr, verb, quiet, output?
129
- gen_test('vtalk_arg', 0, 0, 0, false) { vtalk "hello" }
130
- gen_test('vtalk_arg', 0, 0, 1, false) { vtalk "hello" }
131
- gen_test('vtalk_arg', 0, 1, 0, true) { vtalk "hello" }
132
- gen_test('vtalk_arg', 0, 1, 1, true) { vtalk "hello" }
133
- gen_test('vtalk_arg', 1, 0, 0, false) { vtalk "hello" }
134
- gen_test('vtalk_arg', 1, 0, 1, false) { vtalk "hello" }
135
- gen_test('vtalk_arg', 1, 1, 0, true) { vtalk "hello" }
136
- gen_test('vtalk_arg', 1, 1, 1, true) { vtalk "hello" }
155
+ gen_test('vtalk_arg', ' ', false) { vtalk "hello" }
156
+ gen_test('vtalk_arg', ' q ', false) { vtalk "hello" }
157
+ gen_test('vtalk_arg', ' v ', "hello\n") { vtalk "hello" }
158
+ gen_test('vtalk_arg', ' vq ', "hello\n") { vtalk "hello" }
159
+ gen_test('vtalk_arg', 'n ', false) { vtalk "hello" }
160
+ gen_test('vtalk_arg', 'n q ', false) { vtalk "hello" }
161
+ gen_test('vtalk_arg', 'nv ', "hello\n") { vtalk "hello" }
162
+ gen_test('vtalk_arg', 'nvq ', "hello\n") { vtalk "hello" }
137
163
  end
138
164
 
139
165
  def test_vtalk_block
140
- # nr, verb, quiet, output?
141
- gen_test('vtalk_block', 0, 0, 0, false) { vtalk { "hello" } }
142
- gen_test('vtalk_block', 0, 0, 1, false) { vtalk { "hello" } }
143
- gen_test('vtalk_block', 0, 1, 0, true) { vtalk { "hello" } }
144
- gen_test('vtalk_block', 0, 1, 1, true) { vtalk { "hello" } }
145
- gen_test('vtalk_block', 1, 0, 0, false) { vtalk { "hello" } }
146
- gen_test('vtalk_block', 1, 0, 1, false) { vtalk { "hello" } }
147
- gen_test('vtalk_block', 1, 1, 0, true) { vtalk { "hello" } }
148
- gen_test('vtalk_block', 1, 1, 1, true) { vtalk { "hello" } }
166
+ gen_test('vtalk_block', ' ', false) { vtalk { "hello" } }
167
+ gen_test('vtalk_block', ' q ', false) { vtalk { "hello" } }
168
+ gen_test('vtalk_block', ' v ', "hello\n") { vtalk { "hello" } }
169
+ gen_test('vtalk_block', ' vq ', "hello\n") { vtalk { "hello" } }
170
+ gen_test('vtalk_block', 'n ', false) { vtalk { "hello" } }
171
+ gen_test('vtalk_block', 'n q ', false) { vtalk { "hello" } }
172
+ gen_test('vtalk_block', 'nv ', "hello\n") { vtalk { "hello" } }
173
+ gen_test('vtalk_block', 'nvq ', "hello\n") { vtalk { "hello" } }
149
174
  end
150
175
 
151
176
  def test_nvtalk_arg
152
- # nr, verb, quiet, output?
153
- gen_test('nvtalk_arg', 0, 0, 0, true) { nvtalk "hello" }
154
- gen_test('nvtalk_arg', 0, 0, 1, true) { nvtalk "hello" }
155
- gen_test('nvtalk_arg', 0, 1, 0, false) { nvtalk "hello" }
156
- gen_test('nvtalk_arg', 0, 1, 1, false) { nvtalk "hello" }
157
- gen_test('nvtalk_arg', 1, 0, 0, true) { nvtalk "hello" }
158
- gen_test('nvtalk_arg', 1, 0, 1, true) { nvtalk "hello" }
159
- gen_test('nvtalk_arg', 1, 1, 0, false) { nvtalk "hello" }
160
- gen_test('nvtalk_arg', 1, 1, 1, false) { nvtalk "hello" }
177
+ gen_test('nvtalk_arg', ' ', "hello\n") { nvtalk "hello" }
178
+ gen_test('nvtalk_arg', ' q ', "hello\n") { nvtalk "hello" }
179
+ gen_test('nvtalk_arg', ' v ', false) { nvtalk "hello" }
180
+ gen_test('nvtalk_arg', ' vq ', false) { nvtalk "hello" }
181
+ gen_test('nvtalk_arg', 'n ', "hello\n") { nvtalk "hello" }
182
+ gen_test('nvtalk_arg', 'n q ', "hello\n") { nvtalk "hello" }
183
+ gen_test('nvtalk_arg', 'nv ', false) { nvtalk "hello" }
184
+ gen_test('nvtalk_arg', 'nvq ', false) { nvtalk "hello" }
161
185
  end
162
186
 
163
187
  def test_nvtalk_block
164
- # nr, verb, quiet, output?
165
- gen_test('nvtalk_block', 0, 0, 0, true) { nvtalk { "hello" } }
166
- gen_test('nvtalk_block', 0, 0, 1, true) { nvtalk { "hello" } }
167
- gen_test('nvtalk_block', 0, 1, 0, false) { nvtalk { "hello" } }
168
- gen_test('nvtalk_block', 0, 1, 1, false) { nvtalk { "hello" } }
169
- gen_test('nvtalk_block', 1, 0, 0, true) { nvtalk { "hello" } }
170
- gen_test('nvtalk_block', 1, 0, 1, true) { nvtalk { "hello" } }
171
- gen_test('nvtalk_block', 1, 1, 0, false) { nvtalk { "hello" } }
172
- gen_test('nvtalk_block', 1, 1, 1, false) { nvtalk { "hello" } }
188
+ gen_test('nvtalk_block', ' ', true) { nvtalk { "hello" } }
189
+ gen_test('nvtalk_block', ' q ', true) { nvtalk { "hello" } }
190
+ gen_test('nvtalk_block', ' v ', false) { nvtalk { "hello" } }
191
+ gen_test('nvtalk_block', ' vq ', false) { nvtalk { "hello" } }
192
+ gen_test('nvtalk_block', 'n ', true) { nvtalk { "hello" } }
193
+ gen_test('nvtalk_block', 'n q ', true) { nvtalk { "hello" } }
194
+ gen_test('nvtalk_block', 'nv ', false) { nvtalk { "hello" } }
195
+ gen_test('nvtalk_block', 'nvq ', false) { nvtalk { "hello" } }
173
196
  end
174
197
 
175
198
  def test_nrtalk_arg
176
- # nr, verb, quiet, output?
177
- gen_test('nrtalk_arg', 0, 0, 0, false) { nrtalk "hello" }
178
- gen_test('nrtalk_arg', 0, 0, 1, false) { nrtalk "hello" }
179
- gen_test('nrtalk_arg', 0, 1, 0, false) { nrtalk "hello" }
180
- gen_test('nrtalk_arg', 0, 1, 1, false) { nrtalk "hello" }
181
- gen_test('nrtalk_arg', 1, 0, 0, true) { nrtalk "hello" }
182
- gen_test('nrtalk_arg', 1, 0, 1, true) { nrtalk "hello" }
183
- gen_test('nrtalk_arg', 1, 1, 0, true) { nrtalk "hello" }
184
- gen_test('nrtalk_arg', 1, 1, 1, true) { nrtalk "hello" }
199
+ gen_test('nrtalk_arg', ' ', false) { nrtalk "hello" }
200
+ gen_test('nrtalk_arg', ' q ', false) { nrtalk "hello" }
201
+ gen_test('nrtalk_arg', ' v ', false) { nrtalk "hello" }
202
+ gen_test('nrtalk_arg', ' vq ', false) { nrtalk "hello" }
203
+ gen_test('nrtalk_arg', 'n ', true) { nrtalk "hello" }
204
+ gen_test('nrtalk_arg', 'n q ', true) { nrtalk "hello" }
205
+ gen_test('nrtalk_arg', 'nv ', true) { nrtalk "hello" }
206
+ gen_test('nrtalk_arg', 'nvq ', true) { nrtalk "hello" }
207
+ end
208
+
209
+ def test_nrtalk_prefix
210
+ gen_test('nrtalk_prefix', 'n ', "(norun) hello\n") { nrtalk "hello" }
211
+ gen_test('nrtalk_prefix', ' ', false) { nrtalk "hello" }
212
+ gen_test('nrtalk_prefix', 'n ', "(norun) hello\n") { nrtalk { "hello" } }
213
+ gen_test('nrtalk_prefix', ' ', false) { nrtalk { "hello" } }
185
214
  end
186
215
 
187
216
  def test_nrtalk_block
188
- # nr, verb, quiet, output?
189
- gen_test('nrtalk_block', 0, 0, 0, false) { nrtalk { "hello" } }
190
- gen_test('nrtalk_block', 0, 0, 1, false) { nrtalk { "hello" } }
191
- gen_test('nrtalk_block', 0, 1, 0, false) { nrtalk { "hello" } }
192
- gen_test('nrtalk_block', 0, 1, 1, false) { nrtalk { "hello" } }
193
- gen_test('nrtalk_block', 1, 0, 0, true) { nrtalk { "hello" } }
194
- gen_test('nrtalk_block', 1, 0, 1, true) { nrtalk { "hello" } }
195
- gen_test('nrtalk_block', 1, 1, 0, true) { nrtalk { "hello" } }
196
- gen_test('nrtalk_block', 1, 1, 1, true) { nrtalk { "hello" } }
217
+ gen_test('nrtalk_block', ' ', false) { nrtalk { "hello" } }
218
+ gen_test('nrtalk_block', ' q ', false) { nrtalk { "hello" } }
219
+ gen_test('nrtalk_block', ' v ', false) { nrtalk { "hello" } }
220
+ gen_test('nrtalk_block', ' vq ', false) { nrtalk { "hello" } }
221
+ gen_test('nrtalk_block', 'n ', true) { nrtalk { "hello" } }
222
+ gen_test('nrtalk_block', 'n q ', true) { nrtalk { "hello" } }
223
+ gen_test('nrtalk_block', 'nv ', true) { nrtalk { "hello" } }
224
+ gen_test('nrtalk_block', 'nvq ', true) { nrtalk { "hello" } }
197
225
  end
198
226
 
199
227
  def test_nrtalkf_content
200
- gen_test('nrtalkf_content', 0, 0, 1, "-hello-") { nrtalkf "-%s-", "hello" }
201
- gen_test('nrtalkf_content', 0, 0, 1, "-hello-") { nrtalkf("-%s-") { "hello" } }
202
- gen_test('nrtalkf_default_content', 0, 0, 1, "-hello-") { nrtalkf "hello" }
203
- gen_test('nrtalkf_default_content', 0, 0, 1, "-hello-") { nrtalkf { "hello" } }
228
+ gen_test('nrtalkf_content', 'n ', "(norun) -hello-") { nrtalkf "-%s-", "hello" }
229
+ gen_test('nrtalkf_content', 'n ', "(norun) -hello-") { nrtalkf("-%s-") { "hello" } }
230
+ gen_test('nrtalkf_default_content', 'n ', "(norun) hello") { nrtalkf "hello" }
231
+ gen_test('nrtalkf_default_content', 'n ', "(norun) hello") { nrtalkf { "hello" } }
232
+
233
+ gen_test('nrtalkf_content', ' ', false) { nrtalkf "-%s-", "hello" }
234
+ gen_test('nrtalkf_content', ' ', false) { nrtalkf("-%s-") { "hello" } }
235
+ gen_test('nrtalkf_default_content', ' ', false) { nrtalkf "hello" }
236
+ gen_test('nrtalkf_default_content', ' ', false) { nrtalkf { "hello" } }
237
+ end
238
+
239
+ def test_nrtalkf_prefix
240
+ gen_test('nrtalkf_prefix', 'n ', "(norun) -hello-") { nrtalkf "-%s-", "hello" }
241
+ gen_test('nrtalkf_prefix', 'n ', "(norun) -hello-") { nrtalkf("-%s-") { "hello" } }
242
+ gen_test('nrtalkf_default_content', 'n ', "(norun) hello") { nrtalkf "hello" }
243
+ gen_test('nrtalkf_default_content', 'n ', "(norun) hello") { nrtalkf { "hello" } }
244
+ end
245
+
246
+ def test_dtalk_arg
247
+ gen_test('dtalk_arg', ' ', false) { dtalk "hello" }
248
+ gen_test('dtalk_arg', ' q ', false) { dtalk "hello" }
249
+ gen_test('dtalk_arg', ' v ', false) { dtalk "hello" }
250
+ gen_test('dtalk_arg', ' vq ', false) { dtalk "hello" }
251
+ gen_test('dtalk_arg', 'n ', false) { dtalk "hello" }
252
+ gen_test('dtalk_arg', 'n q ', false) { dtalk "hello" }
253
+ gen_test('dtalk_arg', 'nv ', false) { dtalk "hello" }
254
+ gen_test('dtalk_arg', 'nvq ', false) { dtalk "hello" }
255
+
256
+ gen_test('dtalk_arg', ' d', true) { dtalk "hello" }
257
+ gen_test('dtalk_arg', ' qd', true) { dtalk "hello" }
258
+ gen_test('dtalk_arg', ' v d', true) { dtalk "hello" }
259
+ gen_test('dtalk_arg', ' vqd', true) { dtalk "hello" }
260
+ gen_test('dtalk_arg', 'n d', true) { dtalk "hello" }
261
+ gen_test('dtalk_arg', 'n qd', true) { dtalk "hello" }
262
+ gen_test('dtalk_arg', 'nv d', true) { dtalk "hello" }
263
+ gen_test('dtalk_arg', 'nvqd', true) { dtalk "hello" }
264
+ end
265
+
266
+ def test_dtalk_block
267
+ gen_test('dtalk_block', ' d', true) { dtalk { "hello" } }
268
+ gen_test('dtalk_block', ' qd', true) { dtalk { "hello" } }
269
+ gen_test('dtalk_block', ' v d', true) { dtalk { "hello" } }
270
+ gen_test('dtalk_block', ' vqd', true) { dtalk { "hello" } }
271
+ gen_test('dtalk_block', 'n d', true) { dtalk { "hello" } }
272
+ gen_test('dtalk_block', 'n qd', true) { dtalk { "hello" } }
273
+ gen_test('dtalk_block', 'nv d', true) { dtalk { "hello" } }
274
+ gen_test('dtalk_block', 'nvqd', true) { dtalk { "hello" } }
204
275
  end
205
276
 
277
+ def test_dtalkf_content
278
+ gen_test('dtalkf_content', ' d', "-hello-") { dtalkf "-%s-", "hello" }
279
+ gen_test('dtalkf_content', ' d', "-hello-") { dtalkf("-%s-") { "hello" } }
280
+ gen_test('dtalkf_default_content', ' d', "-hello-") { dtalkf "hello" }
281
+ gen_test('dtalkf_default_content', ' d', "-hello-") { dtalkf { "hello" } }
282
+
283
+ gen_test('dtalkf_content', ' ', false) { dtalkf "-%s-", "hello" }
284
+ gen_test('dtalkf_content', ' ', false) { dtalkf("-%s-") { "hello" } }
285
+ gen_test('dtalkf_default_content', ' ', false) { dtalkf "hello" }
286
+ gen_test('dtalkf_default_content', ' ', false) { dtalkf { "hello" } }
287
+ end
288
+
289
+ def test_block_contents
290
+ gen_test('talkf_content', ' ', "-hello-there-") { talkf "-%s-%s-", "hello", "there" }
291
+ gen_test('talkf_content', ' ', "-hello-there-") { talkf("-%s-%s-") { ["hello", "there" ] } }
292
+ gen_test('talkf_content', ' ', "-hello-there-") { talkf("-%s-%s-", "hello") { "there" } }
293
+ gen_test('talkf_content', ' ', "-hello-there-") { talkf("-%s-%s-", "hello") { [ "there" ] } }
294
+ gen_test('talkf_default_content', ' ', "-hello-there-") { talkf { [ "-%s-%s-", "hello", "there" ] } }
295
+ end
296
+
297
+ def test_run_output
298
+ run_test("run 1", 'n', false, "(norun) echo 'hello'\n") { cmd_run "echo 'hello'" }
299
+ run_test("run 2", 'v', "hello\n", ">> echo 'hello'\n") { cmd_run "echo 'hello'" }
300
+ run_test("run 3", '', "hello\n", false) { cmd_run "echo 'hello'" }
301
+ run_test("run 4", '', false, "hello\n") { cmd_run "echo 'hello' 1>&2" }
302
+ run_test("run 5", '', false, false) { cmd_run "echo" }
303
+ end
304
+
305
+ def test_run_errmsg
306
+ run_test("run 11", 'n', false, "(norun) ( exit 1)\n") { cmd_run "( exit 1)", 'error 11' }
307
+ run_test("run 12", 'v', false, ">> ( exit 1)\nerror 12\n") { cmd_run "( exit 1)", 'error 12' }
308
+ run_test("run 13", 'v', false, ">> ( exit 0)\n") { cmd_run "( exit 0)", 'no error 13' }
309
+ run_test("run 14", ' ', false, false) { cmd_run "( exit 0)", 'no error 13' }
310
+ end
206
311
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmd-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
5
- prerelease:
4
+ version: 1.0.12
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alan K. Stebbens
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-19 00:00:00.000000000 Z
11
+ date: 2013-09-11 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Some handy utilities for writing command line scripts in ruby.
15
14
  email: aks@stebbens.org
@@ -21,30 +20,29 @@ files:
21
20
  - lib/lookup.rb
22
21
  - tests/test-cmd-utils.rb
23
22
  - tests/test-lookup.rb
24
- homepage: http://github.com/aks/cmd-utils
23
+ homepage: http://gitlab.com/as/cmd-utils
25
24
  licenses:
26
25
  - GPL-2
26
+ metadata: {}
27
27
  post_install_message:
28
28
  rdoc_options: []
29
29
  require_paths:
30
30
  - lib
31
31
  required_ruby_version: !ruby/object:Gem::Requirement
32
- none: false
33
32
  requirements:
34
- - - ! '>='
33
+ - - '>='
35
34
  - !ruby/object:Gem::Version
36
35
  version: '0'
37
36
  required_rubygems_version: !ruby/object:Gem::Requirement
38
- none: false
39
37
  requirements:
40
- - - ! '>='
38
+ - - '>='
41
39
  - !ruby/object:Gem::Version
42
40
  version: '0'
43
41
  requirements: []
44
42
  rubyforge_project:
45
- rubygems_version: 1.8.23
43
+ rubygems_version: 2.0.7
46
44
  signing_key:
47
- specification_version: 3
45
+ specification_version: 4
48
46
  summary: Utilities for ruby command line scripts
49
47
  test_files:
50
48
  - tests/test-cmd-utils.rb