7

在我正在处理的 rails 项目中,我使用此 Gemfile(部分)插入了对 rspec、cucumber 和 autotest 的支持

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 中插入相应的 gem,一切正常,但它破坏了我的 CI 服务器(在 Linux 上)上的构建

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

编辑:

目前我用 Gemfile 中的这些行解决了

if RUBY_PLATFORM.downcase.include?("darwin") # I'm on Mac
  gem 'autotest-fsevent'
  gem 'autotest-growl'
end

它在本地和 CI 服务器上都可以工作,我不知道它是否会弄乱一些东西,目前它似乎可以完美地工作。

任何更清洁的方法仍然受到欢迎。

编辑2:

我切换到组解决方案。虽然之前的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"
4

2 回答 2

0

您可以利用不同的 Gemfile 环境(测试、开发、生产)来处理这个问题。

您的本地机器可以是开发,而 CI 服务器是您的“生产”环境。

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

编辑:对不起,我觉得我扫描你的帖子太快了。但是你可以添加你的~/.autotest.gitignore这样它就不会包含在你的 CI 服务器上。

于 2011-04-12T12:04:31.250 回答
0

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

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

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

于 2011-04-12T12:41:40.817 回答