cocaine 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -75,7 +75,17 @@ But don't fear, you can specify where to look for the command.
75
75
  ```ruby
76
76
  Cocaine::CommandLine.path = "/opt/bin"
77
77
  line = Cocaine::CommandLine.new("lolwut")
78
- line.command # => "/opt/bin/lolwut"
78
+ line.command # => "lolwut", but it looks in /opt/bin for it.
79
+ ```
80
+
81
+ You can even give it a bunch of places to look.
82
+
83
+ ```ruby
84
+ FileUtils.rm("/opt/bin/lolwut")
85
+ `echo 'echo Hello' > /usr/local/bin/lolwut`
86
+ Cocaine::CommandLine.path = ["/opt/bin", "/usr/local/bin"]
87
+ line = Cocaine::CommandLine.new("lolwut")
88
+ line.run # => prints 'Hello', because it searches the path
79
89
  ```
80
90
 
81
91
  Or, just, you know, put it in the command.
@@ -96,6 +106,20 @@ rescue Cocaine::ExitStatusError => e
96
106
  end
97
107
  ```
98
108
 
109
+ You can see what's getting run. The 'Command' part it logs is in green for visibility!
110
+
111
+ ```ruby
112
+ line = Cocaine::CommandLine.new("echo", ":var", :var => "LOL!", :logger => Logger.new(STDOUT))
113
+ line.run # => Logs this with #info -> Command :: echo 'LOL!'
114
+ ```
115
+
116
+ But you don't have to, as you saw above where it doesn't use this. But you CAN log every command!
117
+
118
+ ```ruby
119
+ Cocaine::CommandLine.logger = Logger.new(STDOUT)
120
+ Cocaine::CommandLine.new("date").run # => Logs this -> Command :: date
121
+ ```
122
+
99
123
  ## License
100
124
 
101
125
  Copyright 2011 Jon Yurek and thoughtbot, inc. This is free software, and may be redistributed under the terms specified in the LICENSE file.
@@ -1,3 +1,4 @@
1
+ require 'rbconfig'
1
2
  require 'cocaine/command_line'
2
3
  require 'cocaine/exceptions'
3
4
 
@@ -1,13 +1,14 @@
1
1
  module Cocaine
2
2
  class CommandLine
3
3
  class << self
4
- attr_accessor :path
4
+ attr_accessor :path, :logger
5
5
  end
6
6
 
7
7
  def initialize(binary, params = "", options = {})
8
8
  @binary = binary.dup
9
9
  @params = params.dup
10
10
  @options = options.dup
11
+ @logger = @options.delete(:logger) || self.class.logger
11
12
  @swallow_stderr = @options.delete(:swallow_stderr)
12
13
  @expected_outcodes = @options.delete(:expected_outcodes)
13
14
  @expected_outcodes ||= [0]
@@ -25,6 +26,7 @@ module Cocaine
25
26
  output = ''
26
27
  begin
27
28
  with_modified_path do
29
+ @logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
28
30
  output = self.class.send(:'`', command)
29
31
  end
30
32
  rescue Errno::ENOENT
@@ -66,7 +68,7 @@ module Cocaine
66
68
  end
67
69
 
68
70
  def invalid_variables
69
- %w(expected_outcodes swallow_stderr)
71
+ %w(expected_outcodes swallow_stderr logger)
70
72
  end
71
73
 
72
74
  def interpolation(vars, key)
@@ -89,7 +91,7 @@ module Cocaine
89
91
  end
90
92
 
91
93
  def self.unix?
92
- File.exist?("/dev/null")
94
+ (Config::CONFIG['host_os'] =~ /mswin|mingw/).nil?
93
95
  end
94
96
  end
95
97
  end
@@ -1,3 +1,3 @@
1
1
  module Cocaine
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,15 +3,15 @@ require 'spec_helper'
3
3
  describe Cocaine::CommandLine do
4
4
  before do
5
5
  Cocaine::CommandLine.path = nil
6
- File.stubs(:exist?).with("/dev/null").returns(true)
6
+ on_unix! # Assume we're on unix unless otherwise specified.
7
7
  end
8
8
 
9
- it "takes a command and parameters and produce a shell command for bash" do
9
+ it "takes a command and parameters and produces a Bash command line" do
10
10
  cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
11
11
  cmd.command.should == "convert a.jpg b.png"
12
12
  end
13
13
 
14
- it "specifies the path where the command should be run" do
14
+ it "specifies the $PATH where the command can be found" do
15
15
  Cocaine::CommandLine.path = "/path/to/command/dir"
16
16
  cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
17
17
  cmd.command.should == "ruby -e 'puts ENV[%{PATH}]'"
@@ -19,7 +19,7 @@ describe Cocaine::CommandLine do
19
19
  output.should match(%r{/path/to/command/dir})
20
20
  end
21
21
 
22
- it "specifies more than one path where the command should be run" do
22
+ it "specifies more than one path where the command can be found" do
23
23
  Cocaine::CommandLine.path = ["/path/to/command/dir", "/some/other/path"]
24
24
  cmd = Cocaine::CommandLine.new("ruby", "-e 'puts ENV[%{PATH}]'")
25
25
  output = cmd.run
@@ -27,41 +27,41 @@ describe Cocaine::CommandLine do
27
27
  output.should match(%r{/some/other/path})
28
28
  end
29
29
 
30
- it "can interpolate quoted variables into the parameters" do
30
+ it "can interpolate quoted variables into the command line's parameters" do
31
31
  cmd = Cocaine::CommandLine.new("convert",
32
- ":one :{two}",
33
- :one => "a.jpg",
34
- :two => "b.png",
35
- :swallow_stderr => false)
32
+ ":one :{two}",
33
+ :one => "a.jpg",
34
+ :two => "b.png",
35
+ :swallow_stderr => false)
36
36
  cmd.command.should == "convert 'a.jpg' 'b.png'"
37
37
  end
38
38
 
39
39
  it "quotes command line options differently if we're on windows" do
40
- File.stubs(:exist?).with("/dev/null").returns(false)
40
+ on_windows!
41
41
  cmd = Cocaine::CommandLine.new("convert",
42
- ":one :{two}",
43
- :one => "a.jpg",
44
- :two => "b.png",
45
- :swallow_stderr => false)
42
+ ":one :{two}",
43
+ :one => "a.jpg",
44
+ :two => "b.png",
45
+ :swallow_stderr => false)
46
46
  cmd.command.should == 'convert "a.jpg" "b.png"'
47
47
  end
48
48
 
49
49
  it "can quote and interpolate dangerous variables" do
50
50
  cmd = Cocaine::CommandLine.new("convert",
51
- ":one :two",
52
- :one => "`rm -rf`.jpg",
53
- :two => "ha'ha.png",
54
- :swallow_stderr => false)
51
+ ":one :two",
52
+ :one => "`rm -rf`.jpg",
53
+ :two => "ha'ha.png",
54
+ :swallow_stderr => false)
55
55
  cmd.command.should == "convert '`rm -rf`.jpg' 'ha'\\''ha.png'"
56
56
  end
57
57
 
58
58
  it "can quote and interpolate dangerous variables even on windows" do
59
- File.stubs(:exist?).with("/dev/null").returns(false)
59
+ on_windows!
60
60
  cmd = Cocaine::CommandLine.new("convert",
61
- ":one :two",
62
- :one => "`rm -rf`.jpg",
63
- :two => "ha'ha.png",
64
- :swallow_stderr => false)
61
+ ":one :two",
62
+ :one => "`rm -rf`.jpg",
63
+ :two => "ha'ha.png",
64
+ :swallow_stderr => false)
65
65
  cmd.command.should == %{convert "`rm -rf`.jpg" "ha'ha.png"}
66
66
  end
67
67
 
@@ -70,36 +70,39 @@ describe Cocaine::CommandLine do
70
70
  cmd.command.should == "convert 'a.jpg' xc:black 'b.jpg'"
71
71
  end
72
72
 
73
- it "adds redirection to get rid of stderr in bash" do
74
- File.stubs(:exist?).with("/dev/null").returns(true)
73
+ it "can redirect stderr to the bit bucket if requested" do
75
74
  cmd = Cocaine::CommandLine.new("convert",
76
- "a.jpg b.png",
77
- :swallow_stderr => true)
75
+ "a.jpg b.png",
76
+ :swallow_stderr => true)
78
77
 
79
78
  cmd.command.should == "convert a.jpg b.png 2>/dev/null"
80
79
  end
81
80
 
82
- it "adds redirection to get rid of stderr in cmd.exe" do
83
- File.stubs(:exist?).with("/dev/null").returns(false)
81
+ it "can redirect stderr to the bit bucket on windows" do
82
+ on_windows!
84
83
  cmd = Cocaine::CommandLine.new("convert",
85
- "a.jpg b.png",
86
- :swallow_stderr => true)
84
+ "a.jpg b.png",
85
+ :swallow_stderr => true)
87
86
 
88
87
  cmd.command.should == "convert a.jpg b.png 2>NUL"
89
88
  end
90
89
 
91
- it "raises if trying to interpolate :swallow_stderr or :expected_outcodes" do
92
- cmd = Cocaine::CommandLine.new("convert",
93
- ":swallow_stderr :expected_outcodes",
94
- :swallow_stderr => false,
95
- :expected_outcodes => [0, 1])
90
+ it "raises if trying to interpolate :swallow_stderr" do
91
+ cmd = Cocaine::CommandLine.new("convert", ":swallow_stderr", :swallow_stderr => false)
92
+ lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
93
+ end
94
+
95
+ it "raises if trying to interpolate :expected_outcodes" do
96
+ cmd = Cocaine::CommandLine.new("convert", ":expected_outcodes", :expected_outcodes => [0])
97
+ lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
98
+ end
96
99
 
97
- lambda do
98
- cmd.command
99
- end.should raise_error(Cocaine::CommandLineError)
100
+ it "raises if trying to interpolate :logger" do
101
+ cmd = Cocaine::CommandLine.new("convert", ":logger", :logger => stub)
102
+ lambda { cmd.command }.should raise_error(Cocaine::CommandLineError)
100
103
  end
101
104
 
102
- it "runs the #command it's given and return the output" do
105
+ it "runs the command it's given and return the output" do
103
106
  cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
104
107
  cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
105
108
  with_exitstatus_returning(0) do
@@ -107,7 +110,7 @@ describe Cocaine::CommandLine do
107
110
  end
108
111
  end
109
112
 
110
- it "raises a CommandLineError if the result code isn't expected" do
113
+ it "raises a CommandLineError if the result code from the command isn't expected" do
111
114
  cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
112
115
  cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
113
116
  with_exitstatus_returning(1) do
@@ -117,11 +120,11 @@ describe Cocaine::CommandLine do
117
120
  end
118
121
  end
119
122
 
120
- it "does not raise a CommandLineError if the result code is expected" do
123
+ it "does not raise if the result code is expected, even if nonzero" do
121
124
  cmd = Cocaine::CommandLine.new("convert",
122
- "a.jpg b.png",
123
- :expected_outcodes => [0, 1],
124
- :swallow_stderr => false)
125
+ "a.jpg b.png",
126
+ :expected_outcodes => [0, 1],
127
+ :swallow_stderr => false)
125
128
  cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
126
129
  with_exitstatus_returning(1) do
127
130
  lambda do
@@ -130,13 +133,37 @@ describe Cocaine::CommandLine do
130
133
  end
131
134
  end
132
135
 
133
- it "detects that the system is unix or windows based on presence of /dev/null" do
134
- File.stubs(:exist?).returns(true)
135
- Cocaine::CommandLine.unix?.should == true
136
+ it "detects that the system is unix" do
137
+ Cocaine::CommandLine.unix?.should be_true
138
+ end
139
+
140
+ it "detects that the system is windows" do
141
+ on_windows!
142
+ Cocaine::CommandLine.unix?.should be_false
143
+ end
144
+
145
+ it "detects that the system is windows (mingw)" do
146
+ on_windows!("mingw")
147
+ Cocaine::CommandLine.unix?.should be_false
148
+ end
149
+
150
+ it "logs the command to a supplied logger" do
151
+ logger = stub
152
+ logger.stubs(:info).with(anything).returns(nil)
153
+ Cocaine::CommandLine.new("echo", "'Logging!'", :logger => logger).run
154
+ logger.should have_received(:info).with("\e[32mCommand\e[0m :: echo 'Logging!'")
155
+ end
156
+
157
+ it "logs the command to a default logger" do
158
+ Cocaine::CommandLine.logger = stub
159
+ Cocaine::CommandLine.logger.stubs(:info).with(anything).returns(nil)
160
+ Cocaine::CommandLine.new("echo", "'Logging!'").run
161
+ Cocaine::CommandLine.logger.should have_received(:info).with("\e[32mCommand\e[0m :: echo 'Logging!'")
136
162
  end
137
163
 
138
- it "detects that the system is not unix or windows based on absence of /dev/null" do
139
- File.stubs(:exist?).returns(false)
140
- Cocaine::CommandLine.unix?.should_not == true
164
+ it "is fine if no logger is supplied" do
165
+ Cocaine::CommandLine.logger = nil
166
+ cmd = Cocaine::CommandLine.new("echo", "'Logging!'", :logger => nil)
167
+ lambda { cmd.run }.should_not raise_error
141
168
  end
142
169
  end
@@ -1,5 +1,6 @@
1
1
  require 'rspec'
2
2
  require './spec/support/with_exitstatus'
3
+ require './spec/support/stub_os'
3
4
  require 'mocha'
4
5
  require 'bourne'
5
6
  require 'cocaine'
@@ -7,4 +8,5 @@ require 'cocaine'
7
8
  RSpec.configure do |config|
8
9
  config.mock_with :mocha
9
10
  config.include WithExitstatus
11
+ config.include StubOS
10
12
  end
@@ -0,0 +1,14 @@
1
+ module StubOS
2
+ def on_windows!(host_string = 'mswin')
3
+ stub_os(host_string)
4
+ end
5
+
6
+ def on_unix!(host_string = 'darwin11.0.0')
7
+ stub_os(host_string)
8
+ end
9
+
10
+ def stub_os(host_string)
11
+ # http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
12
+ Config::CONFIG.stubs(:[]).with('host_os').returns(host_string)
13
+ end
14
+ end
@@ -2,10 +2,10 @@ module WithExitstatus
2
2
  def with_exitstatus_returning(code)
3
3
  saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
4
4
  begin
5
- `ruby -e 'exit #{code.to_i}'`
5
+ `ruby -e "exit #{code.to_i}"`
6
6
  yield
7
7
  ensure
8
- `ruby -e 'exit #{saved_exitstatus.to_i}'`
8
+ `ruby -e "exit #{saved_exitstatus.to_i}"`
9
9
  end
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocaine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jon Yurek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-05 00:00:00 -04:00
18
+ date: 2011-08-17 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -72,12 +72,13 @@ files:
72
72
  - README.md
73
73
  - LICENSE
74
74
  - Rakefile
75
- - lib/cocaine.rb
76
75
  - lib/cocaine/command_line.rb
77
76
  - lib/cocaine/exceptions.rb
78
77
  - lib/cocaine/version.rb
79
- - spec/spec_helper.rb
78
+ - lib/cocaine.rb
80
79
  - spec/cocaine/command_line_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/support/stub_os.rb
81
82
  - spec/support/with_exitstatus.rb
82
83
  has_rdoc: true
83
84
  homepage: http://www.thoughtbot.com/projects/cocaine
@@ -109,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  requirements: []
110
111
 
111
112
  rubyforge_project:
112
- rubygems_version: 1.5.2
113
+ rubygems_version: 1.6.2
113
114
  signing_key:
114
115
  specification_version: 3
115
116
  summary: A small library for doing (command) lines