dotanuki-ruby 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,2 @@
1
+ require 'autotest/bundler'
2
+
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ .yardoc
4
+ Gemfile.lock
5
+ pkg
6
+ doc
7
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+
6
+ branches:
7
+ only:
8
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dotanuki.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
3
+ watch(%r{^(spec/.+_spec\.rb)}) { |m| "#{m}" }
4
+
5
+ watch('spec/spec_helper.rb') { "spec" }
6
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.textile ADDED
@@ -0,0 +1,62 @@
1
+ h1. Dotanuki "!https://secure.travis-ci.org/pmenglund/dotanuki.png?branch=master!":http://travis-ci.org/pmenglund/dotanuki
2
+
3
+ Simple but effective executioner of commands, which will deal correctly with failed commands.
4
+
5
+ There are two versions of the Dotanuki gem "dotanuki":http://rubygems.org/gems/dotanuki which uses "posix-spawn":http://rubygems.org/gems/posix-spawn and a pure ruby version "dotanuki-ruby":http://rubygems.org/gems/dotanuki-ruby which uses "open4":http://rubygems.org/gems/open4 instead.
6
+
7
+ Note that if the pure ruby version can load "posix-spawn":http://rubygems.org/gems/posix-spawn it will use it.
8
+
9
+ h1. Examples
10
+
11
+ In the following example, if the <code>mkdir</code> fails, none of the other commands will be executed.
12
+
13
+ <pre>
14
+ require "dotanuki"
15
+
16
+ class Example
17
+ include Dotanuki
18
+
19
+ def test
20
+ commands = [
21
+ "mkdir /tmp/foo",
22
+ "cp /etc/hosts /tmp/foo",
23
+ "cp /etc/passwd /tmp/foo"
24
+ ]
25
+
26
+ result = execute(commands)
27
+ if result.failed?
28
+ puts "execution failed: #{result.fail_message}"
29
+ end
30
+ end
31
+ end
32
+ </pre>
33
+
34
+ It can also be used with a <code>guard</code> block, which will raise an <code>ExecError</code> if a command fails.
35
+
36
+ <pre>
37
+ require "dotanuki"
38
+
39
+ class Example
40
+ include Dotanuki
41
+
42
+ def test
43
+ guard do
44
+ execute "mkdir /tmp/foo"
45
+ execute "cp /etc/hosts /tmp/foo"
46
+ execute "cp /etc/passwd /tmp/foo"
47
+ end
48
+ end
49
+ end
50
+ </pre>
51
+
52
+ If you want to use dotanuki in a class method, you have to use the module method
53
+
54
+ <pre>
55
+ require "dotanuki"
56
+
57
+ class Example
58
+ def self.test?
59
+ Dotanuki.execute("mkdir /tmp/foo").ok?
60
+ end
61
+ end
62
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => [:spec]
5
+
6
+ desc "run rspec tests"
7
+ task :spec => [:"native:spec", :"pure:spec"]
8
+
9
+ desc "build gems"
10
+ task :build => [:"native:build", :"pure:build"]
11
+
12
+ desc "release gems"
13
+ task :release => [:"native:release", :"pure:release"]
14
+
15
+ namespace "native" do
16
+ Bundler::GemHelper.install_tasks :name => 'dotanuki'
17
+ RSpec::Core::RakeTask.new(:spec)
18
+ end
19
+
20
+ namespace "pure" do
21
+ Bundler::GemHelper.install_tasks :name => 'dotanuki-ruby'
22
+ RSpec::Core::RakeTask.new(:spec)
23
+ end
24
+
25
+ desc "run autotest"
26
+ task :autotest do
27
+ system "autotest"
28
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dotanuki/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dotanuki-ruby"
7
+ s.version = Dotanuki::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Martin Englund"]
10
+ s.email = ["martin@englund.nu"]
11
+ s.homepage = "https://github.com/pmenglund/dotanuki"
12
+ s.summary = %q{Command executioner}
13
+ s.description = %q{A pure ruby command executioner which doesn't blindly stumble on when a command fails'}
14
+
15
+ s.rubyforge_project = "dotanuki"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "open4"
23
+
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "guard-rspec"
26
+ s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "rake"
28
+
29
+ end
data/dotanuki.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dotanuki/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dotanuki"
7
+ s.version = Dotanuki::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Martin Englund"]
10
+ s.email = ["martin@englund.nu"]
11
+ s.homepage = "https://github.com/pmenglund/dotanuki"
12
+ s.summary = %q{Command executioner}
13
+ s.description = %q{A command executioner which doesn't blindly stumble on when a command fails'}
14
+
15
+ s.rubyforge_project = "dotanuki"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "posix-spawn"
23
+
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "guard-rspec"
26
+ s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "rake"
28
+
29
+ end
data/lib/dotanuki.rb ADDED
@@ -0,0 +1,189 @@
1
+ # Module intented to be included into classes which execute system commands
2
+ # @author Martin Englund
3
+ module Dotanuki
4
+
5
+ begin
6
+ require 'posix/spawn'
7
+ POSIX_SPAWN = true
8
+ rescue LoadError
9
+ require 'open4'
10
+ POSIX_SPAWN = false
11
+ end
12
+
13
+ # Error raised when an execution error occurs
14
+ class ExecError < StandardError
15
+ # Create a new ExecError
16
+ #
17
+ # @param [String] message error message
18
+ def initialize(message)
19
+ super(message)
20
+ end
21
+ end
22
+
23
+ # Result of one or more command executions
24
+ class ExecResult
25
+
26
+ # Array of stdout from each command executed
27
+ # @return [Array]
28
+ attr_reader :stdout
29
+
30
+ # Array of stderr from each command executed
31
+ # @return [Array]
32
+ attr_reader :stderr
33
+
34
+ # Exit status of the command that failed, nil if the command was not found
35
+ # and 0 if all commands succeeded
36
+ #
37
+ # @return [Fixnum]
38
+ attr_reader :status
39
+
40
+ # Index of the command that failed, or nil if all commands succeeded
41
+ # @return [Fixnum]
42
+ attr_reader :failed_index
43
+
44
+ def initialize
45
+ @stdout = []
46
+ @stderr = []
47
+ @status = 0
48
+ @failed_index = nil
49
+ end
50
+
51
+ # Returns true if a command has failed
52
+ def failed?
53
+ status != 0
54
+ end
55
+
56
+ # Returns true if a command has not failed
57
+ def ok?
58
+ status == 0
59
+ end
60
+
61
+ # Returns stderr for the command that failed
62
+ def fail_message
63
+ stderr[@failed_index]
64
+ end
65
+
66
+ # Add the result of a command execution
67
+ def add(stdout, stderr, status)
68
+ @stdout << stdout
69
+ @stderr << stderr
70
+ if status.nil? || status != 0
71
+ @status = status
72
+ @failed_index = @stdout.size - 1
73
+ end
74
+ end
75
+
76
+ # Add another [ExecResult] to this
77
+ def <<(result)
78
+ raise ArgumentError unless result.is_a?(ExecResult)
79
+ # TODO merge correctly
80
+ add(result.stdout, result.stderr, result.status)
81
+ end
82
+ end
83
+
84
+ # Default options for executing commands
85
+ DEFAULT_OPTIONS = {:on_error => :exception}
86
+
87
+ # @param [Hash] options the options for error handling
88
+ # @option options [Symbol] :on_error How to handle errors,
89
+ # can be either `:exception` or `:silent`
90
+ def initialize(options={})
91
+ @defaults = DEFAULT_OPTIONS.merge(options)
92
+ end
93
+
94
+ # Execute commands wrapped in a block
95
+ #
96
+ # @param [Hash] options (see #guard)
97
+ # @return [ExecResult]
98
+ # @example
99
+ # guard do
100
+ # execute "uname -a"
101
+ # execute "ls /does/not/exist"
102
+ # end
103
+ # @note this method isn't thread safe
104
+ # @todo pass an environment too
105
+ def guard(options={}, &block)
106
+ # if guard is called as a module function @defaults is not set
107
+ opts = (@defaults || {}).merge(options)
108
+ validate_options(opts)
109
+ # TODO this is not thread safe
110
+ @guard = ExecResult.new
111
+ yield
112
+ clear_guard
113
+ rescue ExecError => e
114
+ result = clear_guard
115
+ raise e if opts[:on_error] == :exception
116
+ result
117
+ end
118
+
119
+ # Execute one or more commands
120
+ #
121
+ # @param [String, Array] commands string or array containing the command to be executed
122
+ # @param [Hash] options (see #guard)
123
+ # @return [ExecResult]
124
+ # @todo pass an environment too
125
+ def execute(commands, options={})
126
+ validate_options(options)
127
+
128
+ result = ExecResult.new
129
+
130
+ [commands].flatten.each do |command|
131
+ begin
132
+ if POSIX_SPAWN
133
+ child = POSIX::Spawn::Child.new(command)
134
+ stdout = child.out.strip
135
+ stderr = child.err.strip
136
+ status = child.status
137
+ else
138
+ status = Open4::popen4(command) do |pid, stdin, out, err|
139
+ stdout = out.read.strip
140
+ stderr = err.read.strip
141
+ end
142
+ end
143
+ result.add(stdout, stderr, status.exitstatus)
144
+
145
+ if status.exitstatus != 0
146
+ if options[:on_error] == :exception || @guard
147
+ @guard << result if @guard
148
+ raise ExecError, stderr
149
+ end
150
+ break
151
+ end
152
+ rescue Errno::ENOENT
153
+ result.add(nil, nil, nil)
154
+ if options[:on_error] == :exception
155
+ @guard << result if @guard
156
+ raise ExecError, "#{command}: command not found"
157
+ end
158
+ break
159
+ end
160
+ end
161
+
162
+ @guard << result if @guard
163
+
164
+ return result
165
+ end
166
+
167
+ # Validates options for Dotanuki#execute or Dotanuki#guard
168
+ #
169
+ # @raise [ArgumentError] if an unknown option is given
170
+ def validate_options(options)
171
+ options.each do |option, value|
172
+ if option == :on_error && ! [:exception, :silent].include?(value)
173
+ raise ArgumentError, "illegal value for option #{option}: #{value}"
174
+ end
175
+ raise ArgumentError, "illegal option: #{option}" if option != :on_error
176
+ end
177
+ end
178
+
179
+ # TODO this is not thread safe
180
+ def clear_guard
181
+ result = @guard
182
+ @guard = nil
183
+ result
184
+ end
185
+
186
+ # TODO don't expose validate_options & clear_guard
187
+ module_function :execute, :guard, :validate_options, :clear_guard
188
+
189
+ end
@@ -0,0 +1,4 @@
1
+ module Dotanuki
2
+ # gem version
3
+ VERSION = "0.3.0"
4
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotanuki do
4
+ include Dotanuki
5
+
6
+ NON_EXISTING_PATH = "/not/an/existing/program"
7
+ COMMAND_NOT_FOUND = NON_EXISTING_PATH
8
+ COMMAND_FAILED = "ls -d #{NON_EXISTING_PATH}"
9
+
10
+ EXISTING_PATH = "/"
11
+ EXISTING_COMMAND = "ls -d #{EXISTING_PATH}"
12
+
13
+ describe "guard" do
14
+ it "should collect all output" do
15
+ guard do
16
+ execute("id")
17
+ execute("ls")
18
+ end.stdout.size.should == 2
19
+ end
20
+
21
+ it "should stop when one item fails" do
22
+ guard(:on_error => :silent) do
23
+ execute("id")
24
+ execute(COMMAND_FAILED)
25
+ execute("uname -n")
26
+ end.failed_index.should == 1
27
+ end
28
+
29
+ it "should raise an exception by default on error" do
30
+ lambda { guard do
31
+ execute(COMMAND_FAILED)
32
+ end }.should raise_error
33
+ end
34
+
35
+ it "should not raise an exception on error when silent" do
36
+ lambda { guard(:on_error => :silent) do
37
+ execute(COMMAND_FAILED)
38
+ end }.should_not raise_error
39
+ end
40
+ end
41
+
42
+ describe "execute" do
43
+
44
+ describe "missing command" do
45
+ it "should stop on first" do
46
+ r = execute([COMMAND_NOT_FOUND, EXISTING_COMMAND])
47
+ r.failed_index.should == 0
48
+ end
49
+
50
+ it "should stop on last" do
51
+ r = execute([EXISTING_COMMAND, COMMAND_NOT_FOUND])
52
+ r.failed_index.should == 1
53
+ end
54
+ end
55
+
56
+ describe "failing command" do
57
+
58
+ it "should stop when the first fails" do
59
+ r = execute([COMMAND_FAILED, "id", "id"])
60
+ r.failed_index.should == 0
61
+ r.status.should_not == 0
62
+ end
63
+
64
+ it "should stop on failure" do
65
+ r = execute(["id", COMMAND_FAILED, "id"])
66
+ r.failed_index.should == 1
67
+ r.status.should_not == 0
68
+ end
69
+
70
+ it "should stop when the last fails" do
71
+ r = execute(["id", "id", COMMAND_FAILED])
72
+ r.failed_index.should == 2
73
+ r.status.should_not == 0
74
+ end
75
+
76
+ it "should not return 0" do
77
+ execute(COMMAND_FAILED).status.should_not == 0
78
+ end
79
+
80
+ it "should capture stderr" do
81
+ r = execute(COMMAND_FAILED)
82
+ r.stderr[0].should match /ls: .*#{NON_EXISTING_PATH}: No such file or directory/
83
+ end
84
+
85
+ end
86
+
87
+ it "should return 0 when the command succeeds" do
88
+ execute("ls -d /").status.should == 0
89
+ end
90
+
91
+ it "should execute all commands in an array" do
92
+ r = execute(["id", "ls -d /", "id"])
93
+ r.status.should == 0
94
+ r.stdout.size.should == 3
95
+ r.stderr.size.should == 3
96
+ end
97
+
98
+ it "should execute a single command" do
99
+ r = execute("echo 'bar'")
100
+ r.status.should == 0
101
+ r.stdout.should == ["bar"]
102
+ r.stderr.should == [""]
103
+ end
104
+
105
+ describe "with exception option should throw an exception" do
106
+ it "on missing command" do
107
+ lambda { execute(COMMAND_NOT_FOUND, {:on_error => :exception}) }.should raise_error Dotanuki::ExecError
108
+ end
109
+
110
+ it "exception failing" do
111
+ lambda { execute(COMMAND_FAILED, {:on_error => :exception}) }.should raise_error Dotanuki::ExecError
112
+ end
113
+ end
114
+
115
+ it "should raise an error on invalid options" do
116
+ lambda { execute(COMMAND_NOT_FOUND, {:on_error => :asd}) }.should raise_error ArgumentError
117
+ end
118
+
119
+ it "should raise an error on invalid option argument" do
120
+ lambda { execute(COMMAND_NOT_FOUND, {:asd => :asd}) }.should raise_error ArgumentError
121
+ end
122
+
123
+ it "should supply correct info on a failing command" do
124
+ r = execute(COMMAND_FAILED)
125
+ r.ok?.should be_false
126
+ r.failed?.should be_true
127
+ r.fail_message.should_not be_empty
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotanuki do
4
+ it "should execute module function" do
5
+ Dotanuki.execute("ls /").failed?.should be_false
6
+ end
7
+
8
+ describe "#guard" do
9
+ it "should not raise exception" do
10
+ lambda { Dotanuki.guard do
11
+ Dotanuki.execute("ls /asd")
12
+ end }.should_not raise_error Dotanuki::ExecError
13
+ end
14
+
15
+ it "should raise exception when told to" do
16
+ lambda { Dotanuki.guard(:on_error => :exception) do
17
+ Dotanuki.execute("ls /asd")
18
+ end }.should raise_error Dotanuki::ExecError
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/spec/"
5
+ end
6
+
7
+ $:.unshift('./lib')
8
+ require 'dotanuki'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotanuki-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Englund
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: open4
16
+ requirement: &70340336526420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70340336526420
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70340336526000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70340336526000
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-rspec
38
+ requirement: &70340336525580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70340336525580
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &70340336525160 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70340336525160
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70340336524740 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70340336524740
69
+ description: A pure ruby command executioner which doesn't blindly stumble on when
70
+ a command fails'
71
+ email:
72
+ - martin@englund.nu
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .autotest
78
+ - .gitignore
79
+ - .rspec
80
+ - .travis.yml
81
+ - Gemfile
82
+ - Guardfile
83
+ - LICENSE
84
+ - README.textile
85
+ - Rakefile
86
+ - dotanuki-ruby.gemspec
87
+ - dotanuki.gemspec
88
+ - lib/dotanuki.rb
89
+ - lib/dotanuki/version.rb
90
+ - spec/dotanuki_spec.rb
91
+ - spec/module_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/pmenglund/dotanuki
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project: dotanuki
113
+ rubygems_version: 1.8.10
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Command executioner
117
+ test_files:
118
+ - spec/dotanuki_spec.rb
119
+ - spec/module_spec.rb
120
+ - spec/spec_helper.rb
121
+ has_rdoc: