Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote environment variable values #250

Merged
merged 3 commits into from
May 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ appear at the top.

* Add your entries below here, remember to credit yourself however you want
to be credited!
* Quote environment variable values.
[PR #250](https://github.com/capistrano/sshkit/pull/250)
@Sinjo - Chris Sinjakli
* Simplified formatter hierarchy.
[PR #248](https://github.com/capistrano/sshkit/pull/248)
@robd
Expand Down
8 changes: 3 additions & 5 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,9 @@ def environment_hash

def environment_string
environment_hash.collect do |key,value|
if key.is_a? Symbol
"#{key.to_s.upcase}=#{value}"
else
"#{key.to_s}=#{value}"
end
key_string = key.is_a?(Symbol) ? key.to_s.upcase : key.to_s
escaped_value = value.to_s.gsub(/"/, '\"')
%{#{key_string}="#{escaped_value}"}
end.join(' ')
end

Expand Down
22 changes: 14 additions & 8 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,44 @@ def test_using_a_heredoc
def test_including_the_env
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {rails_env: :production})
assert_equal "( RAILS_ENV=production /usr/bin/env rails server )", c.to_command
assert_equal %{( RAILS_ENV="production" /usr/bin/env rails server )}, c.to_command
end

def test_including_the_env_with_multiple_keys
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {rails_env: :production, foo: 'bar'})
assert_equal "( RAILS_ENV=production FOO=bar /usr/bin/env rails server )", c.to_command
assert_equal %{( RAILS_ENV="production" FOO="bar" /usr/bin/env rails server )}, c.to_command
end

def test_including_the_env_with_string_keys
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {'FACTER_env' => :production, foo: 'bar'})
assert_equal "( FACTER_env=production FOO=bar /usr/bin/env rails server )", c.to_command
assert_equal %{( FACTER_env="production" FOO="bar" /usr/bin/env rails server )}, c.to_command
end

def test_double_quotes_are_escaped_in_env
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {foo: 'asdf"hjkl'})
assert_equal %{( FOO="asdf\\\"hjkl" /usr/bin/env rails server )}, c.to_command
end

def test_including_the_env_doesnt_addressively_escape
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {path: '/example:$PATH'})
assert_equal "( PATH=/example:$PATH /usr/bin/env rails server )", c.to_command
assert_equal %{( PATH="/example:$PATH" /usr/bin/env rails server )}, c.to_command
end

def test_global_env
SSHKit.config = nil
SSHKit.config.default_env = { default: 'env' }
c = Command.new(:rails, 'server', env: {})
assert_equal "( DEFAULT=env /usr/bin/env rails server )", c.to_command
assert_equal %{( DEFAULT="env" /usr/bin/env rails server )}, c.to_command
end

def test_default_env_is_overwritten_with_locally_defined
SSHKit.config.default_env = { foo: 'bar', over: 'under' }
c = Command.new(:rails, 'server', env: { over: 'write'})
assert_equal "( FOO=bar OVER=write /usr/bin/env rails server )", c.to_command
assert_equal %{( FOO="bar" OVER="write" /usr/bin/env rails server )}, c.to_command
end

def test_working_in_a_given_directory
Expand All @@ -69,7 +75,7 @@ def test_working_in_a_given_directory

def test_working_in_a_given_directory_with_env
c = Command.new(:ls, '-l', in: "/opt/sites", env: {a: :b})
assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l )", c.to_command
assert_equal %{cd /opt/sites && ( A="b" /usr/bin/env ls -l )}, c.to_command
end

def test_having_a_host_passed
Expand Down Expand Up @@ -114,7 +120,7 @@ def test_umask_with_working_directory_and_user
def test_umask_with_env_and_working_directory_and_user
SSHKit.config.umask = '007'
c = Command.new(:touch, 'somefile', user: 'bob', env: {a: 'b'}, in: '/var')
assert_equal "cd /var && umask 007 && ( A=b sudo -u bob A=b -- sh -c '/usr/bin/env touch somefile' )", c.to_command
assert_equal %{cd /var && umask 007 && ( A="b" sudo -u bob A="b" -- sh -c '/usr/bin/env touch somefile' )}, c.to_command
end

def test_verbosity_defaults_to_logger_info
Expand Down