cocaine 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of cocaine might be problematic. Click here for more details.

data/NEWS.md CHANGED
@@ -1,3 +1,18 @@
1
+ New for 0.4.1:
2
+
3
+ * Introduce FakeRunner for testing, so you don't really run commands.
4
+ * Fix logging: output the actual command, not the un-interpolated pattern.
5
+ * Prevent color codes from being output if log destination isn't a TTY.
6
+
7
+ New for 0.4.0:
8
+
9
+ * Moved interpolation to the `run` method, instead of interpolating on `new`.
10
+ * Remove official support for REE.
11
+
12
+ New for 0.3.2:
13
+
14
+ * Fix a hang when processes wait for IO.
15
+
1
16
  New for 0.3.1:
2
17
 
3
18
  * Made the `Runner` manually swappable, in case `ProcessRunner` doesn't work
@@ -29,6 +29,14 @@ module Cocaine
29
29
  @runner || best_runner
30
30
  end
31
31
 
32
+ def fake!
33
+ @runner = FakeRunner.new
34
+ end
35
+
36
+ def unfake!
37
+ @runner = nil
38
+ end
39
+
32
40
  private
33
41
 
34
42
  def best_runner
@@ -63,8 +71,9 @@ module Cocaine
63
71
  def run(interpolations = {})
64
72
  output = ''
65
73
  begin
66
- @logger.info("\e[32mCommand\e[0m :: #{command}") if @logger
67
- output = execute(command(interpolations))
74
+ full_command = command(interpolations)
75
+ log("#{colored("Command")} :: #{full_command}")
76
+ output = execute(full_command)
68
77
  rescue Errno::ENOENT
69
78
  raise Cocaine::CommandNotFoundError
70
79
  ensure
@@ -85,6 +94,20 @@ module Cocaine
85
94
 
86
95
  private
87
96
 
97
+ def colored(text, ansi_color = "\e[32m")
98
+ if @logger && @logger.tty?
99
+ "#{ansi_color}#{text}\e[0m"
100
+ else
101
+ text
102
+ end
103
+ end
104
+
105
+ def log(text)
106
+ if @logger
107
+ @logger.info(text)
108
+ end
109
+ end
110
+
88
111
  def execute(command)
89
112
  runner.call(command, environment)
90
113
  end
@@ -1,3 +1,4 @@
1
1
  require 'cocaine/command_line/runners/backticks_runner'
2
2
  require 'cocaine/command_line/runners/process_runner'
3
3
  require 'cocaine/command_line/runners/posix_runner'
4
+ require 'cocaine/command_line/runners/fake_runner'
@@ -0,0 +1,22 @@
1
+ module Cocaine
2
+ class CommandLine
3
+ class FakeRunner
4
+
5
+ attr_reader :commands
6
+
7
+ def initialize
8
+ @commands = []
9
+ end
10
+
11
+ def call(command, env = {})
12
+ commands << [command, env]
13
+ ""
14
+ end
15
+
16
+ def ran?(predicate_command)
17
+ @commands.any?{|(command, env)| command =~ Regexp.new(predicate_command) }
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module Cocaine
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.4.1".freeze
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cocaine::CommandLine::FakeRunner do
4
+ it 'records commands' do
5
+ subject.call("some command", :environment)
6
+ subject.call("other command", :other_environment)
7
+ subject.commands.should eq [["some command", :environment], ["other command", :other_environment]]
8
+ end
9
+
10
+ it 'can tell if a command was run' do
11
+ subject.call("some command", :environment)
12
+ subject.call("other command", :other_environment)
13
+ subject.ran?("some command").should be_true
14
+ subject.ran?("no command").should be_false
15
+ end
16
+
17
+ it 'can tell if a command was run even if shell options were set' do
18
+ subject.call("something 2>/dev/null", :environment)
19
+ subject.ran?("something").should be_true
20
+ end
21
+
22
+ end
@@ -173,18 +173,22 @@ describe Cocaine::CommandLine do
173
173
  Cocaine::CommandLine.new("convert").should_not be_unix
174
174
  end
175
175
 
176
+ it "colorizes the output to a tty" do
177
+ logger = FakeLogger.new(:tty => true)
178
+ Cocaine::CommandLine.new("echo", "'Logging!' :foo", :logger => logger).run(:foo => "bar")
179
+ logger.entries.should include("\e[32mCommand\e[0m :: echo 'Logging!' 'bar'")
180
+ end
181
+
176
182
  it "logs the command to a supplied logger" do
177
- logger = stub
178
- logger.stubs(:info).with(anything).returns(nil)
179
- Cocaine::CommandLine.new("echo", "'Logging!'", :logger => logger).run
180
- logger.should have_received(:info).with("\e[32mCommand\e[0m :: echo 'Logging!'")
183
+ logger = FakeLogger.new
184
+ Cocaine::CommandLine.new("echo", "'Logging!' :foo", :logger => logger).run(:foo => "bar")
185
+ logger.entries.should include("Command :: echo 'Logging!' 'bar'")
181
186
  end
182
187
 
183
188
  it "logs the command to a default logger" do
184
- Cocaine::CommandLine.logger = stub
185
- Cocaine::CommandLine.logger.stubs(:info).with(anything).returns(nil)
189
+ Cocaine::CommandLine.logger = FakeLogger.new
186
190
  Cocaine::CommandLine.new("echo", "'Logging!'").run
187
- Cocaine::CommandLine.logger.should have_received(:info).with("\e[32mCommand\e[0m :: echo 'Logging!'")
191
+ Cocaine::CommandLine.logger.entries.should include("Command :: echo 'Logging!'")
188
192
  end
189
193
 
190
194
  it "is fine if no logger is supplied" do
@@ -232,5 +236,27 @@ describe Cocaine::CommandLine do
232
236
  cmd.runner.class.should == Cocaine::CommandLine::BackticksRunner
233
237
  end
234
238
 
239
+ it "can go into 'Fake' mode" do
240
+ Cocaine::CommandLine.fake!
241
+
242
+ cmd = Cocaine::CommandLine.new("echo", "hello")
243
+ cmd.runner.class.should eq Cocaine::CommandLine::FakeRunner
244
+ end
245
+
246
+ it "can turn off Fake mode" do
247
+ Cocaine::CommandLine.fake!
248
+ Cocaine::CommandLine.unfake!
249
+
250
+ cmd = Cocaine::CommandLine.new("echo", "hello")
251
+ cmd.runner.class.should_not eq Cocaine::CommandLine::FakeRunner
252
+ end
253
+
254
+ it "can use a FakeRunner even if not in Fake mode" do
255
+ Cocaine::CommandLine.unfake!
256
+
257
+ cmd = Cocaine::CommandLine.new("echo", "hello", :runner => Cocaine::CommandLine::FakeRunner.new)
258
+ cmd.runner.class.should eq Cocaine::CommandLine::FakeRunner
259
+ end
260
+
235
261
  end
236
262
  end
@@ -0,0 +1,18 @@
1
+ class FakeLogger
2
+ def initialize(options = {})
3
+ @tty = options[:tty]
4
+ @entries = []
5
+ end
6
+
7
+ def info(text)
8
+ @entries << text
9
+ end
10
+
11
+ def entries
12
+ @entries
13
+ end
14
+
15
+ def tty?
16
+ @tty
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocaine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-05 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -110,15 +110,18 @@ files:
110
110
  - lib/cocaine/command_line.rb
111
111
  - lib/cocaine/command_line/runners.rb
112
112
  - lib/cocaine/command_line/runners/backticks_runner.rb
113
+ - lib/cocaine/command_line/runners/fake_runner.rb
113
114
  - lib/cocaine/command_line/runners/posix_runner.rb
114
115
  - lib/cocaine/command_line/runners/process_runner.rb
115
116
  - lib/cocaine/exceptions.rb
116
117
  - lib/cocaine/version.rb
117
118
  - spec/cocaine/command_line/runners/backticks_runner_spec.rb
119
+ - spec/cocaine/command_line/runners/fake_runner_spec.rb
118
120
  - spec/cocaine/command_line/runners/posix_runner_spec.rb
119
121
  - spec/cocaine/command_line/runners/process_runner_spec.rb
120
122
  - spec/cocaine/command_line_spec.rb
121
123
  - spec/spec_helper.rb
124
+ - spec/support/fake_logger.rb
122
125
  - spec/support/nonblocking_examples.rb
123
126
  - spec/support/stub_os.rb
124
127
  - spec/support/with_exitstatus.rb
@@ -148,10 +151,12 @@ specification_version: 3
148
151
  summary: A small library for doing (command) lines
149
152
  test_files:
150
153
  - spec/cocaine/command_line/runners/backticks_runner_spec.rb
154
+ - spec/cocaine/command_line/runners/fake_runner_spec.rb
151
155
  - spec/cocaine/command_line/runners/posix_runner_spec.rb
152
156
  - spec/cocaine/command_line/runners/process_runner_spec.rb
153
157
  - spec/cocaine/command_line_spec.rb
154
158
  - spec/spec_helper.rb
159
+ - spec/support/fake_logger.rb
155
160
  - spec/support/nonblocking_examples.rb
156
161
  - spec/support/stub_os.rb
157
162
  - spec/support/with_exitstatus.rb