Skip to content

Commit

Permalink
Improve clarity of environment manipulation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvz committed Jun 1, 2024
1 parent 356c7d7 commit 279d217
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
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

0 comments on commit 279d217

Please sign in to comment.