Skip to content

Commit fa8f2b7

Browse files
authored
Merge pull request rspec#2632 from rspec/add-isolated-env-for-rake
Add support for running Rake task in a clean environment
2 parents 45ece4f + 4b049ee commit fa8f2b7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Enhancements:
1515
(Viktor Fonic, #2634)
1616
* Issue warning when invalid values are used for `RSpec::Configuration#fail_fast`
1717
(Viktor Fonic, #2634)
18+
* Add support for running the Rake task in a clean environment.
19+
(Jon Rowe, #2632)
1820

1921
Bug Fixes:
2022

lib/rspec/core/rake_task.rb

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

48+
if RUBY_VERSION < "1.9.0" || Support::Ruby.jruby?
49+
# Run RSpec with a clean (empty) environment is not supported
50+
def with_clean_environment=(_value)
51+
raise ArgumentError, "Running in a clean environment is not supported on Ruby versions before 1.9.0"
52+
end
53+
54+
# Run RSpec with a clean (empty) environment is not supported
55+
def with_clean_environment
56+
false
57+
end
58+
else
59+
# Run RSpec with a clean (empty) environment.
60+
attr_accessor :with_clean_environment
61+
end
62+
4863
# Use verbose output. If this is set to true, the task will print the
4964
# executed spec command to stdout. Defaults to `true`.
5065
attr_accessor :verbose
@@ -76,7 +91,12 @@ def run_task(verbose)
7691
command = spec_command
7792
puts command if verbose
7893

79-
return if system(command)
94+
if with_clean_environment
95+
return if system({}, command, :unsetenv_others => true)
96+
else
97+
return if system(command)
98+
end
99+
80100
puts failure_message if failure_message
81101

82102
return unless fail_on_error

spec/rspec/core/rake_task_spec.rb

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

151+
context "with_clean_environment is set" do
152+
it "removes the environment variables", :if => RUBY_VERSION >= '1.9.0', :unless => RSpec::Support::Ruby.jruby? do
153+
with_env_vars 'MY_ENV' => 'ABC' do
154+
if RSpec::Support::OS.windows?
155+
essential_shell_variables = /\["ANSICON", "ANSICON_DEF", "HOME", "TMPDIR", "USER"\]/
156+
else
157+
essential_shell_variables = /\["PWD"(?:, "SHLVL")?(?:, "_")?(?:, "__CF_USER_TEXT_ENCODING")?\]/
158+
end
159+
160+
expect {
161+
task.with_clean_environment = true
162+
task.ruby_opts = '-e "puts \"Environment: #{ENV.keys}\""'
163+
task.run_task false
164+
}.to avoid_outputting.to_stderr.and output(essential_shell_variables).to_stdout_from_any_process
165+
end
166+
end
167+
end
168+
151169
def loaded_files
152170
args = Shellwords.split(spec_command)
153171
args -= [task.class::RUBY, "-S", task.rspec_path]

0 commit comments

Comments
 (0)