Exit_Zero 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,33 +5,63 @@ Exit\_Zero
5
5
  A simple method that raises Exit\_Zero::Non\_Zero
6
6
  if $?.exitstatus is not zero.
7
7
 
8
- If you want other functionality, such as capturing
9
- standard output/error, then you should use
10
- [posix-spawn](https://github.com/rtomayko/posix-spawn).
8
+ Don't like Exit\_Zero? Try:
9
+
10
+ * [posix-spawn](https://github.com/rtomayko/posix-spawn).
11
11
  That is the easiest way to handle child processes
12
- (aka shelling out a command).
13
- [Here are other alternatives](http://stackoverflow.com/questions/6338908/ruby-difference-between-exec-system-and-x-or-backticks).
12
+ (aka shelling out).
13
+ * [POpen4](https://github.com/shairontoledo/popen4) Don't confuse it with regular popen4.
14
+ * [Here are other alternatives](http://stackoverflow.com/questions/6338908/ruby-difference-between-exec-system-and-x-or-backticks).
15
+
16
+ Windows
17
+ ------
18
+
19
+ Use something else. Check the previous list above
20
+ for other alternatives,
21
+ especialy [POpen4](https://github.com/shairontoledo/popen4),
22
+ which is Windows and POSIX compatible.
23
+
24
+ Implementation
25
+ ----
14
26
 
27
+ Exit\_Zero runs your command through bash:
28
+
29
+ your command: uptime
30
+ Final result: POSIX::Spawn::Child.new "bash -lc #{cmd.inspect}"
31
+
32
+ Exit\_Zero lives in one file. So if you have any questions, [here
33
+ it is](https://github.com/da99/Exit\_Zero/blob/master/lib/Exit\_Zero.rb).
15
34
 
16
35
 
17
36
  Installation
18
37
  ------------
19
38
 
20
- gem 'Exit_Zero'
39
+ gem install Exit_Zero
21
40
 
22
- Useage
41
+ Usage
23
42
  ------
24
43
 
25
44
  require "Exit_Zero"
26
45
 
27
- Exit_Zero( 'uptime' )
28
- Exit_Zero( 'ls', :input=>' /some/dir ')
46
+ Exit_Zero 'uptime'
47
+ Exit_Zero 'ls', :input=>' /some/dir '
29
48
  Exit_Zero { system "uptime" }
30
49
 
31
50
  # The following raises an error.
32
- Exit_Zero( 'uptimess' )
33
- Exit_Zero { `uptimesss` }
51
+ Exit_Zero 'uptimeSS'
52
+ Exit_Zero { `uptimeSSS` }
53
+
54
+ # Exit_0 and Exit_Zero are the same.
55
+ Exit_0 'uptime'
56
+ Exit_0 'grep a', :input=>"a \n b \n c"
34
57
 
58
+ # Get results from standard output, standard error.
59
+ Exit_0('uptime').out # String is stripped.
60
+ Exit_0('uptimeSS').err # String is stripped.
61
+
62
+ Exit_0('uptime').raw_out # Raw string, no :strip used.
63
+ Exit_0('uptime').raw_err # Raw string, no :strip used.
64
+
35
65
  Run Tests
36
66
  ---------
37
67
 
data/lib/Exit_Zero.rb CHANGED
@@ -25,6 +25,10 @@ def Exit_Zero *cmd, &blok
25
25
  p
26
26
  end # === Exit_Zero
27
27
 
28
+ def Exit_0 *cmd, &blok
29
+ Exit_Zero *cmd, &blok
30
+ end
31
+
28
32
  class Exit_Zero
29
33
 
30
34
  Non_Zero = Class.new(RuntimeError)
@@ -40,6 +44,21 @@ class Exit_Zero
40
44
 
41
45
  attr_reader :cmd, :child
42
46
  def initialize *cmd
47
+ if cmd[0].is_a?(String)
48
+
49
+ if cmd[0]["\n"]
50
+ cmd[0] = begin
51
+ cmd[0]
52
+ .split("\n")
53
+ .map(&:strip)
54
+ .reject(&:empty?)
55
+ .join(" && ")
56
+ end
57
+ end
58
+
59
+ cmd[0] = "bash -lc #{cmd[0].inspect}"
60
+
61
+ end
43
62
  @child = POSIX::Spawn::Child.new(*cmd)
44
63
  @cmd = cmd.join(' ')
45
64
  end
@@ -48,14 +67,22 @@ class Exit_Zero
48
67
  Split_Lines(child.out)
49
68
  end
50
69
 
51
- %w{ out err status }.each { |m|
70
+ %w{ out err }.each { |m|
52
71
  eval %~
53
- def #{m}
72
+ def raw_#{m}
54
73
  child.#{m}
55
74
  end
75
+
76
+ def #{m}
77
+ child.#{m}.strip
78
+ end
56
79
  ~
57
80
  }
58
81
 
82
+ def status
83
+ child.status
84
+ end
85
+
59
86
  end # === Base
60
87
  include Base
61
88
  end # === Child
@@ -1,3 +1,3 @@
1
1
  class Exit_Zero
2
- VERSION = "1.1.3"
2
+ VERSION = "1.2.0"
3
3
  end
data/spec/tests/Child.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  describe "Child.new" do
2
2
 
3
3
  it 'executes given string' do
4
- Exit_Zero::Child.new("pwd").out.should == `pwd`
4
+ Exit_Zero::Child.new("pwd").raw_out.should == `pwd`
5
5
  end
6
6
 
7
7
  end # === Exit_Zero::Child
@@ -17,12 +17,13 @@ end # === Child#split_lines
17
17
 
18
18
  describe "Child delegate methods" do
19
19
 
20
- %w{ out err status }.each { |m|
21
- it "sets :#{m} equal to Child##{m}" do
20
+ %w{ raw_out raw_err status }.each { |m|
21
+ o_m = m.sub("raw_", '')
22
+ it "sets :#{m} equal to Child :#{o_m}" do
22
23
  cmd = %q! ruby -e "puts 'a'; warn 'b'; exit(127);"!
23
24
  target = POSIX::Spawn::Child.new(cmd)
24
25
  Exit_Zero::Child.new(cmd)
25
- .send(m).should == target.send(m)
26
+ .send(m).should == target.send(o_m)
26
27
  end
27
28
  }
28
29
 
@@ -0,0 +1,9 @@
1
+ describe "Exit_0" do
2
+
3
+ it "accepts the same arguments as Exit_Zero" do
4
+ Exit_0("grep a", :input=>"a\nb\nc")
5
+ .out.should == "a"
6
+ end
7
+
8
+ end # === Exit_0
9
+
@@ -3,14 +3,14 @@ describe "Exit_Zero *cmd" do
3
3
 
4
4
  it "accepts the same arguments as POSIX::Spawn::Child" do
5
5
  Exit_Zero("dc", :input=>'4 4 + p').out
6
- .should == "8\n"
6
+ .should == "8"
7
7
  end
8
-
8
+
9
9
  it "raises Exit_Zero::Non_Zero if command exits with non-zero" do
10
10
  lambda {
11
11
  Exit_Zero 'uptimes'
12
12
  }.should.raise(Exit_Zero::Non_Zero)
13
- .message.should.match %r!127 => uptimes!
13
+ .message.should.match %r!127 => bash: uptimes: command not found!
14
14
  end
15
15
 
16
16
  it "returns a Exit_Zero::Child" do
@@ -18,7 +18,7 @@ describe "Exit_Zero *cmd" do
18
18
  end
19
19
 
20
20
  it "executes valid command" do
21
- Exit_Zero('pwd').out.should == `pwd`
21
+ Exit_Zero('pwd').out.should == `pwd`.strip
22
22
  end
23
23
 
24
24
  it "raises ArgumentError if both a cmd and block are given" do
@@ -27,6 +27,22 @@ describe "Exit_Zero *cmd" do
27
27
  .message.should.match %r!are not allowed!i
28
28
  end
29
29
 
30
+ it "combines a multi-line string into one string, joined by \"&&\"" do
31
+ Exit_Zero(%!
32
+ cd ~/
33
+ pwd
34
+ !)
35
+ .out.should == `cd ~/ && pwd`.strip
36
+ end
37
+
38
+ it "ignores empty lines in a multi-line string" do
39
+ Exit_Zero(%!
40
+ cd ~/
41
+
42
+ pwd
43
+ !).out.should == `cd ~/ && pwd`.strip
44
+ end
45
+
30
46
  end # === Exit_Zero 'cmd'
31
47
 
32
48
  describe "Exit_Zero { }" do
@@ -54,3 +70,33 @@ describe "Exit_Zero { }" do
54
70
 
55
71
  end # === Exit_Zero { }
56
72
 
73
+ describe "Exit_Zero::Child" do
74
+
75
+ # ---- :out
76
+
77
+ it "returns stripped :out" do
78
+ Exit_Zero::Child.new("pwd").out
79
+ .should == `pwd`.strip
80
+ end
81
+
82
+ it "returns raw output for :raw_out" do
83
+ Exit_Zero::Child.new("pwd").raw_out
84
+ .should == `pwd`
85
+ end
86
+
87
+ # ---- :err
88
+
89
+ it "returns stripped :err" do
90
+ Exit_Zero::Child.new("pwdssssss").err
91
+ .should == `bash -lc "pwdssssss" 2>&1`.strip
92
+ end
93
+
94
+ it "returns raw output for :raw_out" do
95
+ Exit_Zero::Child.new("no_exist").err
96
+ .should.match %r!no_exist: command not found!
97
+ end
98
+
99
+ end # === Exit_Zero::Child
100
+
101
+
102
+
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: 1.1.3
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-13 00:00:00.000000000 Z
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
@@ -125,6 +125,7 @@ files:
125
125
  - spec/helper.rb
126
126
  - spec/main.rb
127
127
  - spec/tests/Child.rb
128
+ - spec/tests/Exit_0.rb
128
129
  - spec/tests/Exit_Zero.rb
129
130
  - spec/tests/bin.rb
130
131
  homepage: https://github.com/da99/Exit_Zero