cocaine 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54155863b7f03aa7f26da5dd746c959d9911d4a3
4
- data.tar.gz: f65332c0a145b19251e71e32e020e507d10151ea
3
+ metadata.gz: 750b34929d7740b3106549975bb0700c807130f1
4
+ data.tar.gz: a063b0294e34258351f5b1bfe792cec481004e5e
5
5
  SHA512:
6
- metadata.gz: 5787857e52b0a42f9ef27be222ca10a0c6ef6a21915dc6f9f75d56bc9bc6886dcfba7c110dc934a58c1d95252723a3199ceaad8422d63ff0bdc7a629f12eb717
7
- data.tar.gz: 5d380053dfc1b8774df7966fa2819498619b1e53898145c087d860e647d71be1e2c7cfd974a77cb1148c012fa5b77d5b72d51040ccd094124284cee9cc05e00b
6
+ metadata.gz: c83f8e5119643be599f791f60051a208cd07549a63088f3b6700949b834a79d8cc82285281f4a94aa71a232fd642f22c4a328b19cc7ac7444558247f54091fce
7
+ data.tar.gz: 1912dd9fe81df0f2ceb52378b0f95df326747ba1f6f831af9ae5a7c4f74aa83237e5ceddf40885b23de3eb9c2d61dc831cc8289cf2b8af0d562060502aef9dc3
@@ -2,8 +2,6 @@ rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
4
  - 2.1.0
5
+ - 2.1.1
6
+ - 2.1.2
5
7
  - jruby-19mode
6
-
7
- matrix:
8
- allow_failures:
9
- - rvm: rbx-19mode
data/NEWS.md CHANGED
@@ -1,3 +1,11 @@
1
+ New for 0.5.5:
2
+
3
+ * Bug Fix: Posix- and ProcessRunner respect paths *and* are thread safe!
4
+ * Bug Fix: `exitstatus` should always be set, even if command doesn't run.
5
+ * Test Fix: Do not try to test Runners if they don't run on this system.
6
+ * Improvement: Pass the Errno::ENOENT message through to the exception.
7
+ * Improvement: Improve documentation
8
+
1
9
  New for 0.5.4:
2
10
 
3
11
  * Bug Fix: PosixRunner and ProcessRunner respect supplemental paths now.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Cocaine [![Build Status](https://secure.travis-ci.org/thoughtbot/cocaine.png)](http://travis-ci.org/thoughtbot/cocaine)
1
+ # Cocaine [![Build Status](https://secure.travis-ci.org/thoughtbot/cocaine.png?branch=master)](http://travis-ci.org/thoughtbot/cocaine)
2
2
 
3
3
  A small library for doing (command) lines.
4
4
 
@@ -10,8 +10,8 @@ The basic, normal stuff:
10
10
 
11
11
  ```ruby
12
12
  line = Cocaine::CommandLine.new("echo", "hello 'world'")
13
- line.command # => "echo hello 'world'"
14
- line.run # => "hello world\n"
13
+ line.command # => "echo hello 'world'"
14
+ line.run # => "hello world\n"
15
15
  ```
16
16
 
17
17
  Interpolated arguments:
@@ -180,19 +180,13 @@ try to use `PopenRunner`:
180
180
  Cocaine::CommandLine.runner = Cocaine::CommandLine::PopenRunner.new
181
181
  ```
182
182
 
183
+ ## Thread Safety
183
184
 
184
- ## REE
185
-
186
- So, here's the thing about REE: The specs that involve timeouts don't work
187
- there. Not because the logic is unsound, but because the command runs really
188
- slowly. The test passes -- eventually. This was verified using an external
189
- debugger: the process that REE kicks off in the tests reads and writes
190
- surprisingly slowly. For this reason, we cannot recommend using Cocaine with
191
- REE anymore.
192
-
193
- It's not something we like, so if anyone has any insight into this problem,
194
- we'd love to hear about it. But, for the time being, we'll consider it more
195
- appropriate to just not use it anymore. Upgrade to 1.9.3, people.
185
+ Cocaine should be thread safe. As discussed [here, in this climate_control
186
+ thread](https://github.com/thoughtbot/climate_control/pull/11), climate_control,
187
+ which modifies the environment under which commands are run for the
188
+ BackticksRunner and PopenRunner, is thread-safe but not reentrant. Please let us
189
+ know if you find this is ever not the case.
196
190
 
197
191
  ## Feedback
198
192
 
@@ -1,6 +1,7 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  require 'rbconfig'
4
+ require 'cocaine/os_detector'
4
5
  require 'cocaine/command_line'
5
6
  require 'cocaine/command_line/runners'
6
7
  require 'cocaine/exceptions'
@@ -10,9 +10,9 @@ module Cocaine
10
10
  end
11
11
 
12
12
  def path=(supplemental_path)
13
- @supplemental_path = supplemental_path
14
- @supplemental_environment ||= {}
15
- @supplemental_environment['PATH'] = (Array(supplemental_path) + [ENV['PATH']]).join(File::PATH_SEPARATOR)
13
+ @supplemental_path = Array(supplemental_path).
14
+ flatten.
15
+ join(OS.path_separator)
16
16
  end
17
17
 
18
18
  def environment
@@ -35,10 +35,6 @@ module Cocaine
35
35
  @runner = nil
36
36
  end
37
37
 
38
- def java?
39
- RUBY_PLATFORM =~ /java/
40
- end
41
-
42
38
  private
43
39
 
44
40
  def best_runner
@@ -65,7 +61,7 @@ module Cocaine
65
61
  end
66
62
 
67
63
  def command(interpolations = {})
68
- cmd = [@binary, interpolate(@params, interpolations)]
64
+ cmd = [path_prefix, @binary, interpolate(@params, interpolations)]
69
65
  cmd << bit_bucket if @swallow_stderr
70
66
  cmd.join(" ").strip
71
67
  end
@@ -80,7 +76,7 @@ module Cocaine
80
76
  rescue Errno::ENOENT => e
81
77
  raise Cocaine::CommandNotFoundError, e.message
82
78
  ensure
83
- @exit_status = $?.exitstatus if $?.respond_to?(:exitstatus)
79
+ @exit_status = $?.respond_to?(:exitstatus) ? $?.exitstatus : 0
84
80
  end
85
81
 
86
82
  if @exit_status == 127
@@ -98,10 +94,6 @@ module Cocaine
98
94
  output
99
95
  end
100
96
 
101
- def unix?
102
- RbConfig::CONFIG['host_os'] !~ /mswin|mingw/
103
- end
104
-
105
97
  private
106
98
 
107
99
  def colored(text, ansi_color = "\e[32m")
@@ -118,6 +110,28 @@ module Cocaine
118
110
  end
119
111
  end
120
112
 
113
+ def path_prefix
114
+ if !self.class.path.nil? && !self.class.path.empty?
115
+ os_path_prefix
116
+ end
117
+ end
118
+
119
+ def os_path_prefix
120
+ if OS.unix?
121
+ unix_path_prefix
122
+ else
123
+ windows_path_prefix
124
+ end
125
+ end
126
+
127
+ def unix_path_prefix
128
+ "PATH=#{self.class.path}#{OS.path_separator}$PATH"
129
+ end
130
+
131
+ def windows_path_prefix
132
+ "SET PATH=#{self.class.path}#{OS.path_separator}%PATH% &"
133
+ end
134
+
121
135
  def execute(command)
122
136
  runner.call(command, environment, runner_options)
123
137
  end
@@ -152,7 +166,7 @@ module Cocaine
152
166
 
153
167
  def shell_quote(string)
154
168
  return "" if string.nil?
155
- if unix?
169
+ if OS.unix?
156
170
  if string.empty?
157
171
  "''"
158
172
  else
@@ -164,7 +178,7 @@ module Cocaine
164
178
  end
165
179
 
166
180
  def bit_bucket
167
- unix? ? "2>/dev/null" : "2>NUL"
181
+ OS.unix? ? "2>/dev/null" : "2>NUL"
168
182
  end
169
183
  end
170
184
  end
@@ -13,7 +13,7 @@ module Cocaine
13
13
 
14
14
  def call(command, env = {}, options = {})
15
15
  with_modified_environment(env) do
16
- IO.popen(command, "r", options) do |pipe|
16
+ IO.popen(env_command(command), "r", options) do |pipe|
17
17
  pipe.read
18
18
  end
19
19
  end
@@ -21,6 +21,26 @@ module Cocaine
21
21
 
22
22
  private
23
23
 
24
+ def env_command(command)
25
+ windows_command(command) || java_command(command) || default_command(command)
26
+ end
27
+
28
+ def windows_command(command)
29
+ if OS.windows?
30
+ command
31
+ end
32
+ end
33
+
34
+ def java_command(command)
35
+ if OS.java?
36
+ "env #{command}"
37
+ end
38
+ end
39
+
40
+ def default_command(command)
41
+ command
42
+ end
43
+
24
44
  def with_modified_environment(env, &block)
25
45
  ClimateControl.modify(env, &block)
26
46
  end
@@ -11,7 +11,7 @@ module Cocaine
11
11
  end
12
12
 
13
13
  def self.supported?
14
- available? && !Cocaine::CommandLine.java?
14
+ available? && !OS.java?
15
15
  end
16
16
 
17
17
  def supported?
@@ -21,17 +21,15 @@ module Cocaine
21
21
  def call(command, env = {}, options = {})
22
22
  input, output = IO.pipe
23
23
  options[:out] = output
24
- with_modified_environment(env) do
25
- pid = spawn(env, command, options)
26
- output.close
27
- result = ""
28
- while partial_result = input.read(8192)
29
- result << partial_result
30
- end
31
- waitpid(pid)
32
- input.close
33
- result
24
+ pid = spawn(env, command, options)
25
+ output.close
26
+ result = ""
27
+ while partial_result = input.read(8192)
28
+ result << partial_result
34
29
  end
30
+ waitpid(pid)
31
+ input.close
32
+ result
35
33
  end
36
34
 
37
35
  private
@@ -44,10 +42,6 @@ module Cocaine
44
42
  Process.waitpid(pid)
45
43
  end
46
44
 
47
- def with_modified_environment(env, &block)
48
- ClimateControl.modify(env, &block)
49
- end
50
-
51
45
  end
52
46
  end
53
47
  end
@@ -8,7 +8,7 @@ module Cocaine
8
8
  end
9
9
 
10
10
  def self.supported?
11
- available? && !Cocaine::CommandLine.java?
11
+ available? && !OS.java?
12
12
  end
13
13
 
14
14
  def supported?
@@ -18,14 +18,12 @@ module Cocaine
18
18
  def call(command, env = {}, options = {})
19
19
  input, output = IO.pipe
20
20
  options[:out] = output
21
- with_modified_environment(env) do
22
- pid = spawn(env, command, options)
23
- output.close
24
- result = input.read
25
- waitpid(pid)
26
- input.close
27
- result
28
- end
21
+ pid = spawn(env, command, options)
22
+ output.close
23
+ result = input.read
24
+ waitpid(pid)
25
+ input.close
26
+ result
29
27
  end
30
28
 
31
29
  private
@@ -40,10 +38,6 @@ module Cocaine
40
38
  # In JRuby, waiting on a finished pid raises.
41
39
  end
42
40
 
43
- def with_modified_environment(env, &block)
44
- ClimateControl.modify(env, &block)
45
- end
46
-
47
41
  end
48
42
  end
49
43
  end
@@ -0,0 +1,27 @@
1
+ # coding: UTF-8
2
+
3
+ module Cocaine
4
+ class OSDetector
5
+ def java?
6
+ arch =~ /java/
7
+ end
8
+
9
+ def unix?
10
+ RbConfig::CONFIG['host_os'] !~ /mswin|mingw/
11
+ end
12
+
13
+ def windows?
14
+ !unix?
15
+ end
16
+
17
+ def path_separator
18
+ File::PATH_SEPARATOR
19
+ end
20
+
21
+ def arch
22
+ RUBY_PLATFORM
23
+ end
24
+ end
25
+
26
+ OS = OSDetector.new
27
+ end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module Cocaine
4
- VERSION = "0.5.4".freeze
4
+ VERSION = "0.5.5".freeze
5
5
  end
@@ -10,13 +10,13 @@ describe Cocaine::CommandLine::FakeRunner do
10
10
  it 'can tell if a command was run' do
11
11
  subject.call("some command", :environment)
12
12
  subject.call("other command", :other_environment)
13
- subject.ran?("some command").should be_true
14
- subject.ran?("no command").should be_false
13
+ subject.ran?("some command").should eq true
14
+ subject.ran?("no command").should eq false
15
15
  end
16
16
 
17
17
  it 'can tell if a command was run even if shell options were set' do
18
18
  subject.call("something 2>/dev/null", :environment)
19
- subject.ran?("something").should be_true
19
+ subject.ran?("something").should eq true
20
20
  end
21
21
 
22
22
  end
@@ -11,18 +11,17 @@ describe Cocaine::CommandLine do
11
11
  cmd.command.should == "convert a.jpg b.png"
12
12
  end
13
13
 
14
- it "specifies the $PATH where the command can be found" do
15
- saved_path = ENV['PATH']
16
- begin
17
- ENV['PATH'] = "/the/environment/path:/other/bin"
18
- Cocaine::CommandLine.path = "/path/to/command/dir"
19
- cmd = Cocaine::CommandLine.new("echo", "$PATH")
20
- cmd.command.should == "echo $PATH"
21
- output = cmd.run
22
- output.should match(%r{/path/to/command/dir:/the/environment/path:/other/bin})
23
- ensure
24
- ENV['PATH'] = saved_path
25
- end
14
+ it "specifies the $PATH where the command can be found on unix" do
15
+ Cocaine::CommandLine.path = ["/path/to/command/dir", "/"]
16
+ cmd = Cocaine::CommandLine.new("ls")
17
+ cmd.command.should == "PATH=/path/to/command/dir:/:$PATH ls"
18
+ end
19
+
20
+ it "specifies the %PATH% where the command can be found on windows" do
21
+ on_windows!
22
+ Cocaine::CommandLine.path = ['C:\system32', 'D:\\']
23
+ cmd = Cocaine::CommandLine.new("dir")
24
+ cmd.command.should == 'SET PATH=C:\system32;D:\;%PATH% & dir'
26
25
  end
27
26
 
28
27
  it "specifies more than one path where the command can be found" do
@@ -152,20 +151,6 @@ describe Cocaine::CommandLine do
152
151
  end
153
152
  end
154
153
 
155
- it "detects that the system is unix" do
156
- Cocaine::CommandLine.new("convert").should be_unix
157
- end
158
-
159
- it "detects that the system is windows" do
160
- on_windows!
161
- Cocaine::CommandLine.new("convert").should_not be_unix
162
- end
163
-
164
- it "detects that the system is windows (mingw)" do
165
- on_mingw!
166
- Cocaine::CommandLine.new("convert").should_not be_unix
167
- end
168
-
169
154
  it "colorizes the output to a tty" do
170
155
  logger = FakeLogger.new(:tty => true)
171
156
  Cocaine::CommandLine.new("echo", "'Logging!' :foo", :logger => logger).run(:foo => "bar")
@@ -64,6 +64,6 @@ describe "When an error happens" do
64
64
  command.stubs(:command).raises("An Error")
65
65
 
66
66
  lambda{ command.run }.should raise_error("An Error")
67
- command.exit_status.should be_nil
67
+ command.exit_status.should eq 0
68
68
  end
69
69
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cocaine::OSDetector do
4
+ it "detects that the system is unix" do
5
+ on_unix!
6
+ Cocaine::OS.should be_unix
7
+ end
8
+
9
+ it "detects that the system is windows" do
10
+ on_windows!
11
+ Cocaine::OS.should be_windows
12
+ end
13
+
14
+ it "detects that the system is windows (mingw)" do
15
+ on_mingw!
16
+ Cocaine::OS.should be_windows
17
+ end
18
+
19
+ it "detects that the current Ruby is on Java" do
20
+ on_java!
21
+ Cocaine::OS.should be_java
22
+ end
23
+ end
@@ -72,7 +72,7 @@ describe 'When running an executable in the supplemental path' do
72
72
  end
73
73
 
74
74
  after do
75
- FileUtils.rm_f(Cocaine::CommandLine.path + 'ls')
75
+ FileUtils.rm_f("#{Cocaine::CommandLine.path}/ls")
76
76
  end
77
77
 
78
78
  [
@@ -81,12 +81,14 @@ describe 'When running an executable in the supplemental path' do
81
81
  Cocaine::CommandLine::PosixRunner,
82
82
  Cocaine::CommandLine::ProcessRunner
83
83
  ].each do |runner_class|
84
- describe runner_class do
85
- describe '#run' do
86
- it 'finds the correct executable' do
87
- Cocaine::CommandLine.runner = runner_class.new
88
- result = Cocaine::CommandLine.new('ls').run
89
- expect(result.strip).to eq('overridden-ls')
84
+ if runner_class.supported?
85
+ describe runner_class do
86
+ describe '#run' do
87
+ it 'finds the correct executable' do
88
+ Cocaine::CommandLine.runner = runner_class.new
89
+ result = Cocaine::CommandLine.new('ls').run
90
+ expect(result.strip).to eq('overridden-ls')
91
+ end
90
92
  end
91
93
  end
92
94
  end
@@ -5,9 +5,11 @@ require 'cocaine'
5
5
  require 'timeout'
6
6
  require 'tempfile'
7
7
  require 'pry'
8
- require 'active_support/buffered_logger'
9
8
  require 'thread'
10
9
 
10
+ begin; require 'active_support/logger'; rescue LoadError; end
11
+ begin; require 'active_support/buffered_logger'; rescue LoadError; end
12
+
11
13
  Dir[File.dirname(__FILE__) + "/support/**.rb"].each{|support_file| require support_file }
12
14
 
13
15
  RSpec.configure do |config|
@@ -1,14 +1,21 @@
1
1
  module StubOS
2
2
  def on_windows!
3
3
  stub_os('mswin')
4
+ Cocaine::OS.stubs(:path_separator).returns(";")
4
5
  end
5
6
 
6
7
  def on_unix!
7
8
  stub_os('darwin11.0.0')
9
+ Cocaine::OS.stubs(:path_separator).returns(":")
8
10
  end
9
11
 
10
12
  def on_mingw!
11
13
  stub_os('mingw')
14
+ Cocaine::OS.stubs(:path_separator).returns(";")
15
+ end
16
+
17
+ def on_java!
18
+ Cocaine::OS.stubs(:arch).returns("universal-java1.7")
12
19
  end
13
20
 
14
21
  def stub_os(host_string)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocaine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Yurek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-28 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: climate_control
@@ -144,6 +144,7 @@ files:
144
144
  - lib/cocaine/command_line/runners/posix_runner.rb
145
145
  - lib/cocaine/command_line/runners/process_runner.rb
146
146
  - lib/cocaine/exceptions.rb
147
+ - lib/cocaine/os_detector.rb
147
148
  - lib/cocaine/version.rb
148
149
  - spec/cocaine/command_line/runners/backticks_runner_spec.rb
149
150
  - spec/cocaine/command_line/runners/fake_runner_spec.rb
@@ -152,6 +153,7 @@ files:
152
153
  - spec/cocaine/command_line/runners/process_runner_spec.rb
153
154
  - spec/cocaine/command_line_spec.rb
154
155
  - spec/cocaine/errors_spec.rb
156
+ - spec/cocaine/os_detector_spec.rb
155
157
  - spec/cocaine/runners_spec.rb
156
158
  - spec/spec_helper.rb
157
159
  - spec/support/fake_logger.rb
@@ -191,6 +193,7 @@ test_files:
191
193
  - spec/cocaine/command_line/runners/process_runner_spec.rb
192
194
  - spec/cocaine/command_line_spec.rb
193
195
  - spec/cocaine/errors_spec.rb
196
+ - spec/cocaine/os_detector_spec.rb
194
197
  - spec/cocaine/runners_spec.rb
195
198
  - spec/spec_helper.rb
196
199
  - spec/support/fake_logger.rb