Exit_Zero 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source "http://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in Bob.gemspec
6
6
  gemspec
7
+ gem 'open4'
@@ -1,3 +1,3 @@
1
1
  class Exit_Zero
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/Exit_Zero.rb CHANGED
@@ -4,41 +4,42 @@ require 'posix/spawn'
4
4
 
5
5
  def Exit_Zero cmd=:no_cmd, &blok
6
6
 
7
- if cmd == :no_cmd
8
- r = yield
9
- Exit_Zero.check blok, r
10
- r
7
+ both = cmd != :no_cmd && block_given?
8
+ raise ArgumentError, "Both command and block are not allowed." if both
9
+
10
+ if block_given?
11
+ cmd = blok
12
+ r = p = blok.call
13
+ msg = cmd
11
14
  else
12
- raise ArgumentError, "Both command and block are not allowed." if block_given?
13
- r = Exit_Zero::Result.new(POSIX::Spawn::Child.new(cmd))
14
- Exit_Zero.check cmd, r.status
15
- r
15
+ r = p = Exit_Zero::Child.new(cmd)
16
+ msg = p.err.strip.empty? ? cmd : p.err
16
17
  end
17
-
18
+
19
+ (r = r.status) if r.respond_to?(:status)
20
+ raise(Exit_Zero::Unknown_Exit, msg.inspect) unless r.respond_to?(:exitstatus)
21
+ raise(Exit_Zero::Non_Zero, "#{r.exitstatus} => #{msg}") if r.exitstatus != 0
22
+
23
+ p
18
24
  end # === Exit_Zero
19
25
 
20
26
  class Exit_Zero
21
27
 
28
+ Non_Zero = Class.new(RuntimeError)
29
+ Unknown_Exit = Class.new(RuntimeError)
30
+
22
31
  module Class_Methods
23
-
24
- def check cmd, r = :no_status
25
- r = $? if !r.respond_to?(:exitstatus)
26
- raise(Exit_Zero::Non_Zero, cmd) if r.exitstatus != 0
27
- end
28
-
29
32
  end # === Class_Methods
30
33
 
31
34
  extend Class_Methods
32
-
33
- class Non_Zero < RuntimeError
34
- end # === class Non_Zero
35
35
 
36
- class Result
36
+ class Child
37
37
  module Base
38
38
 
39
- attr_reader :child
40
- def initialize child
41
- @child = child
39
+ attr_reader :cmd, :child
40
+ def initialize cmd
41
+ @cmd = cmd
42
+ @child = POSIX::Spawn::Child.new(cmd)
42
43
  end
43
44
 
44
45
  def split_lines
@@ -55,6 +56,6 @@ class Exit_Zero
55
56
 
56
57
  end # === Base
57
58
  include Base
58
- end # === Result
59
+ end # === Child
59
60
 
60
61
  end # === class Exit_Zero
@@ -0,0 +1,30 @@
1
+ describe "Child.new" do
2
+
3
+ it 'executes given string' do
4
+ Exit_Zero::Child.new("pwd").out.should == `pwd`
5
+ end
6
+
7
+ end # === Exit_Zero::Child
8
+
9
+ describe "Child#split_lines" do
10
+
11
+ it "returns split lines of output through :split_lines" do
12
+ r = Exit_Zero::Child.new('ls -Al')
13
+ r.split_lines.should == `ls -Al`.strip.split("\n")
14
+ end
15
+
16
+ end # === Child#split_lines
17
+
18
+ describe "Child delegate methods" do
19
+
20
+ %w{ out err status }.each { |m|
21
+ it "sets :#{m} equal to Child##{m}" do
22
+ cmd = %q! ruby -e "puts 'a'; warn 'b'; exit(127);"!
23
+ target = POSIX::Spawn::Child.new(cmd)
24
+ Exit_Zero::Child.new(cmd)
25
+ .send(m).should == target.send(m)
26
+ end
27
+ }
28
+
29
+ end # === Child delegate methods
30
+
@@ -1,20 +1,3 @@
1
- describe "Exit_Zero::Result" do
2
-
3
- it "provides split_lines" do
4
- r = Exit_Zero::Result.new(POSIX::Spawn::Child.new('ls -Al'))
5
- r.split_lines.should == `ls -Al`.strip.split("\n")
6
- end
7
-
8
- %w{ out err status }.each { |m|
9
- it "sets :#{m} equal to Child##{m}" do
10
- cmd = %q! ruby -e "puts 'a'; warn 'b'; exit(127);"!
11
- target = POSIX::Spawn::Child.new(cmd)
12
- Exit_Zero::Result.new(POSIX::Spawn::Child.new(cmd))
13
- .send(m).should == target.send(m)
14
- end
15
- }
16
-
17
- end # === Exit_Zero::Result
18
1
 
19
2
  describe "Exit_Zero 'cmd'" do
20
3
 
@@ -22,10 +5,11 @@ describe "Exit_Zero 'cmd'" do
22
5
  lambda {
23
6
  Exit_Zero 'uptimes'
24
7
  }.should.raise(Exit_Zero::Non_Zero)
8
+ .message.should == %!127 => uptimes!
25
9
  end
26
10
 
27
- it "returns a Exit_Zero::Result" do
28
- Exit_Zero('whoami').class.should.be == Exit_Zero::Result
11
+ it "returns a Exit_Zero::Child" do
12
+ Exit_Zero('whoami').class.should.be == Exit_Zero::Child
29
13
  end
30
14
 
31
15
  it "executes valid command" do
@@ -42,12 +26,25 @@ end # === Exit_Zero 'cmd'
42
26
 
43
27
  describe "Exit_Zero { }" do
44
28
 
45
- it "raises Exit_Zero::Non_Zero if $?.exitstatus != 0" do
29
+ it "returns last value of the block" do
30
+ Exit_Zero{ POSIX::Spawn::Child.new("ls ~") }
31
+ .out.should. == `ls ~`
32
+ end
33
+
34
+ it "raises Exit_Zero::Non_Zero if return value has exitstatus != 0" do
35
+ b = lambda { POSIX::Spawn::Child.new("something_not_found") }
46
36
  lambda {
47
- Exit_Zero {
48
- POSIX::Spawn::Child.new("uptimes")
49
- }
37
+ Exit_Zero(&b)
50
38
  }.should.raise(Exit_Zero::Non_Zero)
39
+ .message.should == "127 => #{b.inspect}"
40
+ end
41
+
42
+ it "raise Unknown_Exit if block return value does not respond to :status and :exitstatus" do
43
+ target = lambda { :a }
44
+ lambda {
45
+ Exit_Zero(&target)
46
+ }.should.raise(Exit_Zero::Unknown_Exit)
47
+ .message.should.match %r!#{Regexp.escape target.inspect}!
51
48
  end
52
49
 
53
50
  end # === Exit_Zero { }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Exit_Zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-20 00:00:00.000000000 Z
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
16
- requirement: &8271400 !ruby/object:Gem::Requirement
16
+ requirement: &14691860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *8271400
24
+ version_requirements: *14691860
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &8270660 !ruby/object:Gem::Requirement
27
+ requirement: &14691120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *8270660
35
+ version_requirements: *14691120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: Bacon_Colored
38
- requirement: &8270180 !ruby/object:Gem::Requirement
38
+ requirement: &14690640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *8270180
46
+ version_requirements: *14690640
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &8269700 !ruby/object:Gem::Requirement
49
+ requirement: &14690160 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *8269700
57
+ version_requirements: *14690160
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: Split_Lines
60
- requirement: &8269260 !ruby/object:Gem::Requirement
60
+ requirement: &14689720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *8269260
68
+ version_requirements: *14689720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: posix-spawn
71
- requirement: &8268760 !ruby/object:Gem::Requirement
71
+ requirement: &14689200 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *8268760
79
+ version_requirements: *14689200
80
80
  description: ! "\n A simple method that raises Exit_Zero::Non_Zero\n if $?.exitstatus
81
81
  is not zero.\n Be sure to use the \"posix-spawn\" since almost everything \n
82
82
  \ else is just plain wrong when handling child processes.\n "
@@ -94,6 +94,7 @@ files:
94
94
  - lib/Exit_Zero/version.rb
95
95
  - spec/helper.rb
96
96
  - spec/main.rb
97
+ - spec/tests/Child.rb
97
98
  - spec/tests/Exit_Zero.rb
98
99
  - spec/tests/bin.rb
99
100
  homepage: ''