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

sudo: pass env variables without /usr/bin/env #14620

Merged
merged 2 commits into from Feb 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions Library/Homebrew/system_command.rb
Expand Up @@ -121,7 +121,7 @@ def initialize(

sig { returns(T::Array[String]) }
def command
[*sudo_prefix, *env_args, executable.to_s, *expanded_args]
[*command_prefix, executable.to_s, *expanded_args]
end

private
Expand Down Expand Up @@ -154,15 +154,23 @@ def env_args

return [] if set_variables.empty?

["/usr/bin/env", *set_variables]
set_variables
end

sig { returns(T::Array[String]) }
def sudo_prefix
return [] unless sudo?

askpass_flags = ENV.key?("SUDO_ASKPASS") ? ["-A"] : []
["/usr/bin/sudo", *askpass_flags, "-E", "--"]
["/usr/bin/sudo", *askpass_flags, "-E", *env_args, "--"]
end

sig { returns(T::Array[String]) }
def env_prefix
["/usr/bin/env", *env_args]
end

sig { returns(T::Array[String]) }
def command_prefix
sudo? ? sudo_prefix : env_prefix
end

sig { returns(T::Array[String]) }
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/system_command_spec.rb
Expand Up @@ -54,8 +54,8 @@
expect(Open3)
.to receive(:popen3)
.with(
an_instance_of(Hash), ["/usr/bin/sudo", "/usr/bin/sudo"], "-E", "--",
"/usr/bin/env", "A=1", "B=2", "C=3", "env", *env_args, pgroup: nil
an_instance_of(Hash), ["/usr/bin/sudo", "/usr/bin/sudo"], "-E",
"A=1", "B=2", "C=3", "--", "env", *env_args, pgroup: nil
)
.and_wrap_original do |original_popen3, *_, &block|
original_popen3.call("true", &block)
Expand Down