具有捆绑器的自动测试的平台特定宝石

时间:2011-04-12 11:41:02

标签: ruby-on-rails bundler autotest gemfile

在我正在开发的rails项目中,我使用此Gemfile插入了对rspec,黄瓜和自动测试的支持(部分)

gem 'rspec-rails'
gem 'cucumber-rails'
gem 'autotest-standalone'
gem 'autotest-rails-pure'
gem 'zentest-without-autotest'

然而,为了使用自动测试运行测试,我需要执行bundle exec autotest否则它会失败并显示此消息

$ autotest 
loading autotest/cucumber_rails_rspec_rspec2
Error loading Autotest style autotest/cucumber_rails_rspec_rspec2 (no such file to load -- autotest/cucumber_rails_rspec_rspec2). Aborting.

现在我正在使用Mac进行开发,我想启用autotest-growl和autotest-fsevents gem,但如果我在~/.autotest

中插入这些行
require 'autotest/growl'
require 'autotest/fsevent'

然后我需要在Gemfile中插入相应的gems,一切正常,但它会破坏我的CI服务器(在Linux上)的构建

如何在不为本地和CI环境维护不同的Gemfile的情况下解决这个问题?

编辑:

目前我在Gemfile

中解决了这些问题
if RUBY_PLATFORM.downcase.include?("darwin") # I'm on Mac
  gem 'autotest-fsevent'
  gem 'autotest-growl'
end

它既可以在本地也可以在CI服务器上运行,我不知道它是否会弄乱一些东西,目前它似乎完美无缺。

任何更干净的方式仍然是受欢迎的。

EDIT2:

我切换到群组解决方案。虽然之前的monkeypatch在开发和持续集成方面都运行良好,但如果您使用capistrano bundler任务进行部署或者使用bundle install --deployment选项(在生产中建议),它将在生产中出现错误

使用if RUBY_PLATFORM.downcase.include?("darwin")行时,您将在部署时收到此错误。

# bundle install --deployment --without development test
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.

You have deleted from the Gemfile:
* autotest-fsevent
* autotest-growl

所以我对这个问题的最终解决方案是在给定的组中包含特定于平台的gem,比如osx,然后在生产中和CI服务器上使用bundle排除它。

如果您使用capistrano进行部署,请将其放入config.rb

set :bundle_without,  [:development, :test, :osx]
# capistrano bundler task
require "bundler/capistrano"

2 个答案:

答案 0 :(得分:0)

您可以通过利用不同的Gemfile环境(测试,开发,生产)来解决这个问题。

当CI服务器是您的“生产”环境时,您的本地方框可以开发。

考虑到这一点,您可以编辑Gemfile以根据环境使用适当的gem。

编辑:对不起,我想我过快地扫描了你的帖子。 但您可以将~/.autotest添加到.gitignore,以便它不会包含在您的CI服务器上。

答案 1 :(得分:0)

您可能希望在gemfile中使用组,例如:

group :development do
  gem "autotest-growl"
  gem "autotest-fsevents"
end

并在您使用的服务器上:$ bundle install --without development