Exit_0 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ coverage
7
+ rdoc
8
+ .yardoc
data/Exit_0.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "Exit_0/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "Exit_0"
8
+ s.version = Exit_0::VERSION
9
+ s.authors = ["da99"]
10
+ s.email = ["i-hate-spam-45671204@mailinator.com"]
11
+ s.homepage = "https://github.com/da99/Exit_0"
12
+ s.summary = %q{Make sure your last child process exited with 0.}
13
+ s.description = %q{
14
+ A simple method that runs a child process and raises
15
+ Exit_Zero::Non_Zero if $?.exitstatus is not zero.
16
+ }
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_development_dependency 'bacon'
24
+ s.add_development_dependency 'rake'
25
+ s.add_development_dependency 'Bacon_Colored'
26
+ s.add_development_dependency 'pry'
27
+
28
+ # Specify any dependencies here; for example:
29
+ s.add_runtime_dependency 'posix-spawn'
30
+ s.add_runtime_dependency 'Split_Lines'
31
+ s.add_runtime_dependency 'open4'
32
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ source "http://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in Bob.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+
2
+ Exit\_0
3
+ =========
4
+
5
+ A simple method that raises Exit\_0::Non\_0
6
+ if $?.exitstatus is not 0.
7
+
8
+ Don't like Exit\_0? Try:
9
+
10
+ * [posix-spawn](https://github.com/rtomayko/posix-spawn).
11
+ That is the easiest way to handle child processes
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
+ ----
26
+
27
+ Exit\_0 runs your command through bash:
28
+
29
+ Exit_0 "
30
+ cd ~/
31
+ pwd
32
+ "
33
+
34
+ # --> Your command is processed as:
35
+ str = str.split("\n").map(:strip).map(&:reject).join('&&')
36
+ "bash -lc #{str.inspect}"
37
+
38
+ Exit\_0 lives in one file. So if you have any questions, [here
39
+ it is](https://github.com/da99/Exit\_0/blob/master/lib/Exit\_0.rb).
40
+
41
+
42
+ Installation
43
+ ------------
44
+
45
+ gem install Exit_0
46
+
47
+ Usage
48
+ ------
49
+
50
+ require "Exit_0"
51
+
52
+ Exit_0 'uptime'
53
+ Exit_0 'ls', :input=>' /some/dir '
54
+ Exit_0 { system "uptime" }
55
+
56
+ # The following raises an error.
57
+ Exit_0 'uptimeSS'
58
+ Exit_0 { `uptimeSSS` }
59
+
60
+ # Exit_0 and Exit_0 are the same.
61
+ Exit_0 'uptime'
62
+ Exit_0 'grep a', :input=>"a \n b \n c"
63
+
64
+ # Get results from standard output, standard error.
65
+ Exit_0('uptime').out # String is stripped.
66
+ Exit_0('uptimeSS').err # String is stripped.
67
+
68
+ Exit_0('uptime').raw_out # Raw string, no :strip used.
69
+ Exit_0('uptime').raw_err # Raw string, no :strip used.
70
+
71
+ Run Tests
72
+ ---------
73
+
74
+ git clone git@github.com:da99/Exit_0.git
75
+ cd Exit_0
76
+ bundle update
77
+ bundle exec bacon spec/main.rb
78
+
79
+ "I hate writing."
80
+ -----------------------------
81
+
82
+ If you know of existing software that makes the above redundant,
83
+ please tell me. The last thing I want to do is maintain code.
84
+
85
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class Exit_0
2
+ VERSION = "1.3.0"
3
+ end
data/lib/Exit_0.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'Exit_0/version'
2
+ require 'Split_Lines'
3
+ require 'posix/spawn'
4
+
5
+ def Exit_0 *cmd, &blok
6
+
7
+ both = !cmd.empty? && 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
14
+ else
15
+ r = p = Exit_0::Child.new(*cmd)
16
+ msg = p.err.strip.empty? ? p.cmd : p.err
17
+ msg << " (command: #{cmd})"
18
+ end
19
+
20
+
21
+ (r = r.status) if r.respond_to?(:status)
22
+ raise(Exit_0::Unknown_Exit, msg.inspect) unless r.respond_to?(:exitstatus)
23
+ raise(Exit_0::Non_0, "#{r.exitstatus} => #{msg}") if r.exitstatus != 0
24
+
25
+ p
26
+ end # === Exit_0
27
+
28
+ def Exit_Zero *cmd, &blok
29
+ Exit_0 *cmd, &blok
30
+ end
31
+
32
+ class Exit_0
33
+
34
+ Non_0 = Class.new(RuntimeError)
35
+ Unknown_Exit = Class.new(RuntimeError)
36
+
37
+ module Class_Methods
38
+ end # === Class_Methods
39
+
40
+ extend Class_Methods
41
+
42
+ class Child
43
+ module Base
44
+
45
+ attr_reader :cmd, :child
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
62
+ @child = POSIX::Spawn::Child.new(*cmd)
63
+ @cmd = cmd.join(' ')
64
+ end
65
+
66
+ def split_lines
67
+ Split_Lines(child.out)
68
+ end
69
+
70
+ %w{ out err }.each { |m|
71
+ eval %~
72
+ def raw_#{m}
73
+ child.#{m}
74
+ end
75
+
76
+ def #{m}
77
+ child.#{m}.strip
78
+ end
79
+ ~
80
+ }
81
+
82
+ def status
83
+ child.status
84
+ end
85
+
86
+ end # === Base
87
+ include Base
88
+ end # === Child
89
+
90
+ end # === class Exit_0
data/spec/helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.print e.message, "\n"
7
+ $stderr.print "Run `bundle install` to install missing gems\n"
8
+ exit e.status_code
9
+ end
10
+ require 'bacon'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+
15
+ Bacon.summary_on_exit
data/spec/main.rb ADDED
@@ -0,0 +1,15 @@
1
+
2
+ require File.expand_path('spec/helper')
3
+ require 'Exit_0'
4
+ require 'Bacon_Colored'
5
+ require 'pry'
6
+
7
+
8
+ # ======== Include the tests.
9
+ if ARGV.size > 1 && ARGV[1, ARGV.size - 1].detect { |a| File.exists?(a) }
10
+ # Do nothing. Bacon grabs the file.
11
+ else
12
+ Dir.glob('spec/tests/*.rb').each { |file|
13
+ require File.expand_path(file.sub('.rb', '')) if File.file?(file)
14
+ }
15
+ end
@@ -0,0 +1,31 @@
1
+ describe "Child.new" do
2
+
3
+ it 'executes given string' do
4
+ Exit_0::Child.new("pwd").raw_out.should == `pwd`
5
+ end
6
+
7
+ end # === Exit_0::Child
8
+
9
+ describe "Child#split_lines" do
10
+
11
+ it "returns split lines of output through :split_lines" do
12
+ r = Exit_0::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{ raw_out raw_err status }.each { |m|
21
+ o_m = m.sub("raw_", '')
22
+ it "sets :#{m} equal to Child :#{o_m}" do
23
+ cmd = %q! ruby -e "puts 'a'; warn 'b'; exit(127);"!
24
+ target = POSIX::Spawn::Child.new(cmd)
25
+ Exit_0::Child.new(cmd)
26
+ .send(m).should == target.send(o_m)
27
+ end
28
+ }
29
+
30
+ end # === Child delegate methods
31
+
@@ -0,0 +1,100 @@
1
+
2
+ describe "Exit_0 *cmd" do
3
+
4
+ it "accepts the same arguments as POSIX::Spawn::Child" do
5
+ Exit_0("dc", :input=>'4 4 + p').out
6
+ .should == "8"
7
+ end
8
+
9
+ it "raises Exit_0::Non_0 if command exits with non-0" do
10
+ lambda {
11
+ Exit_0 'uptimes'
12
+ }.should.raise(Exit_0::Non_0)
13
+ .message.should.match %r!127 => bash: uptimes: command not found!
14
+ end
15
+
16
+ it "returns a Exit_0::Child" do
17
+ Exit_0('whoami').class.should.be == Exit_0::Child
18
+ end
19
+
20
+ it "executes valid command" do
21
+ Exit_0('pwd').out.should == `pwd`.strip
22
+ end
23
+
24
+ it "raises ArgumentError if both a cmd and block are given" do
25
+ lambda { Exit_0('uptime') {} }
26
+ .should.raise(ArgumentError)
27
+ .message.should.match %r!are not allowed!i
28
+ end
29
+
30
+ it "combines a multi-line string into one string, joined by \"&&\"" do
31
+ Exit_0(%!
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_0(%!
40
+ cd ~/
41
+
42
+ pwd
43
+ !).out.should == `cd ~/ && pwd`.strip
44
+ end
45
+
46
+ end # === Exit_0 'cmd'
47
+
48
+ describe "Exit_0 { }" do
49
+
50
+ it "returns last value of the block" do
51
+ Exit_0{ POSIX::Spawn::Child.new("ls ~") }
52
+ .out.should. == `ls ~`
53
+ end
54
+
55
+ it "raises Exit_0::Non_0 if return value has exitstatus != 0" do
56
+ b = lambda { POSIX::Spawn::Child.new("something_not_found") }
57
+ lambda {
58
+ Exit_0(&b)
59
+ }.should.raise(Exit_0::Non_0)
60
+ .message.should == "127 => #{b.inspect}"
61
+ end
62
+
63
+ it "raise Unknown_Exit if block return value does not respond to :status and :exitstatus" do
64
+ target = lambda { :a }
65
+ lambda {
66
+ Exit_0(&target)
67
+ }.should.raise(Exit_0::Unknown_Exit)
68
+ .message.should.match %r!#{Regexp.escape target.inspect}!
69
+ end
70
+
71
+ end # === Exit_0 { }
72
+
73
+ describe "Exit_0::Child" do
74
+
75
+ # ---- :out
76
+
77
+ it "returns stripped :out" do
78
+ Exit_0::Child.new("pwd").out
79
+ .should == `pwd`.strip
80
+ end
81
+
82
+ it "returns raw output for :raw_out" do
83
+ Exit_0::Child.new("pwd").raw_out
84
+ .should == `pwd`
85
+ end
86
+
87
+ # ---- :err
88
+
89
+ it "returns stripped :err" do
90
+ Exit_0::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_0::Child.new("no_exist").err
96
+ .should.match %r!no_exist: command not found!
97
+ end
98
+
99
+ end # === Exit_0::Child
100
+
@@ -0,0 +1,12 @@
1
+
2
+
3
+
4
+ describe "Exit_Zero" do
5
+
6
+ it "accepts the same arguments as Exit_Zero" do
7
+ Exit_Zero("grep a", :input=>"a\nb\nc")
8
+ .out.should == "a"
9
+ end
10
+
11
+ end # === Exit_Zero
12
+
data/spec/tests/bin.rb ADDED
@@ -0,0 +1,10 @@
1
+
2
+ describe "permissions of bin/" do
3
+
4
+ it "should be 750" do
5
+ `stat -c %a bin`.strip
6
+ .should.be == "750"
7
+ end
8
+
9
+ end # === permissions of bin/
10
+
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Exit_0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - da99
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bacon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: Bacon_Colored
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: posix-spawn
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: Split_Lines
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: open4
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: ! "\n A simple method that runs a child process and raises\n Exit_Zero::Non_Zero
127
+ if $?.exitstatus is not zero.\n "
128
+ email:
129
+ - i-hate-spam-45671204@mailinator.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - Exit_0.gemspec
136
+ - Gemfile
137
+ - README.md
138
+ - Rakefile
139
+ - lib/Exit_0.rb
140
+ - lib/Exit_0/version.rb
141
+ - spec/helper.rb
142
+ - spec/main.rb
143
+ - spec/tests/Child.rb
144
+ - spec/tests/Exit_0.rb
145
+ - spec/tests/Exit_Zero.rb
146
+ - spec/tests/bin.rb
147
+ homepage: https://github.com/da99/Exit_0
148
+ licenses: []
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 1.8.23
168
+ signing_key:
169
+ specification_version: 3
170
+ summary: Make sure your last child process exited with 0.
171
+ test_files: []