rbatch 1.13.0 → 1.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1,5 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
- require 'rake/rdoctask'
3
2
 
4
3
  desc "Test lib/ by using test/cases/test_*.rb"
5
4
  task :test do
@@ -28,11 +27,6 @@ task :test do
28
27
  end
29
28
  end
30
29
 
31
- Rake::RDocTask.new do |rd|
32
- rd.rdoc_dir = 'rdocs'
33
- rd.rdoc_files = FileList["lib/**/*.rb"]
34
- rd.options << '-charset=UTF-8 '
35
- end
36
30
 
37
31
  desc "Test, Make RDoc, Release"
38
32
  task :all => [:test, :rdoc, :release]
data/lib/rbatch/cmd.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'tempfile'
3
+ require 'tmpdir'
3
4
 
4
5
  module RBatch
5
6
 
@@ -8,26 +9,25 @@ module RBatch
8
9
  #This module is a wrapper of Kernel#spawn.
9
10
  #
10
11
  # * Arguments(cmd_params) are inputed to Kernel#spawn directly and run command.
11
- # * Command's stdout and stderr is written to tmp file.
12
- # * If Platform is "mswin" or "mingw" , then temp directory is ENV["TEMP"]
13
- # * If Platform is "linux" or "cygwin" , then temp directory is "/tmp/"
14
12
  # * Return an object of RBatch::CmdResult which includes stdout, stderr, and exit status.
15
13
  #
16
14
  # ==== Sample 1
17
15
  # require 'rbatch'
18
- # cmd = RBatch::Cmd("ls")
19
- # r = cmd.run
20
- # p r.stdout
16
+ # result = RBatch::cmd("ls")
17
+ # p result.stdout
21
18
  # => "fileA\nfileB\n"
22
19
  #
23
- # ==== Sample 2 ( Use option)
24
- # cmd = RBatch::Cmd("ls", {:verbose => true})
25
- # r = cmd.run
20
+ # ==== Sample 2 (use option)
21
+ # require 'rbatch'
22
+ # result = RBatch::cmd("ls",{:timeout => 1})
23
+ # p result.stdout
24
+ # => "fileA\nfileB\n"
26
25
  #
27
- # ==== Sample 3 ( Use alias)
26
+ # ==== Sample 3 (use instance)
28
27
  # require 'rbatch'
29
- # r = RBatch::cmd("ls")
30
- # p r.stdout
28
+ # cmd = RBatch::Cmd.new("ls")
29
+ # result = cmd.run
30
+ # p result.stdout
31
31
  # => "fileA\nfileB\n"
32
32
  #
33
33
  class Cmd
@@ -69,16 +69,16 @@ module RBatch
69
69
  # ==== Return
70
70
  # instance of RBatch::CmdResult
71
71
  def run()
72
- stdout_file = Tempfile::new("rbatch_tmpout",RBatch::tmp_dir)
73
- stderr_file = Tempfile::new("rbatch_tmperr",RBatch::tmp_dir)
72
+ stdout_file = Tempfile::new("rbatch_tmpout",Dir.tmpdir)
73
+ stderr_file = Tempfile::new("rbatch_tmperr",Dir.tmpdir)
74
74
  pid = spawn(@cmd_str,:out => [stdout_file,"w"],:err => [stderr_file,"w"])
75
75
  if @opt[:timeout] != 0
76
- timeout(@opt[:timeout]) do
77
- begin
76
+ begin
77
+ timeout(@opt[:timeout]) do
78
78
  status = Process.waitpid2(pid)[1] >> 8
79
- rescue Timeout::Error => e
80
- raise(CmdException,"Command timeout (over " + @opt[:timeout] + " sec)" )
81
79
  end
80
+ rescue Timeout::Error => e
81
+ raise(CmdException,"Command timeout. Runtime is over " + @opt[:timeout].to_s + " sec. Command is " + @cmd_str )
82
82
  end
83
83
  else
84
84
  status = Process.waitpid2(pid)[1] >> 8
@@ -1,3 +1,3 @@
1
1
  module RBatch
2
- VERSION = "1.13.0"
2
+ VERSION = "1.13.1"
3
3
  end
data/lib/rbatch.rb CHANGED
@@ -1,88 +1,70 @@
1
1
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
2
2
  require 'digest'
3
+ require 'yaml'
3
4
  module RBatch
4
- @@program_name = $PROGRAM_NAME
5
- @@home_dir = ""
5
+ @@opt = {}
6
+ @@rbatch_config = nil
6
7
  module_function
7
- def program_name=(f) ; @@program_name = f ; end
8
- def program_name ; @@program_name ; end
9
- def home_dir=(d) ; @@home_dir = d ; end
10
- def home_dir ; @@home_dir ; end
11
-
12
- # Hostname
13
- def hostname
8
+ def program_name ; @@opt[:program_name] ; end
9
+ def home_dir ; @@opt[:home_dir] ; end
10
+ def home_dir=(d) ; @@opt[:home_dir]=d ; end
11
+ def hostname ; @@opt[:hostname] ; end
12
+ def rbatch_config ; @@rbatch_config ; end
13
+ def opt ; @@opt ; end
14
+ def init
15
+ @@opt[:program_name] = $PROGRAM_NAME
16
+ @@opt[:home_dir] = ENV["RB_HOME"] ? ENV["RB_HOME"] : File.join(File.dirname(@@opt[:program_name]) , "..")
14
17
  case RUBY_PLATFORM
15
18
  when /mswin|mingw/
16
- return ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
19
+ @@opt[:hostname] = ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
17
20
  when /cygwin|linux/
18
- return ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
21
+ @@opt[:hostname] = ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
19
22
  else
20
- return "unknownhost"
21
- end
22
- end
23
- # tmp dir
24
- def tmp_dir
25
- case RUBY_PLATFORM
26
- when /mswin|mingw/
27
- if ENV["TEMP"].nil?
28
- raise "Cannot use temporary directory, because ENV[\"TEMP\"] is not defined"
29
- else
30
- return ENV["TEMP"]
31
- end
32
- when /cygwin|linux/
33
- return ENV["TMPDIR"] ? ENV["TMPDIR"] : "/tmp"
34
- else
35
- if ENV["TMPDIR"].nil? && ENV["TEMP"].nil?
36
- raise "Unknown RUBY_PRATFORM : " + RUBY_PLATFORM
37
- else
38
- return ENV["TMPDIR"] || ENV["TEMP"]
39
- end
23
+ @@opt[:hostname] = "unknownhost"
40
24
  end
25
+ load_rbatch_config
41
26
  end
42
27
  def rbatch_config_path
43
- File.join(@@home_dir,"conf","rbatch.yaml")
28
+ File.join(@@opt[:home_dir],"conf","rbatch.yaml")
29
+ end
30
+ def config_dir
31
+ File.join(RBatch.home_dir,"conf")
44
32
  end
45
- def rbatch_config
46
- if File.exist?(RBatch.rbatch_config_path)
47
- yaml = YAML::load_file(RBatch.rbatch_config_path)
48
- if yaml
49
- return yaml
50
- else
51
- # If file is emply , YAML::load_file is false
52
- return nil
33
+ def log_dir
34
+ File.join(RBatch.home_dir,"log")
35
+ end
36
+ def load_rbatch_config
37
+ if File.exist?(rbatch_config_path)
38
+ @@rbatch_config = YAML::load_file(rbatch_config_path)
39
+ if @@rbatch_config == false
40
+ @@rbatch_config = nil
53
41
  end
54
42
  else
55
- return nil
43
+ @@rbatch_config = nil
56
44
  end
57
45
  end
58
46
  def double_run_check
59
47
  # double run check
60
- if ( RBatch::rbatch_config != nil && RBatch::rbatch_config["forbid_double_run"] )
61
- lock_file="rbatch_lock_" + Digest::MD5.hexdigest(@@program_name)
62
- if Dir.exists? RBatch::tmp_dir
63
- Dir::foreach(RBatch::tmp_dir) do |f|
48
+ if ( @@rbatch_config != nil && @@rbatch_config["forbid_double_run"] )
49
+ lock_file="rbatch_lock_" + Digest::MD5.hexdigest(@@opt[:program_name])
50
+ if Dir.exists? @@opt[:tmp_dir]
51
+ Dir::foreach(@@opt[:tmp_dir]) do |f|
64
52
  if (Regexp.new(lock_file) =~ f)
65
- raise RBatchException, "Script double run is forbid about \"#{RBatch::program_name}\""
53
+ raise RBatchException, "Script double run is forbid about \"#{@@opt[:program_name]}\""
66
54
  end
67
55
  end
68
56
  end
69
57
  # make lockfile
70
- Tempfile::new(lock_file,RBatch::tmp_dir)
58
+ Tempfile::new(lock_file,@@opt[:tmp_dir])
71
59
  end
72
60
  end
73
-
74
- def double_run_lock_file ; ; end
75
61
  end
76
62
 
77
63
  # RBatch Exception
78
64
  class RBatchException < Exception ; end
79
65
 
80
66
  # main
81
- if ENV["RB_HOME"]
82
- RBatch::home_dir = ENV["RB_HOME"]
83
- else
84
- RBatch::home_dir = File.join(File.dirname(RBatch.program_name) , "..")
85
- end
67
+ RBatch::init
86
68
 
87
69
  require 'rbatch/log'
88
70
  require 'rbatch/config'
@@ -0,0 +1,127 @@
1
+ require 'tmpdir'
2
+ ENV["RB_HOME"]=Dir.tmpdir
3
+
4
+ require 'rbatch'
5
+
6
+ describe RBatch::Cmd do
7
+
8
+ it "run command which status is 0" do
9
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'"
10
+ expect(result.stdout.chomp).to eq "1"
11
+ expect(result.stderr.chomp).to eq "2"
12
+ expect(result.status).to eq 0
13
+ end
14
+ it "run command which status is 1" do
15
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
16
+ expect(result.stdout.chomp).to eq "1"
17
+ expect(result.stderr.chomp).to eq "2"
18
+ expect(result.status).to eq 1
19
+ end
20
+ it "raise error when command does not exist" do
21
+ expect {
22
+ RBatch::cmd "not_exist_command"
23
+ }.to raise_error(Errno::ENOENT)
24
+ end
25
+ it "run command which stdout size is greater than 65534byte" do
26
+ result = RBatch::cmd "ruby -e '100000.times{print 0}'"
27
+ expect(result.stdout.chomp.size).to eq 100000
28
+ expect(result.stderr.chomp).to eq ""
29
+ expect(result.status).to eq 0
30
+ end
31
+ it "run command which stdout size is greater than 65534bytes with status 1" do
32
+ result = RBatch::cmd "ruby -e '100000.times{print 0}; exit 1'"
33
+ expect(result.stdout.chomp.size).to eq 100000
34
+ expect(result.stderr.chomp).to eq ""
35
+ expect(result.status).to eq 1
36
+ end
37
+ it "run command which status is grater than 256" do
38
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 300;'"
39
+ expect(result.stdout.chomp).to eq "1"
40
+ expect(result.stderr.chomp).to eq "2"
41
+ case RUBY_PLATFORM
42
+ when /mswin|mingw/
43
+ expect(result.status).to eq 300
44
+ when /cygwin|linux/
45
+ # windos platform can not handle result code as modular 256
46
+ expect(result.status).to eq 44
47
+ end
48
+ end
49
+ it "run to_h method" do
50
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
51
+ expect(result.to_h[:stdout]).to eq "1"
52
+ expect(result.to_h[:stderr]).to eq "2"
53
+ expect(result.to_h[:status]).to eq 1
54
+ end
55
+ it "run to_s method" do
56
+ result = RBatch::cmd "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
57
+ expect(result.to_s).to eq "{:cmd_str=>\"ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'\", :stdout=>\"1\", :stderr=>\"2\", :status=>1}"
58
+ end
59
+ it "raise error when command is nil" do
60
+ expect {
61
+ RBatch::Cmd.new(nil)
62
+ }.to raise_error(RBatch::CmdException)
63
+ end
64
+ it "run RBatch::Cmd.new method" do
65
+ result = RBatch::Cmd.new("ruby -e 'STDOUT.print 1; STDERR.print 2; exit 0;'").run
66
+ expect(result.stdout.chomp).to eq "1"
67
+ expect(result.stderr.chomp).to eq "2"
68
+ expect(result.status).to eq 0
69
+ end
70
+
71
+
72
+ describe "option by argument" do
73
+ describe "timeout" do
74
+ it "run successfuly when command is short time" do
75
+ opt = {:timeout => 2}
76
+ expect {
77
+ RBatch::cmd("ruby -e 'sleep 1'",opt)
78
+ }.to_not raise_error
79
+ end
80
+ it "raise timeout error when command is long time" do
81
+ opt = {:timeout => 1}
82
+ expect {
83
+ RBatch::cmd("ruby -e 'sleep 2'",opt)
84
+ }.to raise_error(RBatch::CmdException)
85
+ end
86
+ end
87
+ end
88
+
89
+ describe "option by argument" do
90
+ describe "timeout" do
91
+ it "run successfuly when command is short time" do
92
+ opt = {:timeout => 2}
93
+ expect {
94
+ RBatch::cmd("ruby -e 'sleep 1'",opt)
95
+ }.to_not raise_error
96
+ end
97
+ it "raise timeout error when command is long time" do
98
+ opt = {:timeout => 1}
99
+ expect {
100
+ RBatch::cmd("ruby -e 'sleep 2'",opt)
101
+ }.to raise_error(RBatch::CmdException)
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "option by config" do
107
+ before :all do
108
+ @config_dir=File.join(RBatch.home_dir,"conf")
109
+ @config_file = File.join(@config_dir , "rbatch.yaml")
110
+ Dir::mkdir @config_dir if ! Dir.exists? @config_dir
111
+ end
112
+ describe "raise" do
113
+ before :all do
114
+ open( @config_file , "w" ){|f| f.write("cmd_raise: true")}
115
+ RBatch.load_rbatch_config
116
+ end
117
+ it "raise error when command status is not 0" do
118
+ expect {
119
+ RBatch::cmd "ruby -e 'exit 1;'"
120
+ }.to raise_error(RBatch::CmdException)
121
+ end
122
+ after :each do
123
+ FileUtils.rm @config_file
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,48 @@
1
+ require 'tmpdir'
2
+ ENV["RB_HOME"]=Dir.tmpdir
3
+
4
+ require 'rbatch'
5
+
6
+ describe RBatch::Config do
7
+ before :all do
8
+ @config_dir=RBatch.config_dir
9
+ @config_file = File.join(@config_dir , "rspec.yaml")
10
+ Dir::mkdir @config_dir if ! Dir.exists? @config_dir
11
+ end
12
+
13
+ before :each do
14
+ end
15
+
16
+ after :each do
17
+ FileUtils.rm @config_file if File.exists? @config_file
18
+ end
19
+
20
+ after :all do
21
+ end
22
+
23
+ it "read config" do
24
+ open( @config_file , "w" ){|f| f.write("key: value")}
25
+ expect(RBatch.config["key"]).to eq "value"
26
+ end
27
+
28
+ it "raise error when config does not exist" do
29
+ expect {
30
+ RBatch.config
31
+ }.to raise_error(Errno::ENOENT)
32
+ end
33
+
34
+ it "read config twice" do
35
+ open( @config_file , "w" ){|f| f.write("key: value")}
36
+ expect(RBatch.config["key"]).to eq "value"
37
+ expect(RBatch.config["key"]).to eq "value"
38
+ end
39
+
40
+ it "raise error when read value which key does not exist" do
41
+ open( @config_file , "w" ){|f| f.write("key: value")}
42
+ expect {
43
+ RBatch.config["not_exist"]
44
+ }.to raise_error(RBatch::Config::Exception)
45
+ end
46
+
47
+
48
+ end
@@ -0,0 +1,430 @@
1
+ require 'tmpdir'
2
+ ENV["RB_HOME"]=Dir.tmpdir
3
+
4
+ require 'rbatch'
5
+
6
+ describe RBatch::Log do
7
+
8
+ before :all do
9
+ @dir = RBatch.log_dir
10
+ Dir::mkdir(@dir)if ! Dir.exists? @dir
11
+ end
12
+
13
+ before :each do
14
+ # set quiet option
15
+ confstr = "log_quiet: true\n"
16
+ open( RBatch.rbatch_config_path , "w" ){|f| f.write(confstr)}
17
+ RBatch.load_rbatch_config
18
+ end
19
+
20
+ after :each do
21
+ Dir::foreach(@dir) do |f|
22
+ File::delete(File.join(@dir , f)) if ! (/\.+$/ =~ f)
23
+ end
24
+ end
25
+
26
+ it "is run" do
27
+ RBatch::Log.new do | log |
28
+ log.info("test_log")
29
+ end
30
+ Dir::foreach(@dir) do |f|
31
+ if ! (/\.+$/ =~ f)
32
+ File::open(File.join(@dir , f)) {|f|
33
+ expect(f.read).to match /test_log/
34
+ }
35
+ end
36
+ end
37
+ end
38
+
39
+ it "raise error when log dir does not exist" do
40
+ Dir::rmdir(@dir)
41
+ expect{
42
+ RBatch::Log.new {|log|}
43
+ }.to raise_error(Errno::ENOENT)
44
+ Dir::mkdir(@dir)
45
+ end
46
+
47
+ it "run when log block is nested" do
48
+ RBatch::Log.new({:name => "name1" }) do | log |
49
+ log.info("name1")
50
+ RBatch::Log.new({:name => "name2" }) do | log |
51
+ log.info("name2")
52
+ end
53
+ end
54
+ File::open(File.join(@dir,"name1")) {|f| expect(f.read).to match /name1/ }
55
+ File::open(File.join(@dir,"name2")) {|f| expect(f.read).to match /name2/ }
56
+ end
57
+
58
+ describe "option by argument" do
59
+ it "change log name" do
60
+ RBatch::Log.new({:name => "name1.log" }) do | log |
61
+ log.info("hoge")
62
+ end
63
+ File::open(File.join(@dir , "name1.log")) {|f|
64
+ expect(f.read).to match /hoge/
65
+ }
66
+ end
67
+
68
+ it "change log name 2" do
69
+ RBatch::Log.new({:name => "<prog><date>name.log" }) do | log |
70
+ log.info("hoge")
71
+ end
72
+ File::open(File.join(@dir , "rspec" + Time.now.strftime("%Y%m%d") + "name.log")) {|f|
73
+ expect(f.read).to match /hoge/
74
+ }
75
+ end
76
+
77
+ it "change log name 3" do
78
+ RBatch::Log.new({:name => "<prog>-<date>-name.log" }) do | log |
79
+ log.info("hoge")
80
+ end
81
+ File::open(File.join(@dir , "rspec-" + Time.now.strftime("%Y%m%d") + "-name.log")) {|f|
82
+ expect(f.read).to match /hoge/
83
+ }
84
+ end
85
+
86
+ it "change log dir" do
87
+ @tmp = Dir.tmpdir
88
+ RBatch::Log.new({:name => "c.log", :dir=> @tmp }) do | log |
89
+ log.info("hoge")
90
+ end
91
+ File::open(File.join(@tmp , "c.log")) {|f|
92
+ expect(f.read).to match /hoge/
93
+ }
94
+ end
95
+
96
+ it "is append mode" do
97
+ RBatch::Log.new({:append => true, :name => "a.log" }) do | log |
98
+ log.info("line1")
99
+ end
100
+ RBatch::Log.new({:append => true, :name => "a.log" }) do | log |
101
+ log.info("line2")
102
+ end
103
+ File::open(File.join(@dir , "a.log")) {|f|
104
+ str = f.read
105
+ expect(str).to match /line1/
106
+ expect(str).to match /line2/
107
+ }
108
+ end
109
+
110
+ it "is overwrite mode" do
111
+ RBatch::Log.new({:append => false, :name => "a.log" }) do | log |
112
+ log.info("line1")
113
+ end
114
+ RBatch::Log.new({:append => false, :name => "a.log" }) do | log |
115
+ log.info("line2")
116
+ end
117
+ File::open(File.join(@dir , "a.log")) {|f|
118
+ str = f.read
119
+ expect(str).to_not match /line1/
120
+ expect(str).to match /line2/
121
+ }
122
+ end
123
+
124
+ it "is debug level" do
125
+ RBatch::Log.new({ :level => "debug",:name => "a.log" }) do | log |
126
+ log.debug("test_debug")
127
+ log.info("test_info")
128
+ log.warn("test_warn")
129
+ log.error("test_error")
130
+ log.fatal("test_fatal")
131
+ end
132
+ File::open(File.join(@dir , "a.log")) {|f|
133
+ str = f.read
134
+ expect(str).to match /test_debug/
135
+ expect(str).to match /test_info/
136
+ expect(str).to match /test_warn/
137
+ expect(str).to match /test_error/
138
+ expect(str).to match /test_fatal/
139
+ }
140
+ end
141
+
142
+ it "is info level" do
143
+ RBatch::Log.new({ :level => "info",:name => "a.log" }) do | log |
144
+ log.debug("test_debug")
145
+ log.info("test_info")
146
+ log.warn("test_warn")
147
+ log.error("test_error")
148
+ log.fatal("test_fatal")
149
+ end
150
+ File::open(File.join(@dir , "a.log")) {|f|
151
+ str = f.read
152
+ expect(str).to_not match /test_debug/
153
+ expect(str).to match /test_info/
154
+ expect(str).to match /test_warn/
155
+ expect(str).to match /test_error/
156
+ expect(str).to match /test_fatal/
157
+ }
158
+ end
159
+
160
+ it "is warn level" do
161
+ RBatch::Log.new({ :level => "warn",:name => "a.log" }) do | log |
162
+ log.debug("test_debug")
163
+ log.info("test_info")
164
+ log.warn("test_warn")
165
+ log.error("test_error")
166
+ log.fatal("test_fatal")
167
+ end
168
+ File::open(File.join(@dir , "a.log")) {|f|
169
+ str = f.read
170
+ expect(str).to_not match /test_debug/
171
+ expect(str).to_not match /test_info/
172
+ expect(str).to match /test_warn/
173
+ expect(str).to match /test_error/
174
+ expect(str).to match /test_fatal/
175
+ }
176
+ end
177
+
178
+ it "is error level" do
179
+ RBatch::Log.new({ :level => "error",:name => "a.log" }) do | log |
180
+ log.debug("test_debug")
181
+ log.info("test_info")
182
+ log.warn("test_warn")
183
+ log.error("test_error")
184
+ log.fatal("test_fatal")
185
+ end
186
+ File::open(File.join(@dir , "a.log")) {|f|
187
+ str = f.read
188
+ expect(str).to_not match /test_debug/
189
+ expect(str).to_not match /test_info/
190
+ expect(str).to_not match /test_warn/
191
+ expect(str).to match /test_error/
192
+ expect(str).to match /test_fatal/
193
+ }
194
+ end
195
+
196
+ it "is fatal level" do
197
+ RBatch::Log.new({ :level => "fatal",:name => "a.log" }) do | log |
198
+ log.debug("test_debug")
199
+ log.info("test_info")
200
+ log.warn("test_warn")
201
+ log.error("test_error")
202
+ log.fatal("test_fatal")
203
+ end
204
+ File::open(File.join(@dir , "a.log")) {|f|
205
+ str = f.read
206
+ expect(str).to_not match /test_debug/
207
+ expect(str).to_not match /test_info/
208
+ expect(str).to_not match /test_warn/
209
+ expect(str).to_not match /test_error/
210
+ expect(str).to match /test_fatal/
211
+ }
212
+ end
213
+
214
+ it "is default level" do
215
+ RBatch::Log.new({ :name => "a.log" }) do | log |
216
+ log.debug("test_debug")
217
+ log.info("test_info")
218
+ log.warn("test_warn")
219
+ log.error("test_error")
220
+ log.fatal("test_fatal")
221
+ end
222
+ File::open(File.join(@dir , "a.log")) {|f|
223
+ str = f.read
224
+ expect(str).to_not match /test_debug/
225
+ expect(str).to match /test_info/
226
+ expect(str).to match /test_warn/
227
+ expect(str).to match /test_error/
228
+ expect(str).to match /test_fatal/
229
+ }
230
+ end
231
+
232
+ it "delete old log which name include <date>" do
233
+ loglist = [*0..20].map do |day|
234
+ File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
235
+ end
236
+ FileUtils.touch(loglist)
237
+ log = RBatch::Log.new({ :name => "<date>_test_delete.log",:delete_old_log => true})
238
+ log.close
239
+ loglist[1..6].each do |filename|
240
+ expect(File.exists?(filename)).to be true
241
+ end
242
+ loglist[7..20].each do |filename|
243
+ expect(File.exists?(filename)).to be false
244
+ end
245
+ end
246
+
247
+ it "delete old log which name include <date> even if <date> position is changed" do
248
+ loglist = [*0..20].map do |day|
249
+ File.join(@dir , "235959-" + (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
250
+ end
251
+ FileUtils.touch(loglist)
252
+ log = RBatch::Log.new({ :name => "<time>-<date>_test_delete.log",:delete_old_log => true})
253
+ log.close
254
+ loglist[1..6].each do |filename|
255
+ expect(File.exists?(filename)).to be true
256
+ end
257
+ loglist[7..20].each do |filename|
258
+ expect(File.exists?(filename)).to be false
259
+ end
260
+ end
261
+
262
+ it "does not delete old log which name does not include <date>" do
263
+ log = RBatch::Log.new({ :name => "test_delete.log",:delete_old_log => true})
264
+ log.close
265
+ expect(File.exists?(File.join(@dir,"test_delete.log"))).to be true
266
+ end
267
+
268
+
269
+ end
270
+
271
+ describe "option by config" do
272
+ it "change log name" do
273
+ confstr = "log_name: name1.log"
274
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
275
+ RBatch.load_rbatch_config
276
+ RBatch::Log.new() do | log |
277
+ log.info("hoge")
278
+ end
279
+ File::open(File.join(@dir , "name1.log")) {|f|
280
+ expect(f.read).to match /hoge/
281
+ }
282
+ end
283
+
284
+ it "change log dir" do
285
+ @tmp = Dir.tmpdir
286
+ confstr = "log_name: c.log\nlog_dir: " + @tmp
287
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
288
+ RBatch.load_rbatch_config
289
+ RBatch::Log.new({:name => "c.log", :dir=> @tmp }) do | log |
290
+ log.info("hoge")
291
+ end
292
+ File::open(File.join(@tmp , "c.log")) {|f|
293
+ expect(f.read).to match /hoge/
294
+ }
295
+ end
296
+
297
+ it "is append mode" do
298
+ confstr = "log_name: a.log\nappend: true"
299
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
300
+ RBatch.load_rbatch_config
301
+ RBatch::Log.new() do | log |
302
+ log.info("line1")
303
+ end
304
+ RBatch::Log.new() do | log |
305
+ log.info("line2")
306
+ end
307
+ File::open(File.join(@dir , "a.log")) {|f|
308
+ str = f.read
309
+ expect(str).to match /line1/
310
+ expect(str).to match /line2/
311
+ }
312
+ end
313
+
314
+ it "is overwrite mode" do
315
+ confstr = "log_name: a.log\nlog_append: false"
316
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
317
+ RBatch.load_rbatch_config
318
+ RBatch::Log.new() do | log |
319
+ log.info("line1")
320
+ end
321
+ RBatch::Log.new() do | log |
322
+ log.info("line2")
323
+ end
324
+ File::open(File.join(@dir , "a.log")) {|f|
325
+ str = f.read
326
+ expect(str).to_not match /line1/
327
+ expect(str).to match /line2/
328
+ }
329
+ end
330
+
331
+ it "is warn level" do
332
+ confstr = "log_name: a.log\nlog_level: warn"
333
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
334
+ RBatch.load_rbatch_config
335
+ RBatch::Log.new() do | log |
336
+ log.debug("test_debug")
337
+ log.info("test_info")
338
+ log.warn("test_warn")
339
+ log.error("test_error")
340
+ log.fatal("test_fatal")
341
+ end
342
+ File::open(File.join(@dir , "a.log")) {|f|
343
+ str = f.read
344
+ expect(str).to_not match /test_debug/
345
+ expect(str).to_not match /test_info/
346
+ expect(str).to match /test_warn/
347
+ expect(str).to match /test_error/
348
+ expect(str).to match /test_fatal/
349
+ }
350
+ end
351
+
352
+ it "delete old log file which name include <date>" do
353
+ confstr = "log_delete_old_log: true"
354
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
355
+ RBatch.load_rbatch_config
356
+ loglist = [*0..20].map do |day|
357
+ File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
358
+ end
359
+ FileUtils.touch(loglist)
360
+ log = RBatch::Log.new({ :name => "<date>_test_delete.log"})
361
+ log.close
362
+ loglist[1..6].each do |filename|
363
+ expect( File.exists?(filename)).to be true
364
+ end
365
+ loglist[7..20].each do |filename|
366
+ expect( File.exists?(filename)).to be false
367
+ end
368
+ end
369
+ end
370
+
371
+ describe "option by both argument and config" do
372
+ it "is prior to argument than config" do
373
+ confstr = "log_name: a.log"
374
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
375
+ RBatch.load_rbatch_config
376
+ RBatch::Log.new({:name => "b.log"}) do | log |
377
+ log.info("hoge")
378
+ end
379
+ File::open(File.join(@dir , "b.log")) {|f|
380
+ expect(f.read).to match /hoge/
381
+ }
382
+ end
383
+ end
384
+
385
+ describe "instance" do
386
+ it "run" do
387
+ log = RBatch::Log.new
388
+ expect(log).to_not be_nil
389
+ log.info("test_log")
390
+ log.close
391
+ Dir::foreach(@dir) do |f|
392
+ if ! (/\.+$/ =~ f)
393
+ File::open(File.join(@dir , f)) {|f|
394
+ expect(f.read).to match /test_log/
395
+ }
396
+ end
397
+ end
398
+ end
399
+
400
+ it "raise error when log dir does not exist" do
401
+ Dir::rmdir(@dir)
402
+ expect{
403
+ RBatch::Log.new
404
+ }.to raise_error(Errno::ENOENT)
405
+ Dir::mkdir(@dir)
406
+ end
407
+
408
+ it "option by argument" do
409
+ log = RBatch::Log.new({:name => "d.log" })
410
+ log.info("hoge")
411
+ log.close
412
+ File::open(File.join(@dir , "d.log")) {|f|
413
+ expect(f.read).to match /hoge/
414
+ }
415
+ end
416
+
417
+
418
+ it "option by config" do
419
+ confstr = "log_name: e.log"
420
+ open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
421
+ RBatch.load_rbatch_config
422
+ log = RBatch::Log.new()
423
+ log.info("hoge")
424
+ log.close
425
+ File::open(File.join(@dir , "e.log")) {|f|
426
+ expect(f.read).to match /hoge/
427
+ }
428
+ end
429
+ end
430
+ end
@@ -0,0 +1,25 @@
1
+ require 'rbatch'
2
+
3
+ describe RBatch do
4
+ before :all do
5
+ RBatch.home_dir=Dir.tmpdir
6
+ @config_dir=File.join(RBatch.home_dir,"conf")
7
+ @config_file = File.join(@config_dir , "rbatch.yaml")
8
+ Dir::mkdir @config_dir if ! Dir.exists? @config_dir
9
+ end
10
+
11
+ before :each do
12
+ end
13
+
14
+ it "makes lock file when forbid_double_run is enable" do
15
+ open( @config_file , "w" ){|f| f.write("forbid_double_run: true")}
16
+ end
17
+
18
+ after :each do
19
+ FileUtils.rm @config_file if File.exists? @config_file
20
+ end
21
+
22
+ after :all do
23
+ end
24
+
25
+ end
@@ -7,6 +7,7 @@ class RuncherTest < Test::Unit::TestCase
7
7
  Dir::mkdir(@config_dir) if ! Dir.exists? @config_dir
8
8
  confstr = ""
9
9
  open( RBatch.rbatch_config_path , "w" ){|f| f.write(confstr)}
10
+ RBatch.load_rbatch_config
10
11
  end
11
12
 
12
13
  def test_cmd_exists
@@ -105,6 +106,7 @@ class RuncherTest < Test::Unit::TestCase
105
106
  def test_opt_raise_true_status_1_by_conf
106
107
  confstr = "cmd_raise: true "
107
108
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
109
+ RBatch.load_rbatch_config
108
110
  cmd_str = "ruby -e 'STDOUT.print 1; STDERR.print 2; exit 1;'"
109
111
  assert_raise(RBatch::CmdException){
110
112
  RBatch::Cmd.new(cmd_str).run
@@ -14,7 +14,7 @@ class LoggerTest < Test::Unit::TestCase
14
14
  # set STDOUT Logger stop
15
15
  confstr = "log_quiet: true\n"
16
16
  open( RBatch.rbatch_config_path , "w" ){|f| f.write(confstr)}
17
-
17
+ RBatch.load_rbatch_config
18
18
  end
19
19
 
20
20
  def teardown
@@ -85,6 +85,7 @@ class LoggerTest < Test::Unit::TestCase
85
85
  def test_change_name_by_config
86
86
  confstr = "log_name: name1"
87
87
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
88
+ RBatch.load_rbatch_config
88
89
  RBatch::Log.new({:name => "name1.log" }) do | log |
89
90
  log.info("test_change_name_by_config")
90
91
  end
@@ -110,6 +111,7 @@ class LoggerTest < Test::Unit::TestCase
110
111
  def test_change_log_dir_by_config
111
112
  confstr = "log_dir: " + @dir2
112
113
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
114
+ RBatch.load_rbatch_config
113
115
  RBatch::Log.new({:output_dir=> @dir2 }) do | log |
114
116
  log.info("test_change_log_dir_by_config")
115
117
  end
@@ -136,6 +138,7 @@ class LoggerTest < Test::Unit::TestCase
136
138
  def test_opt_overwite_config
137
139
  confstr = "log_name: " + "name1"
138
140
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
141
+ RBatch.load_rbatch_config
139
142
  RBatch::Log.new({:name => "name2" }) do | log |
140
143
  log.info("test_opt_overwite_config")
141
144
  end
@@ -176,6 +179,7 @@ class LoggerTest < Test::Unit::TestCase
176
179
  def test_append_by_conf
177
180
  confstr = "log_append: true"
178
181
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
182
+ RBatch.load_rbatch_config
179
183
 
180
184
  RBatch::Log.new({:name => "test_append" }) do | log |
181
185
  log.info("test_append1")
@@ -193,6 +197,7 @@ class LoggerTest < Test::Unit::TestCase
193
197
  def test_no_append_by_conf
194
198
  confstr = "log_append: false"
195
199
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
200
+ RBatch.load_rbatch_config
196
201
 
197
202
  RBatch::Log.new({ :name => "test_append" }) do | log |
198
203
  log.info("test_append1")
@@ -319,6 +324,7 @@ class LoggerTest < Test::Unit::TestCase
319
324
  def test_log_level_debug_by_conf
320
325
  confstr = "log_level: debug"
321
326
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
327
+ RBatch.load_rbatch_config
322
328
 
323
329
  RBatch::Log.new({ :name => "test_level" }) do | log |
324
330
  log.debug("test_debug")
@@ -340,6 +346,7 @@ class LoggerTest < Test::Unit::TestCase
340
346
  def test_log_level_info_by_conf
341
347
  confstr = "log_level: info"
342
348
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
349
+ RBatch.load_rbatch_config
343
350
  RBatch::Log.new({ :name => "test_level" }) do | log |
344
351
  log.debug("test_debug")
345
352
  log.info("test_info")
@@ -360,6 +367,7 @@ class LoggerTest < Test::Unit::TestCase
360
367
  def test_log_level_warn_by_conf
361
368
  confstr = "log_level: warn"
362
369
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
370
+ RBatch.load_rbatch_config
363
371
  RBatch::Log.new({ :name => "test_level" }) do | log |
364
372
  log.debug("test_debug")
365
373
  log.info("test_info")
@@ -380,6 +388,7 @@ class LoggerTest < Test::Unit::TestCase
380
388
  def test_log_level_error_by_conf
381
389
  confstr = "log_level: error"
382
390
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
391
+ RBatch.load_rbatch_config
383
392
  RBatch::Log.new({ :name => "test_level" }) do | log |
384
393
  log.debug("test_debug")
385
394
  log.info("test_info")
@@ -400,6 +409,7 @@ class LoggerTest < Test::Unit::TestCase
400
409
  def test_log_level_fatal_by_conf
401
410
  confstr = "log_level: fatal"
402
411
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
412
+ RBatch.load_rbatch_config
403
413
  RBatch::Log.new({ :name => "test_level" }) do | log |
404
414
  log.debug("test_debug")
405
415
  log.info("test_info")
@@ -510,6 +520,7 @@ class LoggerTest < Test::Unit::TestCase
510
520
  def test_i_log_level_debug_by_conf
511
521
  confstr = "log_level: debug"
512
522
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
523
+ RBatch.load_rbatch_config
513
524
 
514
525
  log = RBatch::Log.new({ :name => "test_level" })
515
526
  log.debug("test_debug")
@@ -545,6 +556,7 @@ class LoggerTest < Test::Unit::TestCase
545
556
  def test_delete_old_log_by_config
546
557
  confstr = "log_delete_old_log: true"
547
558
  open( RBatch.rbatch_config_path , "a" ){|f| f.write(confstr)}
559
+ RBatch.load_rbatch_config
548
560
 
549
561
  loglist = [*0..20].map do |day|
550
562
  File.join(@dir , (Date.today - day).strftime("%Y%m%d") + "_test_delete.log")
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rbatch
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.13.0
5
+ version: 1.13.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - fetaro
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-12-18 00:00:00 Z
13
+ date: 2013-12-20 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: RBatch has many fanctions to help with your making a batch script such as "data backup script" or "proccess starting script".
@@ -105,6 +105,10 @@ files:
105
105
  - sample/data/amAuthentication.error.1
106
106
  - sample/data/amAuthentication.error.2012-07-10
107
107
  - sample/log/empty
108
+ - spec/cmd_test.spec
109
+ - spec/config_test.spec
110
+ - spec/log_test.spec
111
+ - spec/rbatch_test.spec
108
112
  - test/cases/test_cmd.rb
109
113
  - test/cases/test_common_config.rb
110
114
  - test/cases/test_config.rb
@@ -139,6 +143,10 @@ signing_key:
139
143
  specification_version: 3
140
144
  summary: Ruby-based simple batch framework
141
145
  test_files:
146
+ - spec/cmd_test.spec
147
+ - spec/config_test.spec
148
+ - spec/log_test.spec
149
+ - spec/rbatch_test.spec
142
150
  - test/cases/test_cmd.rb
143
151
  - test/cases/test_common_config.rb
144
152
  - test/cases/test_config.rb