Skip to content

Commit

Permalink
Merge pull request #378 from soberstadt/fix-bundle-env
Browse files Browse the repository at this point in the history
add configuration of Bundler env, default to new behavior
  • Loading branch information
e2 committed Jun 2, 2016
2 parents 28c3337 + 07235a0 commit c941738
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ run_all: { cmd: 'custom rspec command', message: 'custom message' } # Custom opt
title: 'My project' # Display a custom title for the notification, default: 'RSpec results'
chdir: 'directory' # run rspec from within a given subdirectory (useful if project has separate specs for submodules)
results_file: 'some/path' # use the given file for storing results (instead of default relative path)
bundler_env: :original_env # Specify which Bundler env to run the cmd under, default: :original_env
# Available values:
# :clean_env - old behavior, uses Bundler environment with all bundler-related variables removed. This is deprecated in bundler 1.12.x.
# :original_env (default) - uses Bundler environment present before Bundler was activated
# :inherit - runs inside the current environment
```

### Using Launchy to view rspec results
Expand Down
3 changes: 2 additions & 1 deletion lib/guard/rspec/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Options
cmd_additional_args: nil,
launchy: nil,
notification: true,
title: "RSpec results"
title: "RSpec results",
bundler_env: :original_env
}.freeze

class << self
Expand Down
16 changes: 10 additions & 6 deletions lib/guard/rspec/rspec_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class RSpecProcess
class Failure < RuntimeError
end

attr_reader :results
attr_reader :results, :options

def initialize(command, formatter_tmp_file)
def initialize(command, formatter_tmp_file, options = {})
@command = command
@formatter_tmp_file = formatter_tmp_file
@results = nil
@options = options

@exit_code = _run
@results = _read_results
Expand All @@ -24,7 +25,7 @@ def all_green?
private

def _run
_without_bundler_env do
_with_desired_bundler_env do
exit_code = _really_run

msg = "Guard::RSpec: RSpec command %s exited with: %s"
Expand Down Expand Up @@ -65,11 +66,14 @@ def _read_results
File.delete(formatter_tmp_file) if File.exist?(formatter_tmp_file)
end

def _without_bundler_env
if defined?(::Bundler)
def _with_desired_bundler_env
desired_bundler_env = options[:bundler_env]
if !defined?(::Bundler) || desired_bundler_env == :inherit
yield
elsif desired_bundler_env == :clean_env
::Bundler.with_clean_env { yield }
else
yield
::Bundler.with_original_env { yield }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/guard/rspec/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _really_run(cmd, options)
# TODO: add option to specify the file
file = _results_file(options[:results_file], options[:chdir])

process = RSpecProcess.new(cmd, file)
process = RSpecProcess.new(cmd, file, options)
results = process.results

inspector.failed(results.failed_paths)
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/guard/rspec/rspec_process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,33 @@
end
end
end

context "with bundler_env option" do
it "runs without Bunder changes when :inherit" do
expect(Bundler).to_not receive(:with_clean_env)
expect(Bundler).to_not receive(:with_original_env)

described_class.new(cmd, file, bundler_env: :inherit)
end

it "runs on clean Bunder changes when :clean_env" do
expect(Bundler).to receive(:with_clean_env)

described_class.new(cmd, file, bundler_env: :clean_env)
end

it "runs on original Bunder changes when :original_env" do
expect(Bundler).to receive(:with_original_env)

described_class.new(cmd, file, bundler_env: :original_env)
end
end

context "without bundler_env option" do
it "runs on original Bunder" do
expect(Bundler).to receive(:with_original_env)
subject
end
end
end
end
15 changes: 8 additions & 7 deletions spec/lib/guard/rspec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
let(:results_file) { File.join(Dir.pwd, "foobar.txt") }
it "uses the given file" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
with(anything, results_file, options).and_return(process)
runner.run(paths)
end
end
Expand All @@ -227,7 +227,7 @@
let(:results_file) { "/foo/foobar.txt" }
it "uses the given path" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
with(anything, results_file, options).and_return(process)
runner.run(paths)
end
end
Expand All @@ -243,7 +243,7 @@
it "uses a path relative to chdir" do
expected = "/foo/bar/moduleA/foobar.txt"
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, expected).and_return(process)
with(anything, expected, options).and_return(process)
runner.run(paths)
end
end
Expand All @@ -252,7 +252,7 @@
let(:results_file) { "/foo/foobar.txt" }
it "uses the full given path anyway" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
with(anything, results_file, options).and_return(process)
runner.run(paths)
end
end
Expand All @@ -270,7 +270,7 @@
it "uses a path relative to chdir" do
expected = File.join(Dir.pwd, "moduleA/foobar.txt")
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, expected).and_return(process)
with(anything, expected, options).and_return(process)
runner.run(paths)
end

Expand All @@ -285,7 +285,7 @@
let(:results_file) { "/foo/foobar.txt" }
it "uses the full given path anyway" do
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, results_file).and_return(process)
with(anything, results_file, options).and_return(process)
runner.run(paths)
end
end
Expand All @@ -296,8 +296,9 @@
context "with no custom results file" do
let(:options) { { cmd: "rspec" } }
it "uses the default" do
expected_params = [anything, %r{/tmp/rspec_guard_result$}, options]
expect(Guard::RSpec::RSpecProcess).to receive(:new).
with(anything, %r{/tmp/rspec_guard_result$}).and_return(process)
with(*expected_params).and_return(process)
runner.run(paths)
end
end
Expand Down

0 comments on commit c941738

Please sign in to comment.