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

Improve clarity of environment manipulation #931

Merged
merged 1 commit into from
Jun 1, 2024
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
2 changes: 1 addition & 1 deletion lib/aruba/api/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def expand_path(file_name, dir_string = nil)
def with_environment(env = {}, &block)
aruba.environment.nest do |nested_env|
nested_env.update(env)
Aruba.platform.with_environment nested_env.to_h, &block
Aruba.platform.with_replaced_environment nested_env.to_h, &block
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion lib/aruba/platforms/local_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module Platforms
#
# Wraps logic to make enviroment local and restorable
class LocalEnvironment
def initialize(platform)
@platform = platform
end

attr_reader :platform

# Run in environment
#
# @param [Hash] env
Expand All @@ -16,7 +22,7 @@ class LocalEnvironment
# @yield
# The block of code which should with local ENV
def call(env)
old_env = Aruba.platform.environment_variables.hash_from_env
old_env = platform.environment_variables.hash_from_env

ENV.clear
ENV.update env
Expand Down
6 changes: 3 additions & 3 deletions lib/aruba/platforms/unix_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def create_fixed_size_file(*args)
ArubaFixedSizeFileCreator.new.call(*args)
end

def with_environment(env = {}, &block)
LocalEnvironment.new.call(env, &block)
def with_replaced_environment(env = {}, &block)
LocalEnvironment.new(self).call(env, &block)
end

def default_shell
Expand Down Expand Up @@ -128,7 +128,7 @@ def getwd
def chdir(dir_name, &block)
dir_name = ::File.expand_path(dir_name.to_s)

with_environment "OLDPWD" => getwd, "PWD" => dir_name do
with_replaced_environment "OLDPWD" => getwd, "PWD" => dir_name do
::Dir.chdir(dir_name, &block)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/aruba/processes/debug_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.match?(mode)
def start
@started = true
Dir.chdir @working_directory do
Aruba.platform.with_environment(environment) do
Aruba.platform.with_replaced_environment(environment) do
@exit_status = system(command, *arguments) ? 0 : 1
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/aruba/processes/in_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def start
Dir.chdir @working_directory do
before_run

Aruba.platform.with_environment environment.merge("PWD" => @working_directory) do
new_env = environment.merge("PWD" => @working_directory)
Aruba.platform.with_replaced_environment new_env do
main_class.new(@argv, @stdin, @stdout, @stderr, @kernel).execute!
end

Expand Down