Permalink
Browse files

Initial commit, import from Paperclip and convert to rspec

  • Loading branch information...
0 parents commit 069af0cf77653b8ecdca0f17c194a7f1326b51c1 @jyurek jyurek committed May 18, 2011
6 Gemfile
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+group :development do
+ gem 'rspec'
+ gem 'mocha'
+ gem 'bourne'
+end
25 Gemfile.lock
@@ -0,0 +1,25 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ bourne (1.0)
+ mocha (= 0.9.8)
+ diff-lcs (1.1.2)
+ mocha (0.9.8)
+ rake
+ rake (0.8.7)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.0)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bourne
+ mocha
+ rspec
5 lib/cocaine.rb
@@ -0,0 +1,5 @@
+require 'cocaine/command_line'
+require 'cocaine/exceptions'
+
+module Cocaine
+end
89 lib/cocaine.rbc
@@ -0,0 +1,89 @@
+!RBIX
+17831730954501249321
+x
+M
+1
+n
+n
+x
+10
+__script__
+i
+30
+5
+7
+0
+64
+47
+49
+1
+1
+15
+5
+7
+2
+64
+47
+49
+1
+1
+15
+99
+7
+3
+65
+49
+4
+2
+15
+1
+15
+2
+11
+I
+3
+I
+0
+I
+0
+I
+0
+n
+p
+5
+s
+20
+cocaine/command_line
+x
+7
+require
+s
+18
+cocaine/exceptions
+x
+7
+Cocaine
+x
+11
+open_module
+p
+7
+I
+0
+I
+1
+I
+9
+I
+2
+I
+12
+I
+4
+I
+1e
+x
+48
+/Users/jyurek/Development/cocaine/lib/cocaine.rb
+p
+0
85 lib/cocaine/command_line.rb
@@ -0,0 +1,85 @@
+module Cocaine
+ class CommandLine
+ class << self
+ attr_accessor :path
+ end
+
+ def initialize(binary, params = "", options = {})
+ @binary = binary.dup
+ @params = params.dup
+ @options = options.dup
+ @swallow_stderr = @options.has_key?(:swallow_stderr) ? @options.delete(:swallow_stderr) : Cocaine.options[:swallow_stderr]
+ @expected_outcodes = @options.delete(:expected_outcodes)
+ @expected_outcodes ||= [0]
+ end
+
+ def command
+ cmd = []
+ cmd << full_path(@binary)
+ cmd << interpolate(@params, @options)
+ cmd << bit_bucket if @swallow_stderr
+ cmd.join(" ")
+ end
+
+ def run
+ begin
+ output = self.class.send(:'`', command)
+ rescue Errno::ENOENT
+ raise Cocaine::CommandNotFoundError
+ end
+ if $?.exitstatus == 127
+ raise Cocaine::CommandNotFoundError
+ end
+ unless @expected_outcodes.include?($?.exitstatus)
+ raise Cocaine::ExitStatusError, "Command '#{command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}"
+ end
+ output
+ end
+
+ private
+
+ def full_path(binary)
+ [self.class.path, binary].compact.join("/")
+ end
+
+ def interpolate(pattern, vars)
+ # interpolates :variables and :{variables}
+ pattern.gsub(%r#:(?:\w+|\{\w+\})#) do |match|
+ key = match[1..-1]
+ key = key[1..-2] if key[0,1] == '{'
+ if invalid_variables.include?(key)
+ raise InterpolationError,
+ "Interpolation of #{key} isn't allowed."
+ end
+ interpolation(vars, key) || match
+ end
+ end
+
+ def invalid_variables
+ %w(expected_outcodes swallow_stderr)
+ end
+
+ def interpolation(vars, key)
+ if vars.key?(key.to_sym)
+ shell_quote(vars[key.to_sym])
+ end
+ end
+
+ def shell_quote(string)
+ return "" if string.nil? or string.empty?
+ if self.class.unix?
+ string.split("'").map{|m| "'#{m}'" }.join("\\'")
+ else
+ %{"#{string}"}
+ end
+ end
+
+ def bit_bucket
+ self.class.unix? ? "2>/dev/null" : "2>NUL"
+ end
+
+ def self.unix?
+ File.exist?("/dev/null")
+ end
+ end
+end
2,093 lib/cocaine/command_line.rbc
@@ -0,0 +1,2093 @@
+!RBIX
+17831730954501249321
+x
+M
+1
+n
+n
+x
+10
+__script__
+i
+28
+99
+7
+0
+65
+49
+1
+2
+13
+99
+12
+7
+2
+12
+7
+3
+12
+65
+12
+49
+4
+4
+15
+49
+2
+0
+15
+2
+11
+I
+6
+I
+0
+I
+0
+I
+0
+n
+p
+5
+x
+7
+Cocaine
+x
+11
+open_module
+x
+15
+__module_init__
+M
+1
+n
+n
+x
+7
+Cocaine
+i
+29
+5
+66
+99
+7
+0
+1
+65
+49
+1
+3
+13
+99
+12
+7
+2
+12
+7
+3
+12
+65
+12
+49
+4
+4
+15
+49
+2
+0
+11
+I
+6
+I
+0
+I
+0
+I
+0
+n
+p
+5
+x
+11
+CommandLine
+x
+10
+open_class
+x
+14
+__class_init__
+M
+1
+n
+n
+x
+11
+CommandLine
+i
+171
+5
+66
+5
+99
+43
+0
+12
+49
+1
+1
+13
+99
+12
+7
+2
+12
+7
+3
+12
+65
+12
+49
+4
+4
+15
+54
+50
+2
+0
+15
+99
+7
+5
+7
+6
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+9
+7
+10
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+11
+7
+12
+65
+67
+49
+7
+0
+49
+8
+4
+15
+5
+48
+13
+15
+99
+7
+14
+7
+15
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+16
+7
+17
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+18
+7
+19
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+20
+7
+21
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+22
+7
+23
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+24
+7
+25
+65
+67
+49
+7
+0
+49
+8
+4
+15
+99
+7
+26
+7
+27
+65
+5
+49
+4
+4
+11
+I
+6
+I
+0
+I
+0
+I
+0
+n
+p
+28
+x
+4
+Type
+x
+22
+object_singleton_class
+x
+18
+__metaclass_init__
+M
+1
+n
+n
+x
+18
+__metaclass_init__
+i
+10
+5
+66
+5
+7
+0
+47
+49
+1
+1
+11
+I
+2
+I
+0
+I
+0
+I
+0
+n
+p
+2
+x
+4
+path
+x
+13
+attr_accessor
+p
+3
+I
+2
+I
+4
+I
+a
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+x
+13
+attach_method
+x
+10
+initialize
+M
+1
+n
+n
+x
+10
+initialize
+i
+102
+23
+1
+10
+10
+7
+0
+64
+19
+1
+15
+23
+2
+10
+24
+44
+43
+1
+78
+49
+2
+1
+19
+2
+15
+20
+0
+49
+3
+0
+38
+4
+15
+20
+1
+49
+3
+0
+38
+5
+15
+20
+2
+49
+3
+0
+38
+6
+15
+39
+6
+7
+7
+49
+8
+1
+9
+66
+39
+6
+7
+7
+49
+9
+1
+8
+77
+45
+10
+11
+49
+12
+0
+7
+7
+49
+13
+1
+38
+14
+15
+39
+6
+7
+15
+49
+9
+1
+38
+16
+15
+39
+16
+13
+10
+101
+15
+78
+35
+1
+38
+16
+11
+I
+5
+I
+3
+I
+1
+I
+3
+n
+p
+17
+s
+0
+
+x
+4
+Hash
+x
+16
+new_from_literal
+x
+3
+dup
+x
+7
+@binary
+x
+7
+@params
+x
+8
+@options
+x
+14
+swallow_stderr
+x
+8
+has_key?
+x
+6
+delete
+x
+7
+Cocaine
+n
+x
+7
+options
+x
+2
+[]
+x
+15
+@swallow_stderr
+x
+17
+expected_outcodes
+x
+18
+@expected_outcodes
+p
+17
+I
+-1
+I
+7
+I
+18
+I
+8
+I
+20
+I
+9
+I
+28
+I
+a
+I
+30
+I
+b
+I
+4d
+I
+b
+I
+50
+I
+c
+I
+5a
+I
+d
+I
+66
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+3
+x
+6
+binary
+x
+6
+params
+x
+7
+options
+x
+17
+method_visibility
+x
+15
+add_defn_method
+x
+7
+command
+M
+1
+n
+n
+x
+7
+command
+i
+58
+35
+0
+19
+0
+15
+20
+0
+5
+39
+0
+47
+49
+1
+1
+49
+2
+1
+15
+20
+0
+5
+39
+3
+39
+4
+47
+49
+5
+2
+49
+2
+1
+15
+39
+6
+9
+47
+20
+0
+5
+48
+7
+49
+2
+1
+8
+48
+1
+15
+20
+0
+7
+8
+64
+49
+9
+1
+11
+I
+5
+I
+1
+I
+0
+I
+0
+n
+p
+10
+x
+7
+@binary
+x
+9
+full_path
+x
+2
+<<
+x
+7
+@params
+x
+8
+@options
+x
+11
+interpolate
+x
+15
+@swallow_stderr
+x
+10
+bit_bucket
+s
+1
+
+x
+4
+join
+p
+15
+I
+-1
+I
+10
+I
+0
+I
+11
+I
+5
+I
+12
+I
+12
+I
+13
+I
+21
+I
+14
+I
+30
+I
+0
+I
+31
+I
+15
+I
+3a
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+1
+x
+3
+cmd
+x
+3
+run
+M
+1
+n
+n
+x
+3
+run
+i
+171
+26
+93
+0
+15
+29
+24
+0
+5
+49
+0
+0
+7
+1
+5
+48
+2
+49
+3
+2
+19
+0
+30
+8
+62
+26
+93
+1
+15
+24
+13
+45
+4
+5
+43
+6
+12
+49
+7
+1
+10
+43
+8
+57
+15
+5
+45
+8
+9
+43
+10
+47
+49
+11
+1
+25
+8
+62
+15
+92
+1
+27
+34
+92
+0
+27
+15
+99
+43
+12
+7
+13
+49
+14
+1
+49
+15
+0
+4
+127
+83
+16
+9
+95
+5
+45
+8
+17
+43
+10
+47
+49
+11
+1
+8
+96
+1
+15
+39
+18
+99
+43
+12
+7
+13
+49
+14
+1
+49
+15
+0
+49
+19
+1
+9
+118
+1
+8
+167
+5
+45
+8
+20
+43
+21
+7
+22
+5
+48
+2
+47
+101
+23
+7
+24
+99
+43
+12
+7
+13
+49
+14
+1
+49
+15
+0
+47
+101
+23
+7
+25
+39
+18
+7
+26
+64
+49
+27
+1
+47
+101
+23
+63
+6
+47
+49
+11
+2
+15
+20
+0
+11
+I
+c
+I
+1
+I
+0
+I
+0
+n
+p
+28
+x
+5
+class
+x
+1
+`
+x
+7
+command
+x
+4
+send
+x
+5
+Errno
+n
+x
+6
+ENOENT
+x
+3
+===
+x
+7
+Cocaine
+n
+x
+20
+CommandNotFoundError
+x
+5
+raise
+x
+7
+Globals
+x
+2
+$?
+x
+2
+[]
+x
+10
+exitstatus
+x
+2
+==
+n
+x
+18
+@expected_outcodes
+x
+8
+include?
+n
+x
+15
+ExitStatusError
+s
+9
+Command '
+x
+4
+to_s
+s
+11
+' returned
+s
+11
+. Expected
+s
+2
+,
+x
+4
+join
+p
+29
+I
+-1
+I
+18
+I
+0
+I
+1a
+I
+18
+I
+0
+I
+1d
+I
+1b
+I
+2c
+I
+1c
+I
+3e
+I
+0
+I
+42
+I
+1e
+I
+53
+I
+1f
+I
+5f
+I
+1e
+I
+60
+I
+0
+I
+61
+I
+21
+I
+76
+I
+22
+I
+a7
+I
+0
+I
+a8
+I
+24
+I
+ab
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+1
+x
+6
+output
+x
+7
+private
+x
+9
+full_path
+M
+1
+n
+n
+x
+9
+full_path
+i
+21
+5
+49
+0
+0
+49
+1
+0
+20
+0
+35
+2
+49
+2
+0
+7
+3
+64
+49
+4
+1
+11
+I
+3
+I
+1
+I
+1
+I
+1
+n
+p
+5
+x
+5
+class
+x
+4
+path
+x
+7
+compact
+s
+1
+/
+x
+4
+join
+p
+5
+I
+-1
+I
+29
+I
+0
+I
+2a
+I
+15
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+1
+x
+6
+binary
+x
+11
+interpolate
+M
+1
+n
+n
+x
+11
+interpolate
+i
+26
+20
+0
+7
+0
+13
+70
+9
+20
+15
+44
+43
+1
+7
+2
+78
+49
+3
+2
+6
+0
+56
+4
+50
+5
+1
+11
+I
+6
+I
+2
+I
+2
+I
+2
+n
+p
+6
+n
+x
+6
+Regexp
+s
+16
+:(?:\w+|\{\w+\})
+x
+3
+new
+M
+1
+p
+2
+x
+9
+for_block
+t
+n
+x
+11
+interpolate
+i
+104
+57
+19
+0
+15
+20
+0
+44
+43
+0
+79
+77
+49
+1
+2
+49
+2
+1
+19
+1
+15
+20
+1
+78
+79
+49
+2
+2
+7
+3
+64
+83
+4
+9
+52
+20
+1
+44
+43
+0
+79
+7
+5
+49
+1
+2
+49
+2
+1
+19
+1
+8
+53
+1
+15
+5
+48
+6
+20
+1
+49
+7
+1
+9
+85
+5
+45
+8
+9
+7
+10
+20
+1
+47
+101
+11
+7
+12
+63
+3
+47
+49
+13
+2
+8
+86
+1
+15
+5
+21
+1
+1
+20
+1
+47
+49
+14
+2
+13
+10
+103
+15
+20
+0
+11
+I
+8
+I
+2
+I
+1
+I
+1
+n
+p
+15
+x
+5
+Range
+x
+3
+new
+x
+2
+[]
+s
+1
+{
+x
+2
+==
+I
+-2
+x
+17
+invalid_variables
+x
+8
+include?
+x
+18
+InterpolationError
+n
+s
+17
+Interpolation of
+x
+4
+to_s
+s
+15
+ isn't allowed.
+x
+5
+raise
+x
+13
+interpolation
+p
+23
+I
+0
+I
+2f
+I
+4
+I
+30
+I
+14
+I
+31
+I
+35
+I
+0
+I
+36
+I
+32
+I
+40
+I
+33
+I
+44
+I
+34
+I
+4f
+I
+33
+I
+55
+I
+32
+I
+56
+I
+0
+I
+57
+I
+36
+I
+68
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+2
+x
+5
+match
+x
+3
+key
+x
+4
+gsub
+p
+5
+I
+-1
+I
+2d
+I
+0
+I
+2f
+I
+1a
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+2
+x
+7
+pattern
+x
+4
+vars
+x
+17
+invalid_variables
+M
+1
+n
+n
+x
+17
+invalid_variables
+i
+9
+7
+0
+64
+7
+1
+64
+35
+2
+11
+I
+2
+I
+0
+I
+0
+I
+0
+n
+p
+2
+s
+17
+expected_outcodes
+s
+14
+swallow_stderr
+p
+5
+I
+-1
+I
+3a
+I
+0
+I
+3b
+I
+9
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+x
+13
+interpolation
+M
+1
+n
+n
+x
+13
+interpolation
+i
+31
+20
+0
+20
+1
+49
+0
+0
+49
+1
+1
+9
+29
+5
+20
+0
+20
+1
+49
+0
+0
+49
+2
+1
+47
+49
+3
+1
+8
+30
+1
+11
+I
+5
+I
+2
+I
+2
+I
+2
+n
+p
+4
+x
+6
+to_sym
+x
+4
+key?
+x
+2
+[]
+x
+11
+shell_quote
+p
+11
+I
+-1
+I
+3e
+I
+0
+I
+3f
+I
+c
+I
+40
+I
+1d
+I
+3f
+I
+1e
+I
+0
+I
+1f
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+2
+x
+4
+vars
+x
+3
+key
+x
+11
+shell_quote
+M
+1
+n
+n
+x
+11
+shell_quote
+i
+66
+20
+0
+49
+0
+0
+13
+10
+14
+15
+20
+0
+49
+1
+0
+9
+22
+7
+2
+64
+11
+8
+23
+1
+15
+5
+49
+3
+0
+49
+4
+0
+9
+54
+20
+0
+7
+5
+64
+49
+6
+1
+56
+7
+50
+8
+0
+7
+9
+64
+49
+10
+1
+8
+65
+7
+11
+20
+0
+47
+101
+12
+7
+11
+63
+3
+11
+I
+4
+I
+1
+I
+1
+I
+1
+n
+p
+13
+x
+4
+nil?
+x
+6
+empty?
+s
+0
+
+x
+5
+class
+x
+5
+unix?
+s
+1
+'
+x
+5
+split
+M
+1
+p
+2
+x
+9
+for_block
+t
+n
+x
+11
+shell_quote
+i
+16
+57
+19
+0
+15
+7
+0
+20
+0
+47
+101
+1
+7
+0
+63
+3
+11
+I
+5
+I
+1
+I
+1
+I
+1
+n
+p
+2
+s
+1
+'
+x
+4
+to_s
+p
+3
+I
+0
+I
+47
+I
+10
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+1
+x
+1
+m
+x
+3
+map
+s
+2
+\'
+x
+4
+join
+s
+1
+"
+x
+4
+to_s
+p
+15
+I
+-1
+I
+44
+I
+0
+I
+45
+I
+17
+I
+0
+I
+18
+I
+46
+I
+21
+I
+47
+I
+36
+I
+49
+I
+41
+I
+0
+I
+42
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+1
+x
+6
+string
+x
+10
+bit_bucket
+M
+1
+n
+n
+x
+10
+bit_bucket
+i
+18
+5
+49
+0
+0
+49
+1
+0
+9
+14
+7
+2
+64
+8
+17
+7
+3
+64
+11
+I
+1
+I
+0
+I
+0
+I
+0
+n
+p
+4
+x
+5
+class
+x
+5
+unix?
+s
+11
+2>/dev/null
+s
+5
+2>NUL
+p
+7
+I
+-1
+I
+4d
+I
+0
+I
+4e
+I
+11
+I
+0
+I
+12
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+x
+5
+unix?
+M
+1
+n
+n
+x
+5
+unix?
+i
+10
+45
+0
+1
+7
+2
+64
+49
+3
+1
+11
+I
+2
+I
+0
+I
+0
+I
+0
+n
+p
+4
+x
+4
+File
+n
+s
+9
+/dev/null
+x
+6
+exist?
+p
+5
+I
+-1
+I
+51
+I
+0
+I
+52
+I
+a
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+p
+25
+I
+2
+I
+3
+I
+1e
+I
+7
+I
+2c
+I
+10
+I
+3a
+I
+18
+I
+48
+I
+27
+I
+4c
+I
+29
+I
+5a
+I
+2d
+I
+68
+I
+3a
+I
+76
+I
+3e
+I
+84
+I
+44
+I
+92
+I
+4d
+I
+a0
+I
+51
+I
+ab
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+x
+13
+attach_method
+p
+3
+I
+2
+I
+2
+I
+1d
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
+x
+13
+attach_method
+p
+3
+I
+0
+I
+1
+I
+1c
+x
+61
+/Users/jyurek/Development/cocaine/lib/cocaine/command_line.rb
+p
+0
6 lib/cocaine/exceptions.rb
@@ -0,0 +1,6 @@
+module Cocaine
+ class CommandLineError < StandardError; end
+ class CommandNotFoundError < CommandLineError; end
+ class ExitStatusError < CommandLineError; end
+ class InterpolationError < CommandLineError; end
+end
197 lib/cocaine/exceptions.rbc
@@ -0,0 +1,197 @@
+!RBIX
+17831730954501249321
+x
+M
+1
+n
+n
+x
+10
+__script__
+i
+28
+99
+7
+0
+65
+49
+1
+2
+13
+99
+12
+7
+2
+12
+7
+3
+12
+65
+12
+49
+4
+4
+15
+49
+2
+0
+15
+2
+11
+I
+6
+I
+0
+I
+0
+I
+0
+n
+p
+5
+x
+7
+Cocaine
+x
+11
+open_module
+x
+15
+__module_init__
+M
+1
+n
+n
+x
+7
+Cocaine
+i
+54
+5
+66
+99
+7
+0
+45
+1
+2
+65
+49
+3
+3
+15
+1
+15
+99
+7
+4
+45
+0
+5
+65
+49
+3
+3
+15
+1
+15
+99
+7
+6
+45
+0
+7
+65
+49
+3
+3
+15
+1
+15
+99
+7
+8
+45
+0
+9
+65
+49
+3
+3
+15
+1
+11
+I
+4
+I
+0
+I
+0
+I
+0
+n
+p
+10
+x
+16
+CommandLineError
+x
+13
+StandardError
+n
+x
+10
+open_class
+x
+20
+CommandNotFoundError
+n
+x
+15
+ExitStatusError
+n
+x
+18
+InterpolationError
+n
+p
+9
+I
+2
+I
+2
+I
+f
+I
+3
+I
+1c
+I
+4
+I
+29
+I
+5
+I
+36
+x
+59
+/Users/jyurek/Development/cocaine/lib/cocaine/exceptions.rb
+p
+0
+x
+13
+attach_method
+p
+3
+I
+0
+I
+1
+I
+1c
+x
+59
+/Users/jyurek/Development/cocaine/lib/cocaine/exceptions.rb
+p
+0
142 spec/lib/cocaine/command_line_spec.rb
@@ -0,0 +1,142 @@
+require 'spec_helper'
+
+def with_exitstatus_returning(code)
+ saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
+ begin
+ `ruby -e 'exit #{code.to_i}'`
+ yield
+ ensure
+ `ruby -e 'exit #{saved_exitstatus.to_i}'`
+ end
+end
+
+describe Cocaine::CommandLine do
+ before do
+ Cocaine::CommandLine.path = nil
+ File.stubs(:exist?).with("/dev/null").returns(true)
+ end
+
+ it "takes a command and parameters and produce a shell command for bash" do
+ cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
+ cmd.command.should == "convert a.jpg b.png"
+ end
+
+ it "can set a default path and produce commands with that path" do
+ Cocaine::CommandLine.path = "/opt/bin"
+ cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
+ cmd.command.should == "/opt/bin/convert a.jpg b.png"
+ end
+
+ it "can interpolate quoted variables into the parameters" do
+ cmd = Cocaine::CommandLine.new("convert",
+ ":one :{two}",
+ :one => "a.jpg",
+ :two => "b.png",
+ :swallow_stderr => false)
+ cmd.command.should == "convert 'a.jpg' 'b.png'"
+ end
+
+ it "quotes command line options differently if we're on windows" do
+ File.stubs(:exist?).with("/dev/null").returns(false)
+ cmd = Cocaine::CommandLine.new("convert",
+ ":one :{two}",
+ :one => "a.jpg",
+ :two => "b.png",
+ :swallow_stderr => false)
+ cmd.command.should == 'convert "a.jpg" "b.png"'
+ end
+
+ it "can quote and interpolate dangerous variables" do
+ cmd = Cocaine::CommandLine.new("convert",
+ ":one :two",
+ :one => "`rm -rf`.jpg",
+ :two => "ha'ha.png",
+ :swallow_stderr => false)
+ cmd.command.should == "convert '`rm -rf`.jpg' 'ha'\\''ha.png'"
+ end
+
+ it "can quote and interpolate dangerous variables even on windows" do
+ File.stubs(:exist?).with("/dev/null").returns(false)
+ cmd = Cocaine::CommandLine.new("convert",
+ ":one :two",
+ :one => "`rm -rf`.jpg",
+ :two => "ha'ha.png",
+ :swallow_stderr => false)
+ cmd.command.should == %{convert "`rm -rf`.jpg" "ha'ha.png"}
+ end
+
+ it "allows colons in parameters" do
+ cmd = Cocaine::CommandLine.new("convert", "'a.jpg' xc:black 'b.jpg'", :swallow_stderr => false)
+ cmd.command.should == "convert 'a.jpg' xc:black 'b.jpg'"
+ end
+
+ it "adds redirection to get rid of stderr in bash" do
+ File.stubs(:exist?).with("/dev/null").returns(true)
+ cmd = Cocaine::CommandLine.new("convert",
+ "a.jpg b.png",
+ :swallow_stderr => true)
+
+ cmd.command.should == "convert a.jpg b.png 2>/dev/null"
+ end
+
+ it "adds redirection to get rid of stderr in cmd.exe" do
+ File.stubs(:exist?).with("/dev/null").returns(false)
+ cmd = Cocaine::CommandLine.new("convert",
+ "a.jpg b.png",
+ :swallow_stderr => true)
+
+ cmd.command.should == "convert a.jpg b.png 2>NUL"
+ end
+
+ it "raises if trying to interpolate :swallow_stderr or :expected_outcodes" do
+ cmd = Cocaine::CommandLine.new("convert",
+ ":swallow_stderr :expected_outcodes",
+ :swallow_stderr => false,
+ :expected_outcodes => [0, 1])
+
+ lambda do
+ cmd.command
+ end.should raise_error(Cocaine::CommandLineError)
+ end
+
+ it "runs the #command it's given and return the output" do
+ cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
+ cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ with_exitstatus_returning(0) do
+ cmd.run.should == :correct_value
+ end
+ end
+
+ it "raises a CommandLineError if the result code isn't expected" do
+ cmd = Cocaine::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
+ cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ with_exitstatus_returning(1) do
+ lambda do
+ cmd.run
+ end.should raise_error(Cocaine::CommandLineError)
+ end
+ end
+
+ it "does not raise a CommandLineError if the result code is expected" do
+ cmd = Cocaine::CommandLine.new("convert",
+ "a.jpg b.png",
+ :expected_outcodes => [0, 1],
+ :swallow_stderr => false)
+ cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
+ with_exitstatus_returning(1) do
+ lambda do
+ cmd.run
+ end.should_not raise_error
+ end
+ end
+
+ it "detects that the system is unix or windows based on presence of /dev/null" do
+ File.stubs(:exist?).returns(true)
+ Cocaine::CommandLine.unix?.should == true
+ end
+
+ it "detects that the system is not unix or windows based on absence of /dev/null" do
+ File.stubs(:exist?).returns(false)
+ Cocaine::CommandLine.unix?.should_not == true
+ end
+end
4,501 spec/lib/cocaine/command_line_spec.rbc
4,501 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
8 spec/spec_helper.rb
@@ -0,0 +1,8 @@
+require 'rspec'
+require 'mocha'
+require 'bourne'
+require 'cocaine'
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
184 spec/spec_helper.rbc
@@ -0,0 +1,184 @@
+!RBIX
+17831730954501249321
+x
+M
+1
+n
+n
+x
+10
+__script__
+i
+47
+5
+7
+0
+64
+47
+49
+1
+1
+15
+5
+7
+2
+64
+47
+49
+1
+1
+15
+5
+7
+3
+64
+47
+49
+1
+1
+15
+5
+7
+4
+64
+47
+49
+1
+1
+15
+45
+5
+6
+56
+7
+50
+8
+0
+15
+2
+11
+I
+2
+I
+0
+I
+0
+I
+0
+n
+p
+9
+s
+5
+rspec
+x
+7
+require
+s
+5
+mocha
+s
+6
+bourne
+s
+7
+cocaine
+x
+5
+RSpec
+n
+M
+1
+p
+2
+x
+9
+for_block
+t
+n
+x
+9
+__block__
+i
+12
+57
+19
+0
+15
+20
+0
+7
+0
+49
+1
+1
+11
+I
+4
+I
+1
+I
+1
+I
+1
+n
+p
+2
+x
+5
+mocha
+x
+9
+mock_with
+p
+5
+I
+0
+I
+6
+I
+4
+I
+7
+I
+c
+x
+53
+/Users/jyurek/Development/cocaine/spec/spec_helper.rb
+p
+1
+x
+6
+config
+x
+9
+configure
+p
+11
+I
+0
+I
+1
+I
+9
+I
+2
+I
+12
+I
+3
+I
+1b
+I
+4
+I
+24
+I
+6
+I
+2f
+x
+53
+/Users/jyurek/Development/cocaine/spec/spec_helper.rb
+p
+0

0 comments on commit 069af0c

Please sign in to comment.