From 279d2170fc0e542df30aa1a5f0c4c80c22bef751 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sat, 1 Jun 2024 08:09:30 +0200 Subject: [PATCH] Improve clarity of environment manipulation This does two things: - Avoid having two methods #with_environment that, apart from operating at a different level, also use their arguments differently: one takes changes to the environment as an argument, the other takes a full replacement environment. - Decouple LocalEnvironment from the global platform singleton. This is neater because it makes the dependency go only one way. --- lib/aruba/api/core.rb | 2 +- lib/aruba/platforms/local_environment.rb | 8 +++++++- lib/aruba/platforms/unix_platform.rb | 6 +++--- lib/aruba/processes/debug_process.rb | 2 +- lib/aruba/processes/in_process.rb | 3 ++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/aruba/api/core.rb b/lib/aruba/api/core.rb index f16976e7..573d5129 100644 --- a/lib/aruba/api/core.rb +++ b/lib/aruba/api/core.rb @@ -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 diff --git a/lib/aruba/platforms/local_environment.rb b/lib/aruba/platforms/local_environment.rb index 76cdcd22..20b6f5ac 100644 --- a/lib/aruba/platforms/local_environment.rb +++ b/lib/aruba/platforms/local_environment.rb @@ -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 @@ -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 diff --git a/lib/aruba/platforms/unix_platform.rb b/lib/aruba/platforms/unix_platform.rb index 03709215..5fc5d6fd 100644 --- a/lib/aruba/platforms/unix_platform.rb +++ b/lib/aruba/platforms/unix_platform.rb @@ -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 @@ -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 diff --git a/lib/aruba/processes/debug_process.rb b/lib/aruba/processes/debug_process.rb index 5d28d7dd..19c0044b 100644 --- a/lib/aruba/processes/debug_process.rb +++ b/lib/aruba/processes/debug_process.rb @@ -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 diff --git a/lib/aruba/processes/in_process.rb b/lib/aruba/processes/in_process.rb index 4d63fb43..64972c1c 100644 --- a/lib/aruba/processes/in_process.rb +++ b/lib/aruba/processes/in_process.rb @@ -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