Skip to content

Commit

Permalink
Merge pull request #128 from integrum/master
Browse files Browse the repository at this point in the history
Set environment variables
  • Loading branch information
thibaudgg committed Sep 29, 2012
2 parents df2ea83 + ff88053 commit 49fef65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,13 @@ guard 'rspec', :spec_paths => "test" do
# ...
end
```
If you want to set an environment variable, you can configure `:env` option with a hash:

``` ruby
guard 'rspec', :env => {'RAILS_ENV' => 'guard'} do
# ...
end
```
[Turnip](https://github.com/jnicklas/turnip) is supported (Ruby 1.9.X only), but you must enable it:
``` ruby
guard 'rspec', :turnip => true do
Expand Down
8 changes: 7 additions & 1 deletion lib/guard/rspec/runner.rb
Expand Up @@ -14,6 +14,7 @@ def initialize(options = {})
:binstubs => false,
:rvm => nil,
:cli => nil,
:env => nil,
:notification => true,
:turnip => false
}.merge(options)
Expand Down Expand Up @@ -80,6 +81,11 @@ def parsed_or_default_formatter

private

def environment_variables
return if @options[:env].nil?
"export " + @options[:env].map {|key, value| "#{key}=#{value}"}.join(' ') + ';'
end

def rspec_arguments(paths, options)
arg_parts = []
arg_parts << options[:cli]
Expand All @@ -97,6 +103,7 @@ def rspec_arguments(paths, options)

def rspec_command(paths, options)
cmd_parts = []
cmd_parts << environment_variables
cmd_parts << "rvm #{@options[:rvm].join(',')} exec" if @options[:rvm].respond_to?(:join)
cmd_parts << "bundle exec" if bundle_exec?
cmd_parts << rspec_executable
Expand Down Expand Up @@ -230,7 +237,6 @@ def deprecations_warnings
def formatter_regex
@formatter_regex ||= /(?:^|\s)(?:-f\s*|--format(?:=|\s+))([\w:]+)/
end

end
end
end
15 changes: 15 additions & 0 deletions spec/guard/rspec/runner_spec.rb
Expand Up @@ -370,6 +370,21 @@
end
end
end

describe ':env' do
context ":env => {'RAILS_ENV' => 'blue'}" do
subject { described_class.new(:env => {'RAILS_ENV' => 'blue'}) }

it 'sets the Rails environment' do
subject.should_receive(:system).with(
"export RAILS_ENV=blue; bundle exec rspec -f progress -r #{@lib_path.join('guard/rspec/formatters/notification_rspec.rb')} " <<
'-f Guard::RSpec::Formatter::NotificationRSpec --out /dev/null --failure-exit-code 2 spec'
).and_return(true)

subject.run(['spec'])
end
end
end
end
end

Expand Down

0 comments on commit 49fef65

Please sign in to comment.