@@ -9,6 +9,11 @@ def test_maps_a_command
99 assert_equal '/usr/bin/env example' , c . to_command
1010 end
1111
12+ def test_maps_a_command_with_arguments
13+ c = Command . new ( 'example' , 'hello world' )
14+ assert_equal '/usr/bin/env example hello\\ world' , c . to_command
15+ end
16+
1217 def test_not_mapping_a_builtin
1318 %w{ if test time } . each do |builtin |
1419 c = Command . new ( builtin )
@@ -33,60 +38,65 @@ def test_leading_and_trailing_space_is_stripped
3338 def test_including_the_env
3439 SSHKit . config = nil
3540 c = Command . new ( :rails , 'server' , env : { rails_env : :production } )
36- assert_equal %{( export RAILS_ENV=" production" ; /usr/bin/env rails server )} , c . to_command
41+ assert_equal %{( export RAILS_ENV=production ; /usr/bin/env rails server )} , c . to_command
3742 end
3843
3944 def test_including_the_env_with_multiple_keys
4045 SSHKit . config = nil
4146 c = Command . new ( :rails , 'server' , env : { rails_env : :production , foo : 'bar' } )
42- assert_equal %{( export RAILS_ENV=" production" FOO=" bar" ; /usr/bin/env rails server )} , c . to_command
47+ assert_equal %{( export RAILS_ENV=production FOO=bar ; /usr/bin/env rails server )} , c . to_command
4348 end
4449
4550 def test_including_the_env_with_string_keys
4651 SSHKit . config = nil
4752 c = Command . new ( :rails , 'server' , env : { 'FACTER_env' => :production , foo : 'bar' } )
48- assert_equal %{( export FACTER_env=" production" FOO=" bar" ; /usr/bin/env rails server )} , c . to_command
53+ assert_equal %{( export FACTER_env=production FOO=bar ; /usr/bin/env rails server )} , c . to_command
4954 end
5055
5156 def test_double_quotes_are_escaped_in_env
5257 SSHKit . config = nil
5358 c = Command . new ( :rails , 'server' , env : { foo : 'asdf"hjkl' } )
54- assert_equal %{( export FOO=" asdf\\ \" hjkl" ; /usr/bin/env rails server )} , c . to_command
59+ assert_equal %{( export FOO=asdf\\ \" hjkl ; /usr/bin/env rails server )} , c . to_command
5560 end
5661
5762 def test_percentage_symbol_handled_in_env
5863 SSHKit . config = nil
5964 c = Command . new ( :rails , 'server' , env : { foo : 'asdf%hjkl' } , user : "anotheruser" )
60- assert_equal %{( export FOO=" asdf%hjkl" ; sudo -u anotheruser FOO=\" asdf%hjkl\" -- sh -c ' /usr/bin/env rails server' )} , c . to_command
65+ assert_equal %{( export FOO=asdf\\ %hjkl ; sudo -u anotheruser FOO=asdf\\ %hjkl -- sh -c /usr/bin/env\\ rails\\ server )} , c . to_command
6166 end
6267
6368 def test_including_the_env_doesnt_addressively_escape
6469 SSHKit . config = nil
6570 c = Command . new ( :rails , 'server' , env : { path : '/example:$PATH' } )
66- assert_equal %{( export PATH=" /example:$PATH" ; /usr/bin/env rails server )} , c . to_command
71+ assert_equal %{( export PATH=/example:\\ $PATH ; /usr/bin/env rails server )} , c . to_command
6772 end
6873
6974 def test_global_env
7075 SSHKit . config = nil
7176 SSHKit . config . default_env = { default : 'env' }
7277 c = Command . new ( :rails , 'server' , env : { } )
73- assert_equal %{( export DEFAULT=" env" ; /usr/bin/env rails server )} , c . to_command
78+ assert_equal %{( export DEFAULT=env ; /usr/bin/env rails server )} , c . to_command
7479 end
7580
7681 def test_default_env_is_overwritten_with_locally_defined
7782 SSHKit . config . default_env = { foo : 'bar' , over : 'under' }
7883 c = Command . new ( :rails , 'server' , env : { over : 'write' } )
79- assert_equal %{( export FOO=" bar" OVER=" write" ; /usr/bin/env rails server )} , c . to_command
84+ assert_equal %{( export FOO=bar OVER=write ; /usr/bin/env rails server )} , c . to_command
8085 end
8186
8287 def test_working_in_a_given_directory
8388 c = Command . new ( :ls , '-l' , in : "/opt/sites" )
8489 assert_equal "cd /opt/sites && /usr/bin/env ls -l" , c . to_command
8590 end
8691
92+ def test_working_in_a_given_weird_directory
93+ c = Command . new ( :ls , '-l' , in : "/opt/sites and stuff" )
94+ assert_equal "cd /opt/sites\\ and\\ stuff && /usr/bin/env ls -l" , c . to_command
95+ end
96+
8797 def test_working_in_a_given_directory_with_env
8898 c = Command . new ( :ls , '-l' , in : "/opt/sites" , env : { a : :b } )
89- assert_equal %{cd /opt/sites && ( export A="b" ; /usr/bin/env ls -l )} , c . to_command
99+ assert_equal %{cd /opt/sites && ( export A=b ; /usr/bin/env ls -l )} , c . to_command
90100 end
91101
92102 def test_having_a_host_passed
@@ -97,17 +107,27 @@ def test_having_a_host_passed
97107
98108 def test_working_as_a_given_user
99109 c = Command . new ( :whoami , user : :anotheruser )
100- assert_equal "sudo -u anotheruser -- sh -c '/usr/bin/env whoami'" , c . to_command
110+ assert_equal "sudo -u anotheruser -- sh -c /usr/bin/env\\ whoami" , c . to_command
111+ end
112+
113+ def test_working_as_a_given_weird_user
114+ c = Command . new ( :whoami , user : "mr space |" )
115+ assert_equal "sudo -u mr\\ space\\ \\ | -- sh -c /usr/bin/env\\ whoami" , c . to_command
101116 end
102117
103118 def test_working_as_a_given_group
104119 c = Command . new ( :whoami , group : :devvers )
105- assert_equal 'sg devvers -c "/usr/bin/env whoami"' , c . to_command
120+ assert_equal 'sg devvers -c /usr/bin/env\\ whoami' , c . to_command
121+ end
122+
123+ def test_working_as_a_given_weird_group
124+ c = Command . new ( :whoami , group : "space | group" )
125+ assert_equal "sg space\\ \\ |\\ group -c /usr/bin/env\\ whoami" , c . to_command
106126 end
107127
108128 def test_working_as_a_given_user_and_group
109129 c = Command . new ( :whoami , user : :anotheruser , group : :devvers )
110- assert_equal %Q(sudo -u anotheruser -- sh -c 'sg devvers -c " /usr/bin/env whoami"' ) , c . to_command
130+ assert_equal %Q(sudo -u anotheruser -- sh -c sg \\ devvers\\ -c\\ /usr/bin/env\\ \\ \\ whoami) , c . to_command
111131 end
112132
113133 def test_umask
@@ -125,13 +145,13 @@ def test_umask_with_working_directory
125145 def test_umask_with_working_directory_and_user
126146 SSHKit . config . umask = '007'
127147 c = Command . new ( :touch , 'somefile' , in : '/var' , user : 'alice' )
128- assert_equal "cd /var && umask 007 && sudo -u alice -- sh -c ' /usr/bin/env touch somefile' " , c . to_command
148+ assert_equal "cd /var && umask 007 && sudo -u alice -- sh -c /usr/bin/env\\ touch\\ somefile" , c . to_command
129149 end
130150
131151 def test_umask_with_env_and_working_directory_and_user
132152 SSHKit . config . umask = '007'
133153 c = Command . new ( :touch , 'somefile' , user : 'bob' , env : { a : 'b' } , in : '/var' )
134- assert_equal %{cd /var && umask 007 && ( export A="b" ; sudo -u bob A="b" -- sh -c ' /usr/bin/env touch somefile' )} , c . to_command
154+ assert_equal %{cd /var && umask 007 && ( export A=b ; sudo -u bob A=b -- sh -c /usr/bin/env\\ touch\\ somefile )} , c . to_command
135155 end
136156
137157 def test_verbosity_defaults_to_logger_info
0 commit comments