@@ -111,6 +111,61 @@ def test_within_home
111111 assert_equal 'cd ~/foo && /usr/bin/env cat file' , backend . executed_command . to_command
112112 end
113113
114+ def test_as_properly_clears
115+ backend = ExampleBackend . new do
116+ as :root do
117+ execute :cat , 'file' , :strip => false
118+ end
119+
120+ execute :cat , 'file' , :strip => false
121+ end
122+
123+ backend . run
124+
125+ assert_equal '/usr/bin/env cat file' , backend . executed_command . to_command
126+ end
127+
128+ def test_as_root
129+ backend = ExampleBackend . new do
130+ as :root do
131+ execute :cat , 'file' , :strip => false
132+ end
133+ end
134+
135+ backend . run
136+
137+ assert_equal 'sudo -u root -- sh -c /usr/bin/env\\ cat\\ file' , backend . executed_command . to_command
138+ end
139+
140+ def test_nested_as
141+ backend = ExampleBackend . new do
142+ as :root do
143+ as :other_user do
144+ execute :cat , 'file' , :strip => false
145+ end
146+ end
147+ end
148+
149+ backend . run
150+
151+ assert_equal 'sudo -u other_user -- sh -c /usr/bin/env\\ cat\\ file' , backend . executed_command . to_command
152+ end
153+
154+ def test_nested_as_properly_clears
155+ backend = ExampleBackend . new do
156+ as :root do
157+ as :other_user do
158+ execute :cat , 'file' , :strip => false
159+ end
160+ execute :cat , 'file' , :strip => false
161+ end
162+ end
163+
164+ backend . run
165+
166+ assert_equal 'sudo -u root -- sh -c /usr/bin/env\\ cat\\ file' , backend . executed_command . to_command
167+ end
168+
114169 def test_background_logs_deprecation_warnings
115170 deprecation_out = +''
116171 SSHKit . config . deprecation_output = deprecation_out
0 commit comments