rbatch 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/CHANGELOG +25 -0
  2. data/LICENSE +0 -0
  3. data/README.ja.md +280 -0
  4. data/README.md +280 -0
  5. data/Rakefile +66 -0
  6. data/VERSION +1 -0
  7. data/doc/rdoc/CHANGELOG.html +160 -0
  8. data/doc/rdoc/LICENSE.html +94 -0
  9. data/doc/rdoc/RBatch.html +476 -0
  10. data/doc/rdoc/RBatch/Cmd.html +329 -0
  11. data/doc/rdoc/RBatch/CmdException.html +154 -0
  12. data/doc/rdoc/RBatch/CmdResult.html +492 -0
  13. data/doc/rdoc/RBatch/Log.html +739 -0
  14. data/doc/rdoc/created.rid +8 -0
  15. data/doc/rdoc/images/brick.png +0 -0
  16. data/doc/rdoc/images/brick_link.png +0 -0
  17. data/doc/rdoc/images/bug.png +0 -0
  18. data/doc/rdoc/images/bullet_black.png +0 -0
  19. data/doc/rdoc/images/bullet_toggle_minus.png +0 -0
  20. data/doc/rdoc/images/bullet_toggle_plus.png +0 -0
  21. data/doc/rdoc/images/date.png +0 -0
  22. data/doc/rdoc/images/find.png +0 -0
  23. data/doc/rdoc/images/loadingAnimation.gif +0 -0
  24. data/doc/rdoc/images/macFFBgHack.png +0 -0
  25. data/doc/rdoc/images/package.png +0 -0
  26. data/doc/rdoc/images/page_green.png +0 -0
  27. data/doc/rdoc/images/page_white_text.png +0 -0
  28. data/doc/rdoc/images/page_white_width.png +0 -0
  29. data/doc/rdoc/images/plugin.png +0 -0
  30. data/doc/rdoc/images/ruby.png +0 -0
  31. data/doc/rdoc/images/tag_green.png +0 -0
  32. data/doc/rdoc/images/wrench.png +0 -0
  33. data/doc/rdoc/images/wrench_orange.png +0 -0
  34. data/doc/rdoc/images/zoom.png +0 -0
  35. data/doc/rdoc/index.html +124 -0
  36. data/doc/rdoc/js/darkfish.js +116 -0
  37. data/doc/rdoc/js/jquery.js +32 -0
  38. data/doc/rdoc/js/quicksearch.js +114 -0
  39. data/doc/rdoc/js/thickbox-compressed.js +10 -0
  40. data/doc/rdoc/lib/rbatch/cmd_rb.html +56 -0
  41. data/doc/rdoc/lib/rbatch/config_rb.html +56 -0
  42. data/doc/rdoc/lib/rbatch/log_rb.html +58 -0
  43. data/doc/rdoc/lib/rbatch_rb.html +58 -0
  44. data/doc/rdoc/rdoc.css +706 -0
  45. data/lib/rbatch.rb +51 -0
  46. data/lib/rbatch/cmd.rb +118 -0
  47. data/lib/rbatch/config.rb +29 -0
  48. data/lib/rbatch/log.rb +205 -0
  49. data/sample/bin/file_batch_copy.rb +9 -0
  50. data/sample/bin/openldap_backup.rb +7 -0
  51. data/sample/bin/test.rb +12 -0
  52. data/sample/config/file_batch_copy.yaml +5 -0
  53. data/sample/config/openldap_backup.yaml +2 -0
  54. data/sample/config/rbatch.yaml +68 -0
  55. data/sample/log/empty +0 -0
  56. data/test/cases/test_cmd.rb +120 -0
  57. data/test/cases/test_config.rb +34 -0
  58. data/test/cases/test_log.rb +594 -0
  59. data/test/config/rbatch.yaml +0 -0
  60. data/test/mocks/PrintArgs.exe +0 -0
  61. data/test/mocks/win_cmd.exe +0 -0
  62. metadata +173 -0
@@ -0,0 +1,68 @@
1
+ # RBatch config
2
+ #
3
+ # This file format is YAML
4
+ #
5
+
6
+ # -------------------
7
+ # Global setting
8
+ # -------------------
9
+
10
+ # Forbit Double Run
11
+ #
12
+ # Default : false
13
+ #
14
+ #forbid_double_run: true
15
+
16
+ # -------------------
17
+ # Cmd setting
18
+ # -------------------
19
+
20
+ # Raise Exception
21
+ #
22
+ # Default : false
23
+ #
24
+ # If command exit status is not 0, raise exception.
25
+ #
26
+ cmd_raise : true
27
+
28
+
29
+ # -------------------
30
+ # Log setting
31
+ # -------------------
32
+
33
+ # Log File Name
34
+ #
35
+ # Default : "<date>_<time>_<prog>.log"
36
+ #
37
+ # Reservation words
38
+ # <data> --> replace to YYYYMMDD date string
39
+ # <time> --> replace to hhmmss time string
40
+ # <prog> --> Program file base name (except extention)
41
+ #
42
+ #log_name : "<date>_<time>_<prog>.log"
43
+ log_name : "<date>_<prog>.log"
44
+
45
+ # Log Output Directory
46
+ #
47
+ # Default : "(Script path)/../log"
48
+ #
49
+ #log_dir : "/tmp/log"
50
+
51
+ # Append log or not
52
+ #
53
+ # Default : ture
54
+ #
55
+ #log_append : false
56
+
57
+ # Log Level
58
+ #
59
+ # Default : "info"
60
+ # Value : "debug","info","wran","error","fatal"
61
+ #
62
+ #log_level : "debug"
63
+
64
+ # Print log-string both file and STDOUT
65
+ #
66
+ # Default : false
67
+ #
68
+ log_stdout : true
File without changes
@@ -0,0 +1,120 @@
1
+ require 'test/unit'
2
+ require 'rbatch'
3
+
4
+ class RuncherTest < Test::Unit::TestCase
5
+ def setup
6
+ @config_dir = File.join(File.dirname(RBatch.program_name), "..", "config")
7
+ Dir::mkdir(@config_dir) if ! Dir.exists? @config_dir
8
+ confstr = ""
9
+ open( RBatch.common_config_path , "w" ){|f| f.write(confstr)}
10
+ end
11
+
12
+ def test_cmd_exists
13
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'"
14
+ assert_equal "1", result.stdout.chomp
15
+ assert_equal "2", result.stderr.chomp
16
+ assert_equal 0, result.status
17
+ end
18
+ def test_cmd_does_not_exist
19
+ assert_raise(Errno::ENOENT){
20
+ RBatch::cmd "not_exist_command"
21
+ }
22
+ end
23
+ def test_stdout_size_greater_than_65534
24
+ result = RBatch::cmd "ruby -e '100000.times{print 0}'"
25
+ assert_equal 100000, result.stdout.chomp.size
26
+ assert_equal "", result.stderr.chomp
27
+ assert_equal 0, result.status
28
+ end
29
+ def test_stdout_size_greater_than_65534_with_status_1
30
+ result = RBatch::cmd "ruby -e '100000.times{print 0}; exit 1'"
31
+ assert_equal 100000, result.stdout.chomp.size
32
+ assert_equal "", result.stderr.chomp
33
+ assert_equal 1, result.status
34
+ end
35
+ def test_status_code_is_1
36
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
37
+ assert_equal "1", result.stdout.chomp
38
+ assert_equal "2", result.stderr.chomp
39
+ assert_equal 1, result.status
40
+ end
41
+ def test_status_code_is_greater_than_256
42
+ returncode = 300
43
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit #{returncode};'"
44
+ assert_equal "1", result.stdout.chomp
45
+ assert_equal "2", result.stderr.chomp
46
+ case RUBY_PLATFORM
47
+ when /mswin|mingw/
48
+ assert_equal returncode, result.status
49
+ when /cygwin|linux/
50
+ assert_equal returncode % 256, result.status
51
+ end
52
+ end
53
+ def test_to_h
54
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
55
+ assert_equal "1", result.to_h[:stdout]
56
+ assert_equal "2", result.to_h[:stderr]
57
+ assert_equal 1 , result.to_h[:status]
58
+ end
59
+ def test_to_s
60
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
61
+ assert_equal "{:cmd_str=>\"ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'\", :stdout=>\"1\", :stderr=>\"2\", :status=>1}", result.to_s
62
+ end
63
+ def test_nil_command
64
+ assert_raise(RBatch::CmdException){
65
+ RBatch::Cmd.new(nil)
66
+ }
67
+ end
68
+ def test_fullcmd_exists
69
+ result = RBatch::Cmd.new("ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'").run
70
+ assert_equal "1", result.stdout.chomp
71
+ assert_equal "2", result.stderr.chomp
72
+ assert_equal 0, result.status
73
+ end
74
+ def test_opt_raise_true_status_1
75
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
76
+ opt = {:raise => true}
77
+ assert_raise(RBatch::CmdException){
78
+ RBatch::Cmd.new(cmd_str,opt).run
79
+ }
80
+ end
81
+ def test_opt_raise_false_status_1
82
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
83
+ opt = {:raise => false}
84
+ result = RBatch::Cmd.new(cmd_str,opt).run
85
+ assert_equal "1", result.stdout.chomp
86
+ assert_equal "2", result.stderr.chomp
87
+ assert_equal 1, result.status
88
+ end
89
+ def test_opt_raise_true_status_0
90
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'"
91
+ opt = {:raise => true}
92
+ result = RBatch::Cmd.new(cmd_str,opt).run
93
+ assert_equal "1", result.stdout.chomp
94
+ assert_equal "2", result.stderr.chomp
95
+ assert_equal 0, result.status
96
+ end
97
+ def test_opt_raise_false_status_0
98
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'"
99
+ opt = {:raise => false}
100
+ result = RBatch::Cmd.new(cmd_str,opt).run
101
+ assert_equal "1", result.stdout.chomp
102
+ assert_equal "2", result.stderr.chomp
103
+ assert_equal 0, result.status
104
+ end
105
+ def test_opt_raise_true_status_1_by_conf
106
+ confstr = "cmd_raise: true "
107
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
108
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
109
+ assert_raise(RBatch::CmdException){
110
+ RBatch::Cmd.new(cmd_str).run
111
+ }
112
+ end
113
+ def test_opt_raise_true_status_1_shortcut
114
+ cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
115
+ opt = {:raise => true}
116
+ assert_raise(RBatch::CmdException){
117
+ RBatch::cmd(cmd_str,opt)
118
+ }
119
+ end
120
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'rbatch'
3
+
4
+ class RuncherTest < Test::Unit::TestCase
5
+ def setup
6
+ @config_dir = File.join(File.dirname(RBatch.program_name), "..", "config")
7
+ @config_file = File.join(@config_dir , "test_config.yaml")
8
+ Dir::mkdir(@config_dir) if ! Dir.exists? @config_dir
9
+ end
10
+
11
+ def teardown
12
+ File.delete @config_file if File.exist? @config_file
13
+ end
14
+
15
+ def test_require
16
+ end
17
+
18
+ def test_config
19
+ open( @config_file , "w" ){|f| f.write("key: value")}
20
+ assert_equal "value", RBatch.config["key"]
21
+ end
22
+
23
+ def test_read_error
24
+ assert_raise(Errno::ENOENT){
25
+ RBatch.config
26
+ }
27
+ end
28
+
29
+ def test_double_read
30
+ open( @config_file , "w" ){|f| f.write("key: value")}
31
+ assert_equal "value", RBatch.config["key"]
32
+ assert_equal "value", RBatch.config["key"]
33
+ end
34
+ end
@@ -0,0 +1,594 @@
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+ require 'rbatch'
4
+ class LoggerTest < Test::Unit::TestCase
5
+ def setup
6
+ @dir = File.join(File.dirname(RBatch.program_name), "..", "log")
7
+ @dir2 = File.join(File.dirname(RBatch.program_name), "..", "log2")
8
+ @dir3 = File.join(File.dirname(RBatch.program_name), "..", "log3")
9
+ @config_dir = File.join(File.dirname(RBatch.program_name), "..", "config")
10
+ Dir::mkdir(@dir)if ! Dir.exists? @dir
11
+ Dir::mkdir(@dir2)if ! Dir.exists? @dir2
12
+ Dir::mkdir(@config_dir) if ! Dir.exists? @config_dir
13
+ # RBatch::Log.verbose = true
14
+ # set STDOUT Logger stop
15
+ confstr = "log_quiet: true\n"
16
+ open( RBatch.common_config_path , "w" ){|f| f.write(confstr)}
17
+
18
+ end
19
+
20
+ def teardown
21
+ File::delete(RBatch.common_config_path) if File.exists?(RBatch.common_config_path)
22
+ if Dir.exists? @dir
23
+ Dir::foreach(@dir) do |f|
24
+ File::delete(File.join(@dir , f)) if ! (/\.+$/ =~ f)
25
+ end
26
+ Dir::rmdir(@dir)
27
+ end
28
+ if Dir.exists? @dir2
29
+ Dir::foreach(@dir2) do |f|
30
+ File::delete(File.join(@dir2 , f)) if ! (/\.+$/ =~ f)
31
+ end
32
+ Dir::rmdir(@dir2)
33
+ end
34
+ end
35
+
36
+ def test_log
37
+ RBatch::Log.new do | log |
38
+ log.info("test_log")
39
+ end
40
+ Dir::foreach(@dir) do |f|
41
+ if ! (/\.+$/ =~ f)
42
+ File::open(File.join(@dir , f)) {|f|
43
+ assert_match /test_log/, f.read
44
+ }
45
+ end
46
+ end
47
+ end
48
+
49
+ def test_log_dir_doesnot_exist
50
+ Dir::rmdir(@dir)
51
+ assert_raise(Errno::ENOENT){
52
+ RBatch::Log.new {|log|}
53
+ }
54
+ Dir::mkdir(@dir)
55
+ end
56
+
57
+ def test_change_name_by_opt
58
+ RBatch::Log.new({:name => "name1.log" }) do | log |
59
+ log.info("test_change_name_by_opt")
60
+ end
61
+ File::open(File.join(@dir , "name1.log")) {|f|
62
+ assert_match /test_change_name_by_opt/, f.read
63
+ }
64
+ end
65
+
66
+ def test_change_name_by_opt2
67
+ RBatch::Log.new({:name => "<prog><date>name.log" }) do | log |
68
+ log.info("test_change_name_by_opt2")
69
+ end
70
+ File::open(File.join(@dir , "test_log" + Time.now.strftime("%Y%m%d") + "name.log")) {|f|
71
+ assert_match /test_change_name_by_opt2/, f.read
72
+ }
73
+ end
74
+
75
+ def test_change_name_by_opt3
76
+ RBatch::Log.new({:name => "<prog>-<date>-name.log" }) do | log |
77
+ log.info("test_change_name_by_opt2")
78
+ end
79
+ File::open(File.join(@dir , "test_log-" + Time.now.strftime("%Y%m%d") + "-name.log")) {|f|
80
+ assert_match /test_change_name_by_opt2/, f.read
81
+ }
82
+ end
83
+
84
+
85
+ def test_change_name_by_config
86
+ confstr = "log_name: name1"
87
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
88
+ RBatch::Log.new({:name => "name1.log" }) do | log |
89
+ log.info("test_change_name_by_config")
90
+ end
91
+ File::open(File.join(@dir , "name1.log")) {|f|
92
+ assert_match /test_change_name_by_config/, f.read
93
+ }
94
+ end
95
+
96
+
97
+ def test_change_log_dir_by_opt
98
+ RBatch::Log.new({:output_dir=> @dir2 }) do | log |
99
+ log.info("test_change_log_dir_by_opt")
100
+ end
101
+ Dir::foreach(@dir2) do |f|
102
+ if ! (/\.+$/ =~ f)
103
+ File::open(File.join(@dir2 , f)) {|f|
104
+ assert_match /test_change_log_dir_by_opt/, f.read
105
+ }
106
+ end
107
+ end
108
+ end
109
+
110
+ def test_change_log_dir_by_config
111
+ confstr = "log_dir: " + @dir2
112
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
113
+ RBatch::Log.new({:output_dir=> @dir2 }) do | log |
114
+ log.info("test_change_log_dir_by_config")
115
+ end
116
+ Dir::foreach(@dir2) do |f|
117
+ if ! (/\.+$/ =~ f)
118
+ File::open(File.join(@dir2 , f)) {|f|
119
+ assert_match /test_change_log_dir_by_config/, f.read
120
+ }
121
+ end
122
+ end
123
+ end
124
+
125
+ def test_change_formatte
126
+ RBatch::Log.new({:name => "file" , :formatter => proc { |severity, datetime, progname, msg| "test_change_formatte#{msg}\n" }}) do | log |
127
+ log.info("bar")
128
+ end
129
+ File::open(File.join(@dir,"file")) {|f| assert_match /test_change_formatte/, f.read }
130
+ end
131
+
132
+ def test_nest_block
133
+ RBatch::Log.new({:name => "name1" }) do | log |
134
+ log.info("name1")
135
+ RBatch::Log.new({:name => "name2" }) do | log |
136
+ log.info("name2")
137
+ end
138
+ end
139
+ File::open(File.join(@dir,"name1")) {|f| assert_match /name1/, f.read }
140
+ File::open(File.join(@dir,"name2")) {|f| assert_match /name2/, f.read }
141
+ end
142
+
143
+ def test_opt_overwite_config
144
+ confstr = "log_name: " + "name1"
145
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
146
+ RBatch::Log.new({:name => "name2" }) do | log |
147
+ log.info("test_opt_overwite_config")
148
+ end
149
+ File::open(File.join(@dir , "name2")) {|f|
150
+ assert_match /test_opt_overwite_config/, f.read
151
+ }
152
+ end
153
+
154
+ def test_append_by_opt
155
+ RBatch::Log.new({:append => true, :name => "test_append" }) do | log |
156
+ log.info("test_append1")
157
+ end
158
+ RBatch::Log.new({:append => true, :name => "test_append" }) do | log |
159
+ log.info("test_append2")
160
+ end
161
+ File::open(File.join(@dir , "test_append")) {|f|
162
+ str = f.read
163
+ assert_match /test_append1/, str
164
+ assert_match /test_append2/, str
165
+ }
166
+ end
167
+
168
+ def test_no_append_by_opt
169
+ RBatch::Log.new({:append => false, :name => "test_append" }) do | log |
170
+ log.info("test_append1")
171
+ end
172
+ RBatch::Log.new({:append => false, :name => "test_append" }) do | log |
173
+ log.info("test_append2")
174
+ end
175
+ File::open(File.join(@dir , "test_append")) {|f|
176
+ str = f.read
177
+ assert_no_match /test_append1/, str
178
+ assert_match /test_append2/, str
179
+ }
180
+ end
181
+
182
+
183
+ def test_append_by_conf
184
+ confstr = "log_append: true"
185
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
186
+
187
+ RBatch::Log.new({:name => "test_append" }) do | log |
188
+ log.info("test_append1")
189
+ end
190
+ RBatch::Log.new({:name => "test_append" }) do | log |
191
+ log.info("test_append2")
192
+ end
193
+ File::open(File.join(@dir , "test_append")) {|f|
194
+ str = f.read
195
+ assert_match /test_append1/, str
196
+ assert_match /test_append2/, str
197
+ }
198
+ end
199
+
200
+ def test_no_append_by_conf
201
+ confstr = "log_append: false"
202
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
203
+
204
+ RBatch::Log.new({ :name => "test_append" }) do | log |
205
+ log.info("test_append1")
206
+ end
207
+ RBatch::Log.new({ :name => "test_append" }) do | log |
208
+ log.info("test_append2")
209
+ end
210
+ File::open(File.join(@dir , "test_append")) {|f|
211
+ str = f.read
212
+ assert_no_match /test_append1/, str
213
+ assert_match /test_append2/, str
214
+ }
215
+ end
216
+
217
+ def test_log_level_default
218
+ RBatch::Log.new({ :name => "test_level" }) do | log |
219
+ log.debug("test_debug")
220
+ log.info("test_info")
221
+ log.warn("test_warn")
222
+ log.error("test_error")
223
+ log.fatal("test_fatal")
224
+ end
225
+ File::open(File.join(@dir , "test_level")) {|f|
226
+ str = f.read
227
+ assert_no_match /test_debug/, str
228
+ assert_match /test_info/, str
229
+ assert_match /test_warn/, str
230
+ assert_match /test_error/, str
231
+ assert_match /test_fatal/, str
232
+ }
233
+ end
234
+
235
+ def test_log_level_debug_by_opt
236
+ RBatch::Log.new({ :level => "debug",:name => "test_level" }) do | log |
237
+ log.debug("test_debug")
238
+ log.info("test_info")
239
+ log.warn("test_warn")
240
+ log.error("test_error")
241
+ log.fatal("test_fatal")
242
+ end
243
+ File::open(File.join(@dir , "test_level")) {|f|
244
+ str = f.read
245
+ assert_match /test_debug/, str
246
+ assert_match /test_info/, str
247
+ assert_match /test_warn/, str
248
+ assert_match /test_error/, str
249
+ assert_match /test_fatal/, str
250
+ }
251
+ end
252
+
253
+ def test_log_level_info_by_opt
254
+ RBatch::Log.new({ :level => "info",:name => "test_level" }) do | log |
255
+ log.debug("test_debug")
256
+ log.info("test_info")
257
+ log.warn("test_warn")
258
+ log.error("test_error")
259
+ log.fatal("test_fatal")
260
+ end
261
+ File::open(File.join(@dir , "test_level")) {|f|
262
+ str = f.read
263
+ assert_no_match /test_debug/, str
264
+ assert_match /test_info/, str
265
+ assert_match /test_warn/, str
266
+ assert_match /test_error/, str
267
+ assert_match /test_fatal/, str
268
+ }
269
+ end
270
+
271
+ def test_log_level_warn_by_opt
272
+ RBatch::Log.new({ :level => "warn",:name => "test_level" }) do | log |
273
+ log.debug("test_debug")
274
+ log.info("test_info")
275
+ log.warn("test_warn")
276
+ log.error("test_error")
277
+ log.fatal("test_fatal")
278
+ end
279
+ File::open(File.join(@dir , "test_level")) {|f|
280
+ str = f.read
281
+ assert_no_match /test_debug/, str
282
+ assert_no_match /test_info/, str
283
+ assert_match /test_warn/, str
284
+ assert_match /test_error/, str
285
+ assert_match /test_fatal/, str
286
+ }
287
+ end
288
+
289
+ def test_log_level_error_by_opt
290
+ RBatch::Log.new({ :level => "error",:name => "test_level" }) do | log |
291
+ log.debug("test_debug")
292
+ log.info("test_info")
293
+ log.warn("test_warn")
294
+ log.error("test_error")
295
+ log.fatal("test_fatal")
296
+ end
297
+ File::open(File.join(@dir , "test_level")) {|f|
298
+ str = f.read
299
+ assert_no_match /test_debug/, str
300
+ assert_no_match /test_info/, str
301
+ assert_no_match /test_warn/, str
302
+ assert_match /test_error/, str
303
+ assert_match /test_fatal/, str
304
+ }
305
+ end
306
+
307
+ def test_log_level_fatal_by_opt
308
+ RBatch::Log.new({ :level => "fatal",:name => "test_level" }) do | log |
309
+ log.debug("test_debug")
310
+ log.info("test_info")
311
+ log.warn("test_warn")
312
+ log.error("test_error")
313
+ log.fatal("test_fatal")
314
+ end
315
+ File::open(File.join(@dir , "test_level")) {|f|
316
+ str = f.read
317
+ assert_no_match /test_debug/, str
318
+ assert_no_match /test_info/, str
319
+ assert_no_match /test_warn/, str
320
+ assert_no_match /test_error/, str
321
+ assert_match /test_fatal/, str
322
+ }
323
+ end
324
+
325
+
326
+ def test_log_level_debug_by_conf
327
+ confstr = "log_level: debug"
328
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
329
+
330
+ RBatch::Log.new({ :name => "test_level" }) do | log |
331
+ log.debug("test_debug")
332
+ log.info("test_info")
333
+ log.warn("test_warn")
334
+ log.error("test_error")
335
+ log.fatal("test_fatal")
336
+ end
337
+ File::open(File.join(@dir , "test_level")) {|f|
338
+ str = f.read
339
+ assert_match /test_debug/, str
340
+ assert_match /test_info/, str
341
+ assert_match /test_warn/, str
342
+ assert_match /test_error/, str
343
+ assert_match /test_fatal/, str
344
+ }
345
+ end
346
+
347
+ def test_log_level_info_by_conf
348
+ confstr = "log_level: info"
349
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
350
+ RBatch::Log.new({ :name => "test_level" }) do | log |
351
+ log.debug("test_debug")
352
+ log.info("test_info")
353
+ log.warn("test_warn")
354
+ log.error("test_error")
355
+ log.fatal("test_fatal")
356
+ end
357
+ File::open(File.join(@dir , "test_level")) {|f|
358
+ str = f.read
359
+ assert_no_match /test_debug/, str
360
+ assert_match /test_info/, str
361
+ assert_match /test_warn/, str
362
+ assert_match /test_error/, str
363
+ assert_match /test_fatal/, str
364
+ }
365
+ end
366
+
367
+ def test_log_level_warn_by_conf
368
+ confstr = "log_level: warn"
369
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
370
+ RBatch::Log.new({ :name => "test_level" }) do | log |
371
+ log.debug("test_debug")
372
+ log.info("test_info")
373
+ log.warn("test_warn")
374
+ log.error("test_error")
375
+ log.fatal("test_fatal")
376
+ end
377
+ File::open(File.join(@dir , "test_level")) {|f|
378
+ str = f.read
379
+ assert_no_match /test_debug/, str
380
+ assert_no_match /test_info/, str
381
+ assert_match /test_warn/, str
382
+ assert_match /test_error/, str
383
+ assert_match /test_fatal/, str
384
+ }
385
+ end
386
+
387
+ def test_log_level_error_by_conf
388
+ confstr = "log_level: error"
389
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
390
+ RBatch::Log.new({ :name => "test_level" }) do | log |
391
+ log.debug("test_debug")
392
+ log.info("test_info")
393
+ log.warn("test_warn")
394
+ log.error("test_error")
395
+ log.fatal("test_fatal")
396
+ end
397
+ File::open(File.join(@dir , "test_level")) {|f|
398
+ str = f.read
399
+ assert_no_match /test_debug/, str
400
+ assert_no_match /test_info/, str
401
+ assert_no_match /test_warn/, str
402
+ assert_match /test_error/, str
403
+ assert_match /test_fatal/, str
404
+ }
405
+ end
406
+
407
+ def test_log_level_fatal_by_conf
408
+ confstr = "log_level: fatal"
409
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
410
+ RBatch::Log.new({ :name => "test_level" }) do | log |
411
+ log.debug("test_debug")
412
+ log.info("test_info")
413
+ log.warn("test_warn")
414
+ log.error("test_error")
415
+ log.fatal("test_fatal")
416
+ end
417
+ File::open(File.join(@dir , "test_level")) {|f|
418
+ str = f.read
419
+ assert_no_match /test_debug/, str
420
+ assert_no_match /test_info/, str
421
+ assert_no_match /test_warn/, str
422
+ assert_no_match /test_error/, str
423
+ assert_match /test_fatal/, str
424
+ }
425
+ end
426
+
427
+
428
+ def test_i_log
429
+ log = RBatch::Log.new
430
+ assert_not_nil log
431
+ log.info("test_log")
432
+ log.close
433
+ Dir::foreach(@dir) do |f|
434
+ if ! (/\.+$/ =~ f)
435
+ File::open(File.join(@dir , f)) {|f|
436
+ assert_match /test_log/, f.read
437
+ }
438
+ end
439
+ end
440
+ end
441
+
442
+ def test_i_log_dir_doesnot_exist
443
+ Dir::rmdir(@dir)
444
+ assert_raise(Errno::ENOENT){
445
+ log = RBatch::Log.new
446
+ log.close
447
+ }
448
+ Dir::mkdir(@dir)
449
+ end
450
+
451
+ def test_i_change_name_by_opt
452
+ log = RBatch::Log.new({:name => "name1.log" })
453
+ log.info("test_change_name_by_opt")
454
+ log.close
455
+ File::open(File.join(@dir , "name1.log")) {|f|
456
+ assert_match /test_change_name_by_opt/, f.read
457
+ }
458
+ end
459
+
460
+ def test_i_change_name_by_opt2
461
+ log = RBatch::Log.new({:name => "<prog><date>name.log" })
462
+ log.info("test_change_name_by_opt2")
463
+ log.close
464
+ File::open(File.join(@dir , "test_log" + Time.now.strftime("%Y%m%d") + "name.log")) {|f|
465
+ assert_match /test_change_name_by_opt2/, f.read
466
+ }
467
+ end
468
+
469
+
470
+ def test_i_log_level_default
471
+ log = RBatch::Log.new({ :name => "test_level" })
472
+ log.debug("test_debug")
473
+ log.info("test_info")
474
+ log.warn("test_warn")
475
+ log.error("test_error")
476
+ log.fatal("test_fatal")
477
+ log.close
478
+
479
+ File::open(File.join(@dir , "test_level")) {|f|
480
+ str = f.read
481
+ assert_no_match /test_debug/, str
482
+ assert_match /test_info/, str
483
+ assert_match /test_warn/, str
484
+ assert_match /test_error/, str
485
+ assert_match /test_fatal/, str
486
+ }
487
+ end
488
+
489
+ def test_i_log_level_debug_by_opt
490
+ log = RBatch::Log.new({ :level => "debug",:name => "test_level" })
491
+ log.debug("test_debug")
492
+ log.info("test_info")
493
+ log.warn("test_warn")
494
+ log.error("test_error")
495
+ log.fatal("test_fatal")
496
+ log.close
497
+ File::open(File.join(@dir , "test_level")) {|f|
498
+ str = f.read
499
+ assert_match /test_debug/, str
500
+ assert_match /test_info/, str
501
+ assert_match /test_warn/, str
502
+ assert_match /test_error/, str
503
+ assert_match /test_fatal/, str
504
+ }
505
+ end
506
+
507
+
508
+ def test_i_log_level_debug_by_conf
509
+ confstr = "log_level: debug"
510
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
511
+
512
+ log = RBatch::Log.new({ :name => "test_level" })
513
+ log.debug("test_debug")
514
+ log.info("test_info")
515
+ log.warn("test_warn")
516
+ log.error("test_error")
517
+ log.fatal("test_fatal")
518
+ log.close
519
+ File::open(File.join(@dir , "test_level")) {|f|
520
+ str = f.read
521
+ assert_match /test_debug/, str
522
+ assert_match /test_info/, str
523
+ assert_match /test_warn/, str
524
+ assert_match /test_error/, str
525
+ assert_match /test_fatal/, str
526
+ }
527
+ end
528
+
529
+ def test_delete_old_log_by_opt
530
+ loglist = [*0..20].map do |day|
531
+ File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
532
+ end
533
+ FileUtils.touch(loglist)
534
+ log = RBatch::Log.new({ :name => "<date>_test_delete.log",:delete_old_log => true})
535
+ loglist[1..6].each do |filename|
536
+ assert File.exists?(filename), "log file \"#{filename}\" should be exist"
537
+ end
538
+ loglist[7..20].each do |filename|
539
+ assert ! File.exists?(filename), "log file \"#{filename}\" should NOT be exist"
540
+ end
541
+ end
542
+ def test_delete_old_log_by_config
543
+ confstr = "log_delete_old_log: true"
544
+ open( RBatch.common_config_path , "a" ){|f| f.write(confstr)}
545
+
546
+ loglist = [*0..20].map do |day|
547
+ File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
548
+ end
549
+ FileUtils.touch(loglist)
550
+ log = RBatch::Log.new({ :name => "<date>_test_delete.log"})
551
+ loglist[1..6].each do |filename|
552
+ assert File.exists?(filename), "log file \"#{filename}\" should be exist"
553
+ end
554
+ loglist[7..20].each do |filename|
555
+ assert ! File.exists?(filename), "log file \"#{filename}\" should NOT be exist"
556
+ end
557
+ end
558
+
559
+ def test_delete_old_log_file_format_change_with_time
560
+ loglist = [*0..20].map do |day|
561
+ File.join(@dir , "235959-" + (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
562
+ end
563
+ FileUtils.touch(loglist)
564
+ log = RBatch::Log.new({ :name => "<time>-<date>_test_delete.log",:delete_old_log => true})
565
+ loglist[1..6].each do |filename|
566
+ assert File.exists?(filename), "log file \"#{filename}\" should be exist"
567
+ end
568
+ loglist[7..20].each do |filename|
569
+ assert ! File.exists?(filename), "log file \"#{filename}\" should NOT be exist"
570
+ end
571
+ end
572
+
573
+ def test_delete_old_log_file_format_change_no_date
574
+ log = RBatch::Log.new({ :name => "test_delete.log",:delete_old_log => true})
575
+ assert File.exists?(File.join(@dir,"test_delete.log")), "log file \"test_delete.log\" should be exist"
576
+ end
577
+
578
+ def test_delete_old_log_date
579
+ loglist = [*0..20].map do |day|
580
+ File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
581
+ end
582
+ FileUtils.touch(loglist)
583
+ log = RBatch::Log.new({ :name => "<date>_test_delete.log",:delete_old_log => true,:delete_old_log_date => 5})
584
+ loglist[1..4].each do |filename|
585
+ assert File.exists?(filename), "log file \"#{filename}\" should be exist"
586
+ end
587
+ loglist[5..20].each do |filename|
588
+ assert ! File.exists?(filename), "log file \"#{filename}\" should NOT be exist"
589
+ end
590
+ end
591
+
592
+
593
+ end
594
+