benry-actionrunner 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGES.md +8 -0
- data/MIT-LICENSE +21 -0
- data/README.md +764 -0
- data/benry-actionrunner.gemspec +38 -0
- data/bin/arun +7 -0
- data/lib/benry/actionrunner.rb +556 -0
- data/test/app_test.rb +425 -0
- data/test/brownie_test.rb +206 -0
- data/test/help_test.rb +33 -0
- data/test/main_test.rb +322 -0
- data/test/run_all.rb +6 -0
- data/test/shared.rb +92 -0
- metadata +105 -0
data/test/app_test.rb
ADDED
@@ -0,0 +1,425 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
|
5
|
+
require_relative './shared'
|
6
|
+
|
7
|
+
|
8
|
+
Oktest.scope do
|
9
|
+
|
10
|
+
before_all do
|
11
|
+
TestHelperModule.setup_all()
|
12
|
+
end
|
13
|
+
|
14
|
+
after_all do
|
15
|
+
TestHelperModule.teardown_all()
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
topic Benry::ActionRunner do
|
20
|
+
|
21
|
+
def new_app()
|
22
|
+
config = Benry::ActionRunner::CONFIG
|
23
|
+
gschema = Benry::ActionRunner::GLOBAL_OPTION_SCHEMA
|
24
|
+
app = Benry::ActionRunner::MainApplication.new(config, gschema)
|
25
|
+
return app
|
26
|
+
end
|
27
|
+
|
28
|
+
before do
|
29
|
+
clear_registry()
|
30
|
+
@app = new_app()
|
31
|
+
@filename = Benry::ActionRunner::DEFAULT_FILENAME
|
32
|
+
File.unlink(@filename) if File.exist?(@filename)
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
File.unlink(@filename) if File.exist?(@filename)
|
37
|
+
end
|
38
|
+
|
39
|
+
def main(*args)
|
40
|
+
sout, serr, status = main!(*args)
|
41
|
+
ok {serr} == ""
|
42
|
+
ok {status} == 0
|
43
|
+
return sout
|
44
|
+
end
|
45
|
+
|
46
|
+
def main!(*args)
|
47
|
+
status = nil
|
48
|
+
sout, serr = capture_sio(tty: true) do
|
49
|
+
status = @app.main(args)
|
50
|
+
end
|
51
|
+
return sout, serr, status
|
52
|
+
end
|
53
|
+
|
54
|
+
topic('.main()') {
|
55
|
+
spec "[!wmcup] handles '$ACRIONRUNNER_OPTION' value." do
|
56
|
+
ENV['ACTIONRUNNER_OPTION'] = "-h -a"
|
57
|
+
at_end { ENV['ACTIONRUNNER_OPTION'] = nil }
|
58
|
+
sout, serr = capture_sio(tty: true) { Benry::ActionRunner.main(["-l"]) }
|
59
|
+
ok {serr} == ""
|
60
|
+
ok {sout} =~ /^\e\[1;34mUsage:\e\[0m$/
|
61
|
+
end
|
62
|
+
spec "[!hujvl] returns status code." do
|
63
|
+
status = nil
|
64
|
+
sout, serr = capture_sio(tty: true) {
|
65
|
+
status = Benry::ActionRunner.main(["-m"])
|
66
|
+
}
|
67
|
+
ok {status} == 1
|
68
|
+
ok {serr} == "\e\[31m[ERROR]\e\[0m -m: Unknown option.\n"
|
69
|
+
ok {sout} == ""
|
70
|
+
#
|
71
|
+
sout, serr = capture_sio(tty: true) {
|
72
|
+
status = Benry::ActionRunner.main(["-V"])
|
73
|
+
}
|
74
|
+
ok {status} == 0
|
75
|
+
ok {serr} == ""
|
76
|
+
ok {sout} != ""
|
77
|
+
end
|
78
|
+
}
|
79
|
+
|
80
|
+
topic('#parse_global_options()') {
|
81
|
+
spec "[!bpedh] parses `--<name>=<val>` as global variables." do
|
82
|
+
main "--g9942=ABC", "-h"
|
83
|
+
ok {$g9942} == "ABC"
|
84
|
+
end
|
85
|
+
spec "[!0tz4j] stops parsing options when any argument found." do
|
86
|
+
args = ["-hV", "-a", "help", "-a"]
|
87
|
+
@app.instance_eval { parse_global_options(args) }
|
88
|
+
ok {args} == ["help", "-a"]
|
89
|
+
end
|
90
|
+
spec "[!gkp9b] returns global options." do
|
91
|
+
args = ["-ha", "--g8839=DDD", "--g7402=EEE", "help"]
|
92
|
+
ret = @app.instance_eval { parse_global_options(args) }
|
93
|
+
ok {ret} == {:help => true, :all => true}
|
94
|
+
gvars = @app.instance_variable_get(:@global_vars)
|
95
|
+
ok {gvars} == {"g8839" => "DDD", "g7402" => "EEE"}
|
96
|
+
end
|
97
|
+
}
|
98
|
+
|
99
|
+
topic('#toggle_global_options()') {
|
100
|
+
spec "[!3kdds] global option '-C' sets `$COLOR_mode = false`." do
|
101
|
+
ok {$COLOR_MODE} == nil
|
102
|
+
at_end { $COLOR_MODE = nil }
|
103
|
+
main "-C", "-h"
|
104
|
+
ok {$COLOR_MODE} == false
|
105
|
+
end
|
106
|
+
spec "[!1882x] global option '-u' sets instance var." do
|
107
|
+
ok {@app.instance_variable_get(:@flag_search)} == false
|
108
|
+
main "-u", "-h"
|
109
|
+
ok {@app.instance_variable_get(:@flag_search)} == true
|
110
|
+
end
|
111
|
+
spec "[!bokge] global option '-w' sets instance var." do
|
112
|
+
ok {@app.instance_variable_get(:@flag_chdir)} == false
|
113
|
+
main "-w", "-h"
|
114
|
+
ok {@app.instance_variable_get(:@flag_chdir)} == true
|
115
|
+
end
|
116
|
+
spec "[!4sk24] global option '-f' changes filename." do
|
117
|
+
ok {@app.instance_variable_get(:@action_file)} == "Actionfile.rb"
|
118
|
+
main "-f", "Actions.rb", "-h"
|
119
|
+
ok {@app.instance_variable_get(:@action_file)} == "Actions.rb"
|
120
|
+
end
|
121
|
+
spec "[!9u400] sets `$BENRY_ECHOBACK = true` if option `-v` specified." do
|
122
|
+
bkup = $BENRY_ECHOBACK
|
123
|
+
at_end { $BENRY_ECHOBACK = bkup }
|
124
|
+
$BENRY_ECHOBACK = false
|
125
|
+
main "-v", "-h"
|
126
|
+
ok {$BENRY_ECHOBACK} == true
|
127
|
+
end
|
128
|
+
spec "[!jp2mw] sets `$BENRY_ECHOBACK = false` if option `-q` specified." do
|
129
|
+
bkup = $BENRY_ECHOBACK
|
130
|
+
at_end { $BENRY_ECHOBACK = bkup }
|
131
|
+
$BENRY_ECHOBACK = true
|
132
|
+
main "-q", "-h"
|
133
|
+
ok {$BENRY_ECHOBACK} == false
|
134
|
+
end
|
135
|
+
}
|
136
|
+
|
137
|
+
topic('#handle_global_options()') {
|
138
|
+
spec "[!psrmp] loads action file (if exists) before displaying help message." do
|
139
|
+
ok {@filename}.not_exist?
|
140
|
+
sout = arun "-h"
|
141
|
+
ok {sout}.include?(<<"END")
|
142
|
+
|
143
|
+
Actions:
|
144
|
+
help : print help message (of action if specified)
|
145
|
+
|
146
|
+
END
|
147
|
+
#
|
148
|
+
prepare_actionfile("a6913")
|
149
|
+
ok {@filename}.file_exist?
|
150
|
+
@app = new_app()
|
151
|
+
sout = arun "-h"
|
152
|
+
ok {sout}.include?(<<"END")
|
153
|
+
|
154
|
+
Actions:
|
155
|
+
a6913 : test
|
156
|
+
help : print help message (of action if specified)
|
157
|
+
|
158
|
+
END
|
159
|
+
end
|
160
|
+
spec "[!9wfaw] loads action file before listing actions by '-l' or '-L' option." do
|
161
|
+
sout = main "-g"
|
162
|
+
ok {@filename}.file_exist?
|
163
|
+
#
|
164
|
+
sout = arun "-l"
|
165
|
+
ok {sout}.include?(<<END)
|
166
|
+
Actions:
|
167
|
+
build : create all
|
168
|
+
build:zip : create zip file
|
169
|
+
clean : delete garbage files (and product files too if '-a')
|
170
|
+
END
|
171
|
+
#
|
172
|
+
sout = arun "-L", "actions"
|
173
|
+
ok {sout}.include?(<<END)
|
174
|
+
Actions:
|
175
|
+
build : create all
|
176
|
+
build:zip : create zip file
|
177
|
+
clean : delete garbage files (and product files too if '-a')
|
178
|
+
END
|
179
|
+
end
|
180
|
+
spec "[!qanx2] option '-l' and '-L' requires action file." do
|
181
|
+
["-l", "-Lactions"].each do |opt|
|
182
|
+
ok {@filename}.not_exist?
|
183
|
+
sout, serr, status = arun! opt
|
184
|
+
ok {status} != 0
|
185
|
+
ok {serr} == "[ERROR] Action file ('Actionfile.rb') not found. Create it by `arun -g` command firstly.\n"
|
186
|
+
ok {sout} == ""
|
187
|
+
end
|
188
|
+
end
|
189
|
+
spec "[!7995e] option '-g' generates action file." do
|
190
|
+
ok {@filename}.not_exist?
|
191
|
+
system "arun", "-g", "-q"
|
192
|
+
ok {@filename}.file_exist?
|
193
|
+
end
|
194
|
+
spec "[!k5nuk] option '-h' or '--help' prints help message." do
|
195
|
+
sout = main "-h"
|
196
|
+
ok {sout} =~ /^\e\[1;34mUsage:\e\[0m$/
|
197
|
+
end
|
198
|
+
spec "[!dmxt2] option '-V' prints version number." do
|
199
|
+
sout = arun "-V"
|
200
|
+
ok {sout} == "#{Benry::ActionRunner::VERSION}\n"
|
201
|
+
end
|
202
|
+
spec "[!i4qm5] option '-v' sets `$VERBOSE_MODE = true`." do
|
203
|
+
bkup = $VERBOSE_MODE
|
204
|
+
at_end { $VERBOSE_MODE = bkup }
|
205
|
+
$VERBOSE_MODE = nil
|
206
|
+
main "-v", "-h"
|
207
|
+
ok {$VERBOSE_MODE} == true
|
208
|
+
end
|
209
|
+
spec "[!5nwnv] option '-q' sets `$QUIET_MODE = true`." do
|
210
|
+
bkup = $QUIET_MODE
|
211
|
+
at_end { $QUIET_MODE = bkup }
|
212
|
+
$QUIET_MODE = nil
|
213
|
+
main "-q", "-h"
|
214
|
+
ok {$QUIET_MODE} == true
|
215
|
+
end
|
216
|
+
spec "[!klxkr] option '-c' sets `$COLOR_MODE = true`." do
|
217
|
+
bkup = $COLOR_MODE
|
218
|
+
at_end { $COLOR_MODE = bkup }
|
219
|
+
$COLOR_MODE = nil
|
220
|
+
main "-c", "-h"
|
221
|
+
ok {$COLOR_MODE} == true
|
222
|
+
end
|
223
|
+
spec "[!kqbwd] option '-C' sets `$COLOR_MODE = false`." do
|
224
|
+
bkup = $COLOR_MODE
|
225
|
+
at_end { $COLOR_MODE = bkup }
|
226
|
+
$COLOR_MODE = nil
|
227
|
+
main "-C", "-h"
|
228
|
+
ok {$COLOR_MODE} == false
|
229
|
+
end
|
230
|
+
spec "[!oce46] option '-D' sets `$DEBUG_MODE = true`." do
|
231
|
+
bkup = $DEBUG_MODE
|
232
|
+
at_end { $DEBUG_MODE = bkup }
|
233
|
+
$DEBUG_MODE = nil
|
234
|
+
main "-D", "-h"
|
235
|
+
ok {$DEBUG_MODE} == true
|
236
|
+
end
|
237
|
+
spec "[!mq5ko] option '-T' enables trace mode." do
|
238
|
+
prepare_actionfile("a3349")
|
239
|
+
sout = main "-T", "a3349", "Alice"
|
240
|
+
ok {sout} == <<"END"
|
241
|
+
\e[33m### enter: a3349\e[0m
|
242
|
+
Hi, Alice!
|
243
|
+
\e[33m### exit: a3349\e[0m
|
244
|
+
END
|
245
|
+
end
|
246
|
+
spec "[!jwah3] option '-X' sets `$DRYRUN_MODE = true`." do
|
247
|
+
bkup = $DRYRUN_MODE
|
248
|
+
at_end { $DRYRUN_MODE = bkup }
|
249
|
+
$DRYRUN_MODE = nil
|
250
|
+
main "-X", "-h"
|
251
|
+
ok {$DRYRUN_MODE} == true
|
252
|
+
end
|
253
|
+
}
|
254
|
+
|
255
|
+
topic('#handle_action()') {
|
256
|
+
spec "[!qdrui] loads action file before performing actions." do
|
257
|
+
prepare_actionfile("a6315")
|
258
|
+
sout, serr = capture_sio do
|
259
|
+
@app.instance_eval { handle_action(["a6315", "Alice"], {}) }
|
260
|
+
end
|
261
|
+
ok {serr} == ""
|
262
|
+
ok {sout} == "Hi, Alice!\n"
|
263
|
+
end
|
264
|
+
spec "[!4992c] raises error if action specified but action file not exist." do
|
265
|
+
pr = proc do
|
266
|
+
@app.instance_eval { handle_action(["hello", "Alice"], {}) }
|
267
|
+
end
|
268
|
+
ok {pr}.raise?(Benry::CmdApp::CommandError,
|
269
|
+
"Action file ('Actionfile.rb') not found. Create it by `arun -g` command firstly.")
|
270
|
+
end
|
271
|
+
}
|
272
|
+
|
273
|
+
topic('#skip_backtrace?()') {
|
274
|
+
spec "[!k8ddu] ignores backtrace of 'actionrunner.rb'." do
|
275
|
+
prepare_actionfile("a7452", "run_once('hello')")
|
276
|
+
sout, serr = capture_sio(tty: true) { @app.main(["a7452"]) }
|
277
|
+
ok {sout} == ""
|
278
|
+
ok {serr} =~ /\e\[31m\[ERROR\]\e\[0m hello: Action not found\./
|
279
|
+
ok {serr} !~ /\bactionrunner\.rb/
|
280
|
+
end
|
281
|
+
spec "[!89mz3] ignores backtrace of 'kernel_require.rb'." do
|
282
|
+
prepare_actionfile("a7613", "run_once('hello')")
|
283
|
+
sout, serr = capture_sio(tty: true) { @app.main(["a7613"]) }
|
284
|
+
ok {sout} == ""
|
285
|
+
ok {serr} =~ /\e\[31m\[ERROR\]\e\[0m hello: Action not found\./
|
286
|
+
ok {serr} !~ /\brequire\b/
|
287
|
+
end
|
288
|
+
spec "[!ttt98] ignores backtrace of 'arun'." do
|
289
|
+
prepare_actionfile("a5310", "run_once('hello')")
|
290
|
+
sout, serr = capture_sio(tty: true) { @app.main(["a5310"]) }
|
291
|
+
ok {sout} == ""
|
292
|
+
ok {serr} =~ /\e\[31m\[ERROR\]\e\[0m hello: Action not found\./
|
293
|
+
ok {serr} !~ /\barun:/
|
294
|
+
end
|
295
|
+
spec "[!z72yj] not ignore backtrace of others." do
|
296
|
+
prepare_actionfile("a6230", "run_once('hello')")
|
297
|
+
sout, serr = capture_sio(tty: true) { @app.main(["a6230"]) }
|
298
|
+
ok {sout} == ""
|
299
|
+
ok {serr} =~ /\e\[31m\[ERROR\]\e\[0m hello: Action not found\./
|
300
|
+
ok {serr} =~ /oktest/
|
301
|
+
end
|
302
|
+
}
|
303
|
+
|
304
|
+
topic('#load_action_file()') {
|
305
|
+
spec "[!nx22j] returns false if action file already loaded." do
|
306
|
+
prepare_actionfile("a0572")
|
307
|
+
ok {@app.instance_eval { load_action_file() }} == true
|
308
|
+
ok {@app.instance_eval { load_action_file() }} == false
|
309
|
+
ok {@app.instance_eval { load_action_file() }} == false
|
310
|
+
end
|
311
|
+
spec "[!aov55] loads action file if exists." do
|
312
|
+
prepare_actionfile("a2894")
|
313
|
+
ok {Benry::CmdApp::REGISTRY.metadata_exist?("a2894")} == false
|
314
|
+
@app.instance_eval { load_action_file() }
|
315
|
+
ok {Benry::CmdApp::REGISTRY.metadata_exist?("a2894")} == true
|
316
|
+
end
|
317
|
+
spec "[!ssmww] raises error when `required: true` and action file not exist." do
|
318
|
+
ok {@filename}.not_exist?
|
319
|
+
pr = proc { @app.instance_eval { load_action_file() } }
|
320
|
+
ok {pr}.raise?(Benry::CmdApp::CommandError,
|
321
|
+
"Action file ('Actionfile.rb') not found. Create it by `arun -g` command firstly.")
|
322
|
+
end
|
323
|
+
spec "[!vwtwe] it is AFTER loading action file to set global variables." do
|
324
|
+
prepare_actionfile("a5298", "puts $_a5298.inspect") # ... (A)
|
325
|
+
File.open(@filename, "a") do |f|
|
326
|
+
f << "puts $_a5298.inspect\n" # ... (B)
|
327
|
+
end
|
328
|
+
sout, serr = capture_sio() do
|
329
|
+
@app.instance_eval {
|
330
|
+
@global_vars = {"_a5298" => "a5298val"}
|
331
|
+
load_action_file()
|
332
|
+
}
|
333
|
+
@app.main(["a5298", "Alice"])
|
334
|
+
end
|
335
|
+
ok {serr} == ""
|
336
|
+
ok {sout} == ("nil\n" + # ... (B)
|
337
|
+
"\"a5298val\"\n") # ... (A)
|
338
|
+
end
|
339
|
+
spec "[!8i55a] prevents to load action file more than once." do
|
340
|
+
prepare_actionfile("a8569")
|
341
|
+
File.open(@filename, "a") do |f|
|
342
|
+
f << "$_a8569 += 1\n"
|
343
|
+
end
|
344
|
+
$_a8569 = 0
|
345
|
+
@app.instance_eval { load_action_file() }
|
346
|
+
ok {$_a8569} == 1
|
347
|
+
@app.instance_eval { load_action_file() }
|
348
|
+
ok {$_a8569} == 1
|
349
|
+
ok {$_a8569} != 2
|
350
|
+
end
|
351
|
+
spec "[!f68yv] returns true if action file loaded successfully." do
|
352
|
+
prepare_actionfile("a9586")
|
353
|
+
ret1 = @app.instance_eval { load_action_file() }
|
354
|
+
ok {ret1} == true
|
355
|
+
ret2 = @app.instance_eval { load_action_file() }
|
356
|
+
ok {ret2} == false
|
357
|
+
ret3 = @app.instance_eval { load_action_file() }
|
358
|
+
ok {ret3} == false
|
359
|
+
end
|
360
|
+
}
|
361
|
+
|
362
|
+
topic('#generate_action_file()') {
|
363
|
+
spec "[!dta7r] generates action file." do
|
364
|
+
ok {@filename}.not_exist?
|
365
|
+
@app.instance_eval { generate_action_file(quiet: true) }
|
366
|
+
ok {@filename}.file_exist?
|
367
|
+
end
|
368
|
+
spec "[!tmlqt] prints action file content if filename is '-'." do
|
369
|
+
ok {@filename}.not_exist?
|
370
|
+
sout = main "-g", "-f", "-"
|
371
|
+
ok {@filename}.not_exist?
|
372
|
+
ok {sout} =~ /^include Benry::ActionRunner::Export$/
|
373
|
+
end
|
374
|
+
spec "[!ymrjh] prints action file content if stdout is not a tty." do
|
375
|
+
ok {@filename}.not_exist?
|
376
|
+
sout, serr = capture_sio(tty: false) do
|
377
|
+
@app.instance_eval { generate_action_file() }
|
378
|
+
end
|
379
|
+
ok {@filename}.not_exist?
|
380
|
+
ok {serr} == ""
|
381
|
+
ok {sout} =~ /^include Benry::ActionRunner::Export$/
|
382
|
+
end
|
383
|
+
spec "[!9e3c0] returns nil if action file is not generated." do
|
384
|
+
ret = nil
|
385
|
+
capture_sio(tty: false) do
|
386
|
+
@app.instance_eval {
|
387
|
+
@action_file = "-"
|
388
|
+
ret = generate_action_file(quiet: true)
|
389
|
+
}
|
390
|
+
end
|
391
|
+
ok {ret} == nil
|
392
|
+
end
|
393
|
+
spec "[!685cq] raises error if action file already exist." do
|
394
|
+
prepare_actionfile("a1697")
|
395
|
+
ok {@filename}.file_exist?
|
396
|
+
pr = proc do
|
397
|
+
@app.instance_eval { generate_action_file(quiet: true) }
|
398
|
+
end
|
399
|
+
ok {pr}.raise?(Benry::CmdApp::CommandError,
|
400
|
+
"Action file ('Actionfile.rb') already exists. If you want to generate a new one, delete it first.")
|
401
|
+
end
|
402
|
+
spec "[!n09pl] reports result if action file generated successfully." do
|
403
|
+
sout, serr = capture_sio(tty: true) do
|
404
|
+
@app.instance_eval { generate_action_file() }
|
405
|
+
end
|
406
|
+
ok {serr} == ""
|
407
|
+
ok {sout} == "[OK] Action file 'Actionfile.rb' generated.\n"
|
408
|
+
end
|
409
|
+
spec "[!iq0p2] reports nothing if option '-q' specified." do
|
410
|
+
sout, serr = capture_sio(tty: true) do
|
411
|
+
@app.instance_eval { generate_action_file(quiet: true) }
|
412
|
+
end
|
413
|
+
ok {serr} == ""
|
414
|
+
ok {sout} == ""
|
415
|
+
end
|
416
|
+
spec "[!bf60l] returns action file name if generated." do
|
417
|
+
ret = @app.instance_eval { generate_action_file(quiet: true) }
|
418
|
+
ok {ret} == @filename
|
419
|
+
end
|
420
|
+
}
|
421
|
+
|
422
|
+
end
|
423
|
+
|
424
|
+
|
425
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
|
5
|
+
require_relative './shared'
|
6
|
+
|
7
|
+
|
8
|
+
Oktest.scope do
|
9
|
+
|
10
|
+
before_all do
|
11
|
+
TestHelperModule.setup_all()
|
12
|
+
end
|
13
|
+
|
14
|
+
after_all do
|
15
|
+
TestHelperModule.teardown_all()
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
topic Benry::ActionRunner::Brownie do
|
20
|
+
|
21
|
+
before do
|
22
|
+
@filename = "Actionfile.rb"
|
23
|
+
$LOADED_FEATURES.delete(File.absolute_path(@filename))
|
24
|
+
config = Benry::ActionRunner::CONFIG
|
25
|
+
@brownie = Benry::ActionRunner::Brownie.new(config)
|
26
|
+
@registry = Benry::CmdApp::REGISTRY
|
27
|
+
end
|
28
|
+
|
29
|
+
after do
|
30
|
+
File.unlink(@filename) if File.exist?(@filename)
|
31
|
+
clear_registry()
|
32
|
+
end
|
33
|
+
|
34
|
+
def cd_tmpdir(dirpath, &b)
|
35
|
+
xs = dirpath.split("/")
|
36
|
+
(1..(xs.length)).each do |len|
|
37
|
+
dir = xs[0, len].join("/")
|
38
|
+
dummy_dir(dir)
|
39
|
+
end
|
40
|
+
pwd = Dir.pwd()
|
41
|
+
Dir.chdir(dirpath)
|
42
|
+
yield
|
43
|
+
ensure
|
44
|
+
Dir.chdir(pwd)
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
topic('#search_and_load_action_file()') {
|
49
|
+
#
|
50
|
+
spec "[!c9e1h] if action file exists in current dir, load it regardless of options." do
|
51
|
+
prepare_actionfile("a2071")
|
52
|
+
ok {@registry.metadata_exist?("a2071")} == false
|
53
|
+
@brownie.search_and_load_action_file(@filename, true, false)
|
54
|
+
ok {@registry.metadata_exist?("a2071")} == true
|
55
|
+
end
|
56
|
+
#
|
57
|
+
spec "[!m5oj7] if action file exists in parent dir, find and load it if option '-s' specified." do
|
58
|
+
prepare_actionfile("a4290")
|
59
|
+
cd_tmpdir("foo/bar") do
|
60
|
+
ok {@registry.metadata_exist?("a4290")} == false
|
61
|
+
@brownie.search_and_load_action_file(@filename, true, false)
|
62
|
+
ok {@registry.metadata_exist?("a4290")} == true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
#
|
66
|
+
spec "[!079xs] returns nil if action file not found." do
|
67
|
+
ok {@filename}.not_exist?
|
68
|
+
ret = @brownie.search_and_load_action_file(@filename, true, false)
|
69
|
+
ok {ret} == nil
|
70
|
+
end
|
71
|
+
#
|
72
|
+
spec "[!7simq] changes current dir to where action file exists if option '-w' specified." do
|
73
|
+
prepare_actionfile("a0561", "puts Dir.pwd")
|
74
|
+
pwd1 = Dir.pwd()
|
75
|
+
cd_tmpdir("foo/bar") do
|
76
|
+
pwd2 = Dir.pwd()
|
77
|
+
ok {pwd2} == File.join(pwd1, "foo/bar")
|
78
|
+
sout = arun "-uw", "a0561"
|
79
|
+
ok {sout} == <<~"END"
|
80
|
+
$ cd ../..
|
81
|
+
#{pwd1}
|
82
|
+
END
|
83
|
+
end
|
84
|
+
end
|
85
|
+
#
|
86
|
+
spec "[!dg2qv] action file can has directory path." do
|
87
|
+
prepare_actionfile("a5767", "puts Dir.pwd")
|
88
|
+
pwd1 = Dir.pwd()
|
89
|
+
cd_tmpdir("foo/bar") do
|
90
|
+
sout = arun "-uw", "-f", "../Actionfile.rb", "a5767"
|
91
|
+
ok {sout} == <<~"END"
|
92
|
+
$ cd ..
|
93
|
+
#{File.join(pwd1, "foo")}
|
94
|
+
END
|
95
|
+
end
|
96
|
+
end
|
97
|
+
#
|
98
|
+
spec "[!d987b] loads action file if exists." do
|
99
|
+
prepare_actionfile("a5626")
|
100
|
+
cd_tmpdir("foo/bar") do
|
101
|
+
ok {@registry.metadata_exist?("a5626")} == false
|
102
|
+
sout = arun "-uw", "a5626", "Bob"
|
103
|
+
ok {sout} == <<~"END"
|
104
|
+
$ cd ../..
|
105
|
+
Hi, Bob!
|
106
|
+
END
|
107
|
+
end
|
108
|
+
end
|
109
|
+
#
|
110
|
+
spec "[!x9xxl] returns absolute path of action file if exists." do
|
111
|
+
prepare_actionfile("a4156")
|
112
|
+
pwd = Dir.pwd()
|
113
|
+
$BENRY_ECHOBACK = false
|
114
|
+
cd_tmpdir("foo/bar") do
|
115
|
+
ok {@registry.metadata_exist?("a4156")} == false
|
116
|
+
ret = @brownie.search_and_load_action_file(@filename, true, true)
|
117
|
+
ok {@registry.metadata_exist?("a4156")} == true
|
118
|
+
ok {ret} == File.join(pwd, "Actionfile.rb")
|
119
|
+
end
|
120
|
+
$BENRY_ECHOBACK = true
|
121
|
+
end
|
122
|
+
}
|
123
|
+
|
124
|
+
topic('#populate_global_variables()') {
|
125
|
+
#
|
126
|
+
spec "[!cr2ph] sets global variables." do
|
127
|
+
gvars = {"g5933x" => "ABC", "g5933y" => "123"}
|
128
|
+
@brownie.populate_global_variables(gvars)
|
129
|
+
ok {$g5933x} == "ABC"
|
130
|
+
ok {$g5933y} == 123
|
131
|
+
end
|
132
|
+
#
|
133
|
+
spec "[!3kow3] normalizes global variable names." do
|
134
|
+
gvars = {"g8258-vari-@able" => "ABC"}
|
135
|
+
@brownie.populate_global_variables(gvars)
|
136
|
+
ok {$g8258_vari__able} == "ABC"
|
137
|
+
end
|
138
|
+
#
|
139
|
+
spec "[!03x7t] decodes JSON string into Ruby object." do
|
140
|
+
gvars = {"g6950" => "[123, true, null]"}
|
141
|
+
@brownie.populate_global_variables(gvars)
|
142
|
+
ok {$g6950} == [123, true, nil]
|
143
|
+
end
|
144
|
+
#
|
145
|
+
spec "[!1ol4a] print global variables if debug mode is on." do
|
146
|
+
gvars = {"g7091" => '{"a": 123}'}
|
147
|
+
$DEBUG_MODE = true
|
148
|
+
at_end { $DEBUG_MODE = nil }
|
149
|
+
sout, serr = capture_sio do
|
150
|
+
@brownie.populate_global_variables(gvars)
|
151
|
+
end
|
152
|
+
ok {serr} == ""
|
153
|
+
ok {sout} == "[DEBUG] $g7091 = {\"a\"=>123}\n"
|
154
|
+
end
|
155
|
+
}
|
156
|
+
|
157
|
+
topic('#_decode_value()') {
|
158
|
+
#
|
159
|
+
spec "[!omxyf] decodes string as JSON format." do
|
160
|
+
str = '["ABC", 123, true, null]'
|
161
|
+
val = @brownie.instance_eval { _decode_value(str) }
|
162
|
+
ok {val} == ["ABC", 123, true, nil]
|
163
|
+
end
|
164
|
+
#
|
165
|
+
spec "[!tvwvn] returns string as it is if failed to parse as JSON." do
|
166
|
+
str = 'nil'
|
167
|
+
val = @brownie.instance_eval { _decode_value(str) }
|
168
|
+
ok {val} == "nil"
|
169
|
+
end
|
170
|
+
}
|
171
|
+
|
172
|
+
topic('#_debug_global_var()') {
|
173
|
+
#
|
174
|
+
spec "[!05l5f] prints var name and it's value." do
|
175
|
+
sout, serr = capture_sio(tty: false) do
|
176
|
+
@brownie.instance_eval { _debug_global_var("var1", ["val1"]) }
|
177
|
+
end
|
178
|
+
ok {serr} == ""
|
179
|
+
ok {sout} == "[DEBUG] $var1 = [\"val1\"]\n"
|
180
|
+
end
|
181
|
+
#
|
182
|
+
spec "[!7lwvz] colorizes output if color mode enabled." do
|
183
|
+
sout, serr = capture_sio(tty: true) do
|
184
|
+
@brownie.instance_eval { _debug_global_var("var1", ["val1"]) }
|
185
|
+
end
|
186
|
+
ok {serr} == ""
|
187
|
+
ok {sout} == "\e[2m[DEBUG] $var1 = [\"val1\"]\e[0m\n"
|
188
|
+
end
|
189
|
+
}
|
190
|
+
|
191
|
+
topic('#render_action_file_content()') {
|
192
|
+
#
|
193
|
+
spec "[!oc03q] returns content of action file." do
|
194
|
+
filename = "Actionfile.rb"
|
195
|
+
s = @brownie.render_action_file_content(filename)
|
196
|
+
ok {s} != ""
|
197
|
+
dummy_file(filename, s)
|
198
|
+
output = `ruby -wc #{filename}`
|
199
|
+
ok {output} == "Syntax OK\n"
|
200
|
+
end
|
201
|
+
}
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
end
|
data/test/help_test.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
|
5
|
+
require_relative './shared'
|
6
|
+
|
7
|
+
|
8
|
+
Oktest.scope do
|
9
|
+
|
10
|
+
before_all do
|
11
|
+
TestHelperModule.setup_all()
|
12
|
+
end
|
13
|
+
|
14
|
+
after_all do
|
15
|
+
TestHelperModule.teardown_all()
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
topic Benry::ActionRunner::ApplicationHelpBuilderModule do
|
20
|
+
|
21
|
+
topic('#section_options()') {
|
22
|
+
topic "option '-h, '--help'" do
|
23
|
+
spec "[!xsfzi] adds '--<name>=<value>' to help message." do
|
24
|
+
sout = arun "-h"
|
25
|
+
ok {sout} =~ /^ --<name>=<value> : set a global variable \(value can be in JSON format\)$/
|
26
|
+
end
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|