Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 0e3e8ed

Browse files
committed
Add support for running Rake task in a clean environment
1 parent d04d4de commit 0e3e8ed

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Enhancements:
1111
outside of examples. (@obromios, #2601)
1212
* Add a minimalist quick fix style formatter, only outputs failures as
1313
`file:line:message`. (Romain Tartière, #2614)
14+
* Add support for running the Rake task in a clean environment.
15+
(Jon Rowe, #2632)
1416

1517
Bug Fixes:
1618

lib/rspec/core/rake_task.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class RakeTask < ::Rake::TaskLib
4545
# A message to print to stderr when there are failures.
4646
attr_accessor :failure_message
4747

48+
# Run RSpec with a clean (empty) environment.
49+
attr_accessor :with_clean_environment
50+
4851
# Use verbose output. If this is set to true, the task will print the
4952
# executed spec command to stdout. Defaults to `true`.
5053
attr_accessor :verbose
@@ -76,7 +79,12 @@ def run_task(verbose)
7679
command = spec_command
7780
puts command if verbose
7881

79-
return if system(command)
82+
if with_clean_environment
83+
return if system({}, command, :unsetenv_others => true)
84+
else
85+
return if system(command)
86+
end
87+
8088
puts failure_message if failure_message
8189

8290
return unless fail_on_error

spec/rspec/core/rake_task_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ def silence_output(&block)
148148
end
149149
end
150150

151+
context "with_clean_environment is set" do
152+
it "removes the environment variables" do
153+
with_env_vars 'MY_ENV' => 'ABC' do
154+
expect {
155+
task.with_clean_environment = true
156+
task.ruby_opts = '-e "puts \"Environment: #{ENV.keys}\""'
157+
task.run_task false
158+
}.to avoid_outputting.to_stderr.and output(
159+
/\["PWD"(?:, "SHLVL")?(?:, "_")?(?:, "__CF_USER_TEXT_ENCODING")?\]/
160+
).to_stdout_from_any_process
161+
end
162+
end
163+
end
164+
151165
def loaded_files
152166
args = Shellwords.split(spec_command)
153167
args -= [task.class::RUBY, "-S", task.rspec_path]

0 commit comments

Comments
 (0)