I'm using foreman to start up my rails development server. It's nice that I can put all of my environment variables in the .env
file. Is there a way to do something similar for my test environment?
我正在使用工头来启动我的rails开发服务器。我可以将所有环境变量放在.env文件中,这很好。有没有办法为我的测试环境做类似的事情?
I want to set an API key that I will use with the vcr
gem, but I don't want to add the API to version control. Any suggestions besides setting the environment variable manually when I start up my tests script?
我想设置一个我将与vcr gem一起使用的API密钥,但我不想将API添加到版本控制中。除了在启动测试脚本时手动设置环境变量之外的任何建议?
3 个解决方案
#1
24
You can use the dotenv gem --- it'll work the same as foreman and load from a .env file. (and a .env.test file for your test environments)
您可以使用dotenv gem ---它将与foreman一样工作并从.env文件加载。 (以及适用于您的测试环境的.env.test文件)
https://github.com/bkeepers/dotenv
https://github.com/bkeepers/dotenv
#2
26
If you just need to set environment variables, you can either set them from command-line:
如果只需要设置环境变量,可以从命令行设置它们:
SOMETHING=123 SOMETHING_ELSE="this is a test" rake spec
Or you could define the following at the top of your Rakefile or spec_helper.rb:
或者您可以在Rakefile或spec_helper.rb的顶部定义以下内容:
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
If they don't always apply, you could use a conditional:
如果它们不总是适用,您可以使用条件:
if something_needs_to_happen?
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
end
If you want to use a Foreman .env
file, which looks like:
如果要使用Foreman .env文件,如下所示:
SOMETHING=123
SOMETHING_ELSE="this is a test"
and turn it into the following and eval it:
并将其转换为以下内容并评估它:
ENV['SOMETHING']='123'
ENV['SOMETHING_ELSE']='this is a test'
You might do:
你可能会这样做:
File.open("/path/to/.env", "r").each_line do |line|
a = line.chomp("\n").split('=',2)
a[1].gsub!(/^"|"$/, '') if ['\'','"'].include?(a[1][0])
eval "ENV['#{a[0]}']='#{a[1] || ''}'"
end
though I don't think that would work for multi-line values.
虽然我认为这不适用于多行值。
And as @JesseWolgamott noted, it looks like you could use gem 'dotenv-rails'
.
正如@JesseWolgamott所说,看起来你可以使用gem'dotenv-rails'。
#3
1
One option is to alias the rspec command to be a little more specific. Put the following line in your dotfiles (.bashrc
or .profile
or something).
一种选择是将rspec命令别名更具体一些。将以下行放在dotfiles(.bashrc或.profile或其他内容)中。
alias 'rspec'='RACK_ENV=test RAILS_ENV=test bundle exec rspec'
Another option is to put environment variables in specific .env files:
另一种选择是将环境变量放在特定的.env文件中:
# .env.test
RAILS_ENV=test
MONGODB_URI=mongodb://localhost/test
# .. etc ..
Using the dotenv
gem works or you can bring them in manually
使用dotenv gem可以使用,也可以手动将它们带入
$ export $(cat .env.test) && rspec
#1
24
You can use the dotenv gem --- it'll work the same as foreman and load from a .env file. (and a .env.test file for your test environments)
您可以使用dotenv gem ---它将与foreman一样工作并从.env文件加载。 (以及适用于您的测试环境的.env.test文件)
https://github.com/bkeepers/dotenv
https://github.com/bkeepers/dotenv
#2
26
If you just need to set environment variables, you can either set them from command-line:
如果只需要设置环境变量,可以从命令行设置它们:
SOMETHING=123 SOMETHING_ELSE="this is a test" rake spec
Or you could define the following at the top of your Rakefile or spec_helper.rb:
或者您可以在Rakefile或spec_helper.rb的顶部定义以下内容:
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
If they don't always apply, you could use a conditional:
如果它们不总是适用,您可以使用条件:
if something_needs_to_happen?
ENV['SOMETHING']=123
ENV['SOMETHING_ELSE']="this is a test"
end
If you want to use a Foreman .env
file, which looks like:
如果要使用Foreman .env文件,如下所示:
SOMETHING=123
SOMETHING_ELSE="this is a test"
and turn it into the following and eval it:
并将其转换为以下内容并评估它:
ENV['SOMETHING']='123'
ENV['SOMETHING_ELSE']='this is a test'
You might do:
你可能会这样做:
File.open("/path/to/.env", "r").each_line do |line|
a = line.chomp("\n").split('=',2)
a[1].gsub!(/^"|"$/, '') if ['\'','"'].include?(a[1][0])
eval "ENV['#{a[0]}']='#{a[1] || ''}'"
end
though I don't think that would work for multi-line values.
虽然我认为这不适用于多行值。
And as @JesseWolgamott noted, it looks like you could use gem 'dotenv-rails'
.
正如@JesseWolgamott所说,看起来你可以使用gem'dotenv-rails'。
#3
1
One option is to alias the rspec command to be a little more specific. Put the following line in your dotfiles (.bashrc
or .profile
or something).
一种选择是将rspec命令别名更具体一些。将以下行放在dotfiles(.bashrc或.profile或其他内容)中。
alias 'rspec'='RACK_ENV=test RAILS_ENV=test bundle exec rspec'
Another option is to put environment variables in specific .env files:
另一种选择是将环境变量放在特定的.env文件中:
# .env.test
RAILS_ENV=test
MONGODB_URI=mongodb://localhost/test
# .. etc ..
Using the dotenv
gem works or you can bring them in manually
使用dotenv gem可以使用,也可以手动将它们带入
$ export $(cat .env.test) && rspec