RunIt 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile.lock +1 -1
  2. data/lib/RunIt.rb +100 -32
  3. data/spec/RunIt_spec.rb +19 -23
  4. metadata +21 -5
  5. checksums.yaml +0 -15
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- RunIt (0.1.0)
4
+ RunIt (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/RunIt.rb CHANGED
@@ -11,6 +11,14 @@
11
11
 
12
12
  A simple class to wrap Open3 to execute a command and return the result, stdout and stderr
13
13
 
14
+ == Synopsis
15
+
16
+ require 'RunIt'
17
+
18
+ runner = RunIt.new "/bin/ls"
19
+ runner.run
20
+ puts runner.output
21
+
14
22
  =end
15
23
 
16
24
  require 'open3'
@@ -18,53 +26,113 @@ require 'ostruct'
18
26
 
19
27
  class RunIt
20
28
 
21
- VERSION = "0.1.0"
29
+ # Version
30
+ VERSION = "1.0.0"
22
31
 
23
- attr_accessor :cmd, :input, :output, :error, :result
32
+ # cmd:: set/get the command to execute
33
+ attr_accessor :cmd
24
34
 
25
- def initialize(cmd, input=nil, output=nil, error=nil)
35
+ # input:: set/get the input for the command
36
+ attr_accessor :input
26
37
 
27
- self.cmd = cmd
28
- self.output = output ||= ''
29
- self.error = error ||= ''
38
+ # output:: get the output from the command
39
+ attr_reader :output
30
40
 
31
- # Input is a little more complicated
41
+ # error:: get the error messages from the command
42
+ attr_reader :error
32
43
 
33
- case input
34
- when String ; self.input = input
35
- when Array ; self.input = input.flatten.join("\n")
36
- when NilClass ; self.input = nil
37
- else self.input = input.to_s
38
- end
39
-
44
+ # result:: the result of the command as a Process::Status object
45
+ attr_reader :result
46
+
47
+
48
+ # Class function to tell if mocking is turned on
49
+ def self.mock?
50
+ @@mock
51
+ end
52
+
53
+ # Class method to turn on mocking
54
+ def self.mock!
55
+ @@mock = true
40
56
  end
41
57
 
58
+ # Class method to turn off mocking
59
+ def self.unmock!
60
+ @@mock = false
61
+ end
62
+
63
+ # Initialize the RunIt object
64
+ #
65
+ # cmd:: (required) [String] - the full command line to execute
66
+ # input:: (optional) [String] - input to the command, if any
67
+ def initialize(cmd, input=nil)
68
+ self.cmd = cmd
69
+ self.input = input
70
+ self.result = nil
71
+ @@mock = false
72
+ end
73
+
74
+ # Execute the specified command
75
+ #
76
+ # If mocking is on, only return the command as the output, and set success to true
42
77
  def run
43
78
 
44
- begin
79
+ if @@mock
45
80
 
46
- Open3.popen3(self.cmd) do |stdin, stdout, stderr, wait|
47
- stdin.puts self.input unless self.input.nil?
48
- stdin.close
49
- until stdout.eof?
50
- self.output << stdout.gets
51
- end
52
- until stderr.eof?
53
- self.error << stderr.gets
81
+ self.result = OpenStruct.new(:success? => true, :exitstatus => 0)
82
+ self.output = "Command entered: #{self.cmd}"
83
+ self.error = ''
84
+ return true
85
+
86
+ else
87
+
88
+ self.output = ''
89
+ self.error = ''
90
+ self.result = nil
91
+
92
+ begin
93
+
94
+ Open3.popen3(self.cmd) do |stdin, stdout, stderr, wait|
95
+ stdin.puts self.input unless self.input.nil?
96
+ stdin.close
97
+ until stdout.eof?
98
+ self.output << stdout.gets
99
+ end
100
+ until stderr.eof?
101
+ self.error << stderr.gets
102
+ end
103
+ self.result = wait.value
54
104
  end
55
- self.result = wait.value
56
- self.result.success?
57
- end
58
105
 
59
- rescue Exception => e
106
+ rescue Exception => e
107
+
108
+ self.result = OpenStruct.new(:success? => false, :exitstatus => -1)
109
+ self.error << "#{cmd} raised an error: #{e.class}:#{e}"
60
110
 
61
- self.result = OpenStruct.new(:success? => false, :exitstatus => -1)
62
- self.error << "#{cmd} raised an error: #{e.class}:#{e}"
63
- false
111
+ end
112
+
113
+ self.result.success?
64
114
 
65
115
  end
66
-
116
+
117
+ end
118
+
119
+ # Whether the command completed successfully
120
+ def success?
121
+ self.result.success?
122
+ end
123
+
124
+ # The exit status of the command
125
+ def exitstatus
126
+ self.result.exitstatus
127
+ end
128
+
129
+ # Instance method to tell if mocking is turned on
130
+ def mock?
131
+ @@mock
67
132
  end
68
133
 
134
+ private
135
+
136
+
69
137
  end
70
-
138
+
data/spec/RunIt_spec.rb CHANGED
@@ -5,7 +5,7 @@ describe RunIt do
5
5
 
6
6
  describe "/bin/ls" do
7
7
 
8
- it "should return the list of files in an array" do
8
+ it "should return the list of files" do
9
9
 
10
10
  runner = RunIt.new "/bin/ls"
11
11
 
@@ -13,7 +13,8 @@ describe RunIt do
13
13
  Dir.chdir(dir) do |dir|
14
14
  %w[a b c d].each {|fn| FileUtils.touch(fn)}
15
15
  runner.run.should be_true
16
- runner.result.success?.should be_true
16
+ runner.success?.should be_true
17
+ runner.exitstatus.should == 0
17
18
  runner.output.should == "a\nb\nc\nd\n"
18
19
  runner.error.should be_empty
19
20
  end
@@ -24,34 +25,18 @@ describe RunIt do
24
25
 
25
26
  describe "/bin/cat" do
26
27
 
27
- let(:input) { "Now is the winter of our discount tent." }
28
+ let(:input) { "Now is the winter of our discount tent.\n" }
28
29
  let(:runner) { RunIt.new "/bin/cat", input }
29
30
 
30
31
  it "should return the same text as input" do
31
32
  runner.run.should be_true
32
- runner.result.success?.should be_true
33
- runner.output.chomp.should == input
33
+ runner.success?.should be_true
34
+ runner.output.should == input
34
35
  runner.error.should be_empty
35
36
  end
36
37
 
37
38
  end
38
39
 
39
- describe "array return" do
40
-
41
- let(:input) {%w[Every good boy deserves favour]}
42
- let(:runner) {RunIt.new "/bin/cat", input, [], nil}
43
-
44
- it "should return the same text as input, but in an array" do
45
- runner.run.should be_true
46
- runner.result.success?.should be_true
47
- runner.output.should be_a(Array)
48
- runner.output.count.should == input.count
49
- runner.output.map(&:chomp).should == input
50
- runner.error.should be_empty
51
- end
52
-
53
- end
54
-
55
40
  describe "Bogus Command" do
56
41
 
57
42
  let(:input) {"Why did the chicken cross the road? "}
@@ -59,13 +44,24 @@ describe RunIt do
59
44
 
60
45
  it "should give a false result" do
61
46
  runner.run.should be_false
62
- runner.result.success?.should be_false
63
- runner.error.chomp.should_not be_empty
47
+ runner.success?.should be_false
48
+ runner.error.should_not be_empty
64
49
  puts "Runner.error: " + runner.error.inspect
65
50
  end
66
51
 
67
52
  end
68
53
 
54
+ describe "Test Mocking" do
55
+ it "Should not do anything except return the command given in the output" do
56
+ runner = RunIt.new "/bin/ls"
57
+ RunIt.mock!
58
+ runner.mock?.should be_true
59
+ runner.run.should be_true
60
+ runner.output.should == "Command entered: /bin/ls"
61
+ end
62
+
63
+
64
+ end
69
65
 
70
66
 
71
67
 
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RunIt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Tamara Temple
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-04-24 00:00:00.000000000 Z
12
+ date: 2013-05-02 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -27,6 +30,7 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rake
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
35
  - - ! '>='
32
36
  - !ruby/object:Gem::Version
@@ -34,6 +38,7 @@ dependencies:
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
43
  - - ! '>='
39
44
  - !ruby/object:Gem::Version
@@ -41,6 +46,7 @@ dependencies:
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
51
  - - ! '>='
46
52
  - !ruby/object:Gem::Version
@@ -48,6 +54,7 @@ dependencies:
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
59
  - - ! '>='
53
60
  - !ruby/object:Gem::Version
@@ -55,6 +62,7 @@ dependencies:
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: rdoc
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
67
  - - ! '>='
60
68
  - !ruby/object:Gem::Version
@@ -62,6 +70,7 @@ dependencies:
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
75
  - - ! '>='
67
76
  - !ruby/object:Gem::Version
@@ -88,26 +97,33 @@ files:
88
97
  homepage: https://github.com/tamouse/RunIt
89
98
  licenses:
90
99
  - MIT
91
- metadata: {}
92
100
  post_install_message:
93
101
  rdoc_options: []
94
102
  require_paths:
95
103
  - lib
96
104
  required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
97
106
  requirements:
98
107
  - - ! '>='
99
108
  - !ruby/object:Gem::Version
100
109
  version: '0'
110
+ segments:
111
+ - 0
112
+ hash: -2733030625963170989
101
113
  required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
102
115
  requirements:
103
116
  - - ! '>='
104
117
  - !ruby/object:Gem::Version
105
118
  version: '0'
119
+ segments:
120
+ - 0
121
+ hash: -2733030625963170989
106
122
  requirements: []
107
123
  rubyforge_project:
108
- rubygems_version: 2.0.0
124
+ rubygems_version: 1.8.25
109
125
  signing_key:
110
- specification_version: 4
126
+ specification_version: 3
111
127
  summary: A simple class to wrap Open3#popen3
112
128
  test_files:
113
129
  - spec/RunIt_spec.rb
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- N2JjMmEzZTk4NzdlM2M0Y2QwOTAzOTU3OTg5ZDljNzEyYzhiYzY2Mg==
5
- data.tar.gz: !binary |-
6
- M2QxNjU2ZmIwODNkYjgzYmE4ZjBhMDI4NTRlMWFiOTQ2NzY0MGZhOA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MTFlOTE5Y2YyZmU4OTgwY2YxMThhZTJkMTQ3NjA5OGU1ZTA5NTZhNDg0Mjg1
10
- NGJkMWUwYzQ3NmUyNWY3NTJhYjhhOTQ5NTVlZGY4Njg1NzkyMjY1ZDM4ZGY2
11
- YzZlYWE5MGU2YTM2ODBjMTRmYTExNjQwYTE3ZTE0MWQ2ZWY0NmY=
12
- data.tar.gz: !binary |-
13
- NDQxOGY1OTAyM2RhNTMxMjkzODc4NTM3M2Q4NTZiZjkxY2M2OTdhNTk3ZTZk
14
- MGRjYzIzMmQzMjRlMjUxNzNhMTBiNzJkNmNlZThiZGYwMmIwY2NmNzA5OTYy
15
- NDM5ZTFlMGIyODc0NmY3NWI1ODI1NGI2YWZjNzE5OWFmMzlhZmY=