Skip to content

Commit

Permalink
Merge branch 'ruby-external-command-env'
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Jul 28, 2023
2 parents 1dac609 + c79fb6c commit 6149757
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rblib/external_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def run
Process.setrlimit(Process::RLIMIT_AS, @memory_limit, @default_memory_limit)
end

# Store current environment variables before being overridden
old_env_values = @env.keys.inject({}) { |m, k| m[k] = ENV[k]; m }

# Override the environment as specified
ENV.update @env

Expand Down Expand Up @@ -129,6 +132,9 @@ def run
Process.setrlimit(Process::RLIMIT_AS, @default_memory_limit)
end

# Reset overridden environment variables
ENV.update(old_env_values)

# if we're not expecting binary output, convert the output streams to the
# default encoding now they are written to - not before, as there might be
# partial characters there
Expand Down
8 changes: 8 additions & 0 deletions rblib/tests/external_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
f.out.should =~ /^FOO=barbie$/
end

it "should restore original environment variable" do
ENV["FOO"] = "frumious"
args = [env_cmd, { :stdin_string => "Here we are\nThis part will be ignored",
:env => { "FOO" => "barbie" } }]
ExternalCommand.new(*args).run
ENV["FOO"].should == "frumious"
end

it "should handle timeouts" do
start_time = Time.now
f = ExternalCommand.new(sleep_cmd, "30", :timeout => 2).run
Expand Down

0 comments on commit 6149757

Please sign in to comment.