benry-actionrunner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/main_test.rb ADDED
@@ -0,0 +1,322 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require_relative './shared'
4
+
5
+
6
+ Oktest.scope do
7
+
8
+ before_all do
9
+ TestHelperModule.setup_all()
10
+ end
11
+
12
+ after_all do
13
+ TestHelperModule.teardown_all()
14
+ end
15
+
16
+
17
+ HELP_MESSAGE_FULL = <<"END"
18
+ \e[1marun\e[0m \e[2m(0.0.0)\e[0m --- Action runner (or task runner), much better than Rake
19
+
20
+ \e[1;34mUsage:\e[0m
21
+ $ \e[1marun\e[0m [<options>] <action> [<arguments>...]
22
+
23
+ \e[1;34mOptions:\e[0m
24
+ -h, --help : print help message (of action if specified)
25
+ -V : print version
26
+ -l : list actions
27
+ -L <topic> : topic list (actions|aliases|prefixes|abbrevs)
28
+ -a : list all actions/options including hidden ones
29
+ -f <file> : actionfile name (default: 'Actionfile.rb')
30
+ -u : search for actionfile in parent or upper dir
31
+ -w : change current dir to where action file exists
32
+ \e[2m -s : same as '-up'\e[0m
33
+ -g : generate actionfile ('Actionfile.rb') with example code
34
+ -v : verbose mode
35
+ -q : quiet mode
36
+ -c : enable color mode
37
+ -C : disable color mode
38
+ -D : debug mode
39
+ -T : trace mode
40
+ -X : dry-run mode (not run; just echoback)
41
+ --<name>=<value> : set a global variable (value can be in JSON format)
42
+
43
+ \e[1;34mActions:\e[0m
44
+ build : create all
45
+ \e[2m build:prepare : prepare directory\e[0m
46
+ build:zip : create zip file
47
+ clean : delete garbage files (and product files too if '-a')
48
+ git:stage : put changes of files into staging area
49
+ \e[2m (alias: stage)\e[0m
50
+ git:staged : show changes in staging area
51
+ \e[2m (alias: staged)\e[0m
52
+ git:status : show status in compact format
53
+ git:status:here : show status of current directory
54
+ \e[2m (alias: git)\e[0m
55
+ git:unstage : remove changes from staging area
56
+ \e[2m (alias: unstage)\e[0m
57
+ hello : print greeting message
58
+ help : print help message (of action if specified)
59
+
60
+ \e[1;34mExample:\e[0m
61
+ $ arun -h | less # print help message
62
+ $ arun -g # generate action file ('Actionfile.rb')
63
+ $ less Actionfile.rb # confirm action file
64
+ $ arun # list actions (or: `arun -l`)
65
+ $ arun -h hello # show help message for 'hello' action
66
+ $ arun hello Alice # run 'hello' action with arguments
67
+ Hello, Alice!
68
+ $ arun hello Alice -l fr # run 'hello' action with args and options
69
+ Bonjour, Alice!
70
+ $ arun : # list prefixes of actions (or '::', ':::')
71
+ $ arun xxxx: # list actions starting with 'xxxx:'
72
+
73
+ \e[1;34mDocument:\e[0m
74
+ https://kwatch.github.io/benry-ruby/benry-actionrunner.html
75
+ END
76
+ HELP_MESSAGE = HELP_MESSAGE_FULL.gsub(/^\e\[2m \S.*\e\[0m\n/, '')
77
+
78
+ HELP_NOFILE_FULL = HELP_MESSAGE_FULL\
79
+ .split(/^\e\[1;34mActions:\e\[0m\n.*?\n\n/m)\
80
+ .join("\e[1;34mActions:\e[0m\n"\
81
+ " help : print help message (of action if specified)\n"\
82
+ "\n")
83
+ HELP_NOFILE = HELP_NOFILE_FULL.gsub(/^\e\[2m \S.*\e\[0m\n/, '')
84
+
85
+ alias_list = <<"END"
86
+
87
+ \e[1;34mAliases:\e[0m
88
+ stage : alias for 'git:stage'
89
+ staged : alias for 'git:staged'
90
+ git : alias for 'git:status:here'
91
+ unstage : alias for 'git:unstage'
92
+ END
93
+
94
+ ACTION_LIST_FULL = (HELP_MESSAGE_FULL =~ /^(\e\[1;34mActions:\e\[0m\n.*?\n)\n/m) && ($1 + alias_list)
95
+ ACTION_LIST = ACTION_LIST_FULL.gsub(/^\e\[2m \S.*\e\[0m\n/, '')
96
+
97
+ ACTION_LIST_WITH_PREFIX = <<"END"
98
+ \e[1;34mActions:\e[0m
99
+ git:stage : put changes of files into staging area
100
+ \e[2m (alias: stage)\e[0m
101
+ git:staged : show changes in staging area
102
+ \e[2m (alias: staged)\e[0m
103
+ git:status : show status in compact format
104
+ git:status:here : show status of current directory
105
+ \e[2m (alias: git)\e[0m
106
+ git:unstage : remove changes from staging area
107
+ \e[2m (alias: unstage)\e[0m
108
+
109
+ \e[1;34mAliases:\e[0m
110
+ stage : alias for 'git:stage'
111
+ staged : alias for 'git:staged'
112
+ git : alias for 'git:status:here'
113
+ unstage : alias for 'git:unstage'
114
+ END
115
+
116
+ HELP_OF_HELP_ACTION = <<"END"
117
+ \e[1marun help\e[0m --- print help message (of action if specified)
118
+
119
+ \e[1;34mUsage:\e[0m
120
+ $ \e[1marun help\e[0m [<options>] [<action>]
121
+
122
+ \e[1;34mOptions:\e[0m
123
+ -a, --all : show all options, including private ones
124
+ END
125
+
126
+ HELP_OF_HELLO_ACTION = <<"END"
127
+ \e[1marun hello\e[0m --- print greeting message
128
+
129
+ \e[1;34mUsage:\e[0m
130
+ $ \e[1marun hello\e[0m [<options>] [<name>]
131
+
132
+ \e[1;34mOptions:\e[0m
133
+ -l, --lang=<lang> : language (en/fr/it)
134
+ END
135
+
136
+
137
+ topic Benry::ActionRunner do
138
+
139
+ fixture :fname do
140
+ Benry::ActionRunner::DEFAULT_FILENAME
141
+ end
142
+
143
+ fixture :actionfile do |fname|
144
+ if ! File.exist?(fname)
145
+ main("-g")
146
+ #capture_sio { main("-g") }
147
+ #at_exit { File.unlink(fname) if File.exist?(fname) }
148
+ end
149
+ fname
150
+ end
151
+
152
+ fixture :noactionfile do |fname|
153
+ File.unlink(fname) if File.exist?(fname)
154
+ nil
155
+ end
156
+
157
+
158
+ topic('.main()') {
159
+
160
+ def main(*args)
161
+ sout, serr = capture_sio(tty: true) do
162
+ Benry::ActionRunner.main(args)
163
+ end
164
+ ok {serr} == ""
165
+ return sout
166
+ end
167
+
168
+ def main!(*args)
169
+ sout, serr = capture_sio(tty: true) do
170
+ Benry::ActionRunner.main(args)
171
+ end
172
+ ok {sout} == ""
173
+ return serr
174
+ end
175
+
176
+ case_when("when action file not exist...") {
177
+
178
+ before do
179
+ @fname = Benry::ActionRunner::DEFAULT_FILENAME
180
+ File.unlink(@fname) if File.exist?(@fname)
181
+ end
182
+
183
+ spec "option '-h' or '--help' prints help message without actions." do
184
+ ok {main("-h")} == HELP_NOFILE
185
+ ok {main("--help")} == HELP_NOFILE
186
+ end
187
+
188
+ spec "option '-h' and '--help' can take an action name." do
189
+ ok {main("-h", "help")} == HELP_OF_HELP_ACTION
190
+ ok {main("--help", "help")} == HELP_OF_HELP_ACTION
191
+ end
192
+
193
+ spec "option '-v' prints version number." do
194
+ ok {main("-V")} == Benry::ActionRunner::VERSION + "\n"
195
+ end
196
+
197
+ spec "option '-l' cannot list action names." do
198
+ ok {main!("-l")} == "\e[31m[ERROR]\e[0m Action file ('#{@fname}') not found. Create it by `arun -g` command firstly.\n"
199
+ end
200
+
201
+ spec "option '-g' generates action file." do
202
+ ok {@fname}.not_exist?
203
+ ok {main("-g")} == "[OK] Action file '#{@fname}' generated.\n"
204
+ ok {@fname}.file_exist?
205
+ end
206
+
207
+ spec "action 'help' can be run." do
208
+ ok {main("help")} == HELP_NOFILE
209
+ ok {main("help", "-a")} == HELP_NOFILE_FULL
210
+ ok {main("help", "help")} == HELP_OF_HELP_ACTION
211
+ end
212
+
213
+ spec "no arguments specified reports error message." do
214
+ ok {main!()} == "\e[31m[ERROR]\e[0m Action file ('#{@fname}') not found. Create it by `arun -g` command firstly.\n"
215
+ end
216
+
217
+ spec "option '-a' without any arguments reports error message." do
218
+ ok {main!("-a")} == "\e[31m[ERROR]\e[0m Action file ('#{@fname}') not found. Create it by `arun -g` command firstly.\n"
219
+ end
220
+
221
+ spec "prefix name specified reports error message." do
222
+ ok {main!("git:")} == "\e[31m[ERROR]\e[0m Action file ('#{@fname}') not found. Create it by `arun -g` command firstly.\n"
223
+ end
224
+
225
+ }
226
+
227
+ case_else("else...") {
228
+
229
+ before do
230
+ @fname = Benry::ActionRunner::DEFAULT_FILENAME
231
+ unless File.exist?(@fname)
232
+ #capture_sio { Benry::ActionRunner.main("-g") }
233
+ config = Benry::ActionRunner::CONFIG
234
+ app = Benry::ActionRunner::MainApplication.new(config)
235
+ app.__send__(:generate_action_file, quiet: true)
236
+ end
237
+ end
238
+
239
+ spec "option '-h' or '--help' prints help message." do
240
+ ok {main("-h")} == HELP_MESSAGE
241
+ ok {main("--help")} == HELP_MESSAGE
242
+ end
243
+
244
+ spec "option '-h' an '--help' can take an action name." do
245
+ ok {main("-h", "hello")} == HELP_OF_HELLO_ACTION
246
+ ok {main("--help", "hello")} == HELP_OF_HELLO_ACTION
247
+ end
248
+
249
+ spec "option '-V' prints version." do
250
+ ok {main("-V")} == Benry::ActionRunner::VERSION + "\n"
251
+ end
252
+
253
+ spec "option '-l' lists action names." do
254
+ ok {main("-l")} == ACTION_LIST
255
+ end
256
+
257
+ spec "option '-a' includes hidden actions into help message." do
258
+ ok {main("-ha")} == HELP_MESSAGE_FULL
259
+ ok {main("-la")} == ACTION_LIST_FULL
260
+ end
261
+
262
+ spec "option '-g' reports error message." do
263
+ ok {main!("-g")} == "\e[31m[ERROR]\e[0m Action file ('#{@fname}') already exists. If you want to generate a new one, delete it first.\n"
264
+ end
265
+
266
+ spec "no arguments specified lists action names." do
267
+ ok {main()} == ACTION_LIST
268
+ end
269
+
270
+ spec "option '-a' without any arguments lists action including hidden ones." do
271
+ ok {main("-a")} == ACTION_LIST_FULL
272
+ end
273
+
274
+ spec "run action with options and args." do
275
+ ok {main("hello", "-lfr", "Alice")} == "Bonjour, Alice!\n"
276
+ end
277
+
278
+ spec "prefix name lists action names starting with prefix." do
279
+ ok {main("git:")} == ACTION_LIST_WITH_PREFIX
280
+ end
281
+
282
+ spec "long options are recognized as global variable values." do
283
+ BuildAction.class_eval do
284
+ @action.("show global variables")
285
+ def gvars1()
286
+ puts "$project=#{$project.inspect}, $release=#{$release.inspect}"
287
+ end
288
+ end
289
+ at_end { Benry::CmdApp.undef_action("build:gvars1") }
290
+ expected = "$project=\"mysample1\", $release=\"3.0.0\"\n"
291
+ ok {main("--project=mysample1", "--release=3.0.0", "build:gvars1")} == expected
292
+ end
293
+
294
+ spec "long option value is parsed as JSON string." do
295
+ BuildAction.class_eval do
296
+ @action.("show global variables")
297
+ def gvars2()
298
+ puts "$num=#{$num.inspect}, $str=#{$str.inspect}, $arr=#{$arr.inspect}"
299
+ end
300
+ end
301
+ at_end { Benry::CmdApp.undef_action("build:gvars2") }
302
+ expected = "$num=123, $str=\"foo\", $arr=[123, true, nil]\n"
303
+ ok {main("--num=123", "--str=foo", "--arr=[123,true,null]", "build:gvars2")} == expected
304
+ end
305
+
306
+ spec "long options are displayed in debug mode." do
307
+ sout = main("-lD", "--num=123", "--str=foo", "--arr=[123,true,null]")
308
+ ok {sout}.start_with?(<<"END")
309
+ \e[2m[DEBUG] $num = 123\e[0m
310
+ \e[2m[DEBUG] $str = \"foo\"\e[0m
311
+ END
312
+ end
313
+
314
+ }
315
+
316
+ }
317
+
318
+
319
+ end
320
+
321
+
322
+ end
data/test/run_all.rb ADDED
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ Dir.glob(File.dirname(__FILE__) + '/**/*_test.rb').each do |filename|
5
+ require File.absolute_path(filename)
6
+ end
data/test/shared.rb ADDED
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+ $0 = "/usr/local/bin/arun"
5
+
6
+ require 'oktest'
7
+
8
+ require 'benry/actionrunner'
9
+
10
+
11
+
12
+ Oktest.global_scope do
13
+
14
+
15
+ def arun(*args)
16
+ sout, serr, status = arun!(*args)
17
+ ok {serr} == ""
18
+ ok {status} == 0
19
+ return sout
20
+ end
21
+
22
+ def arun!(*args)
23
+ #status = nil
24
+ #sout, serr = capture_sio(tty: true) do
25
+ # status = Benry::ActionRunner.main(args)
26
+ #end
27
+ sout = serr = nil
28
+ out = File.open("_sout", "w+", encoding: 'utf-8')
29
+ err = File.open("_serr", "w+", encoding: 'utf-8')
30
+ system "arun", *args, out: out, err: err
31
+ err.rewind(); serr = err.read(); err.rewind(); err.truncate(0); err.close()
32
+ out.rewind(); sout = out.read(); out.rewind(); out.truncate(0); out.close()
33
+ status = $?.to_i
34
+ return sout, serr, status
35
+ ensure
36
+ File.unlink "_sout" if File.exist?("_sout")
37
+ File.unlink "_serr" if File.exist?("_serr")
38
+ end
39
+
40
+ def prepare_actionfile(action, execute=nil)
41
+ execute ||= "puts \"Hi, \#{name}!\""
42
+ content = <<"END"
43
+ require 'benry/actionrunner'
44
+ include Benry::ActionRunner::Export
45
+ class MyAction < Action
46
+ @action.("test")
47
+ def #{action}(name="world")
48
+ #{execute}
49
+ end
50
+ end
51
+ END
52
+ File.write("Actionfile.rb", content, encoding: 'utf-8')
53
+ end
54
+
55
+ def clear_registry()
56
+ Benry::CmdApp::REGISTRY.instance_eval do
57
+ help_action = @metadata_dict["help"]
58
+ @metadata_dict.clear()
59
+ @metadata_dict["help"] = help_action
60
+ @category_dict.clear()
61
+ @abbrev_dict.clear()
62
+ end
63
+ $LOADED_FEATURES.delete(File.absolute_path("Actionfile.rb"))
64
+ Benry::ActionRunner::CONFIG.trace_mode = nil
65
+ end
66
+
67
+
68
+ end
69
+
70
+
71
+ module TestHelperModule
72
+ module_function
73
+
74
+ def setup_all()
75
+ fname = Benry::ActionRunner::DEFAULT_FILENAME
76
+ if File.exist?(fname)
77
+ File.rename(fname, "__" + fname)
78
+ end
79
+ @_pwd = Dir.pwd()
80
+ Dir.chdir "test"
81
+ end
82
+
83
+ def teardown_all()
84
+ fname = Benry::ActionRunner::DEFAULT_FILENAME
85
+ File.unlink(fname) if File.exist?(fname)
86
+ Dir.chdir @_pwd
87
+ if File.exist?("__" + fname)
88
+ File.rename("__" + fname, fname)
89
+ end
90
+ end
91
+
92
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benry-actionrunner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kwatch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: benry-cmdapp
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: benry-unixcommand
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oktest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
55
+ description: |
56
+ Benry-ActionRunner is a Action runner or Task runner, like Rake or Gulp.
57
+
58
+ Compared to Rake, actions of Benry-ActionRunner can take their own options and arguments.
59
+ For example, `arun hello --lang=fr Alice` runs `hello` action with an option `--lang=fr` and an argument `Alice`.
60
+
61
+ Benry-ActionRunner is also an example application of Benry-CmdApp framework.
62
+
63
+ See https://kwatch.github.io/benry-ruby/benry-actionrunner.html for details.
64
+ email: kwatch@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - CHANGES.md
70
+ - MIT-LICENSE
71
+ - README.md
72
+ - benry-actionrunner.gemspec
73
+ - bin/arun
74
+ - lib/benry/actionrunner.rb
75
+ - test/app_test.rb
76
+ - test/brownie_test.rb
77
+ - test/help_test.rb
78
+ - test/main_test.rb
79
+ - test/run_all.rb
80
+ - test/shared.rb
81
+ homepage: https://kwatch.github.io/benry-ruby/benry-actionrunner.html
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '2.3'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.4.10
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Action runner or Task runner, like Rake or Gulp.
104
+ test_files:
105
+ - test/run_all.rb