用于测试 Ruby Gem 的嵌套项目
当为特定应用程序框架创建 gem 时,我将一个项目嵌套在测试目录中的 gem 中。例如,对于 Rails 特定的 gem,我将设置一个目录结构,如下所示:
Rakefile
Gemfile
attached.gemspec
lib/attached.rb
lib/...
test/Gemfile
test/app/...
test/...
为了测试,我使用 gem 'attached', path: '...'
设置嵌套项目 Gemfile 并运行 rake test
在测试目录中。是否可以向我的主 Rakefile 添加一个任务,该任务将允许我在子项目中运行测试,而无需先更改到目录?
When creating a gem for a specific application framework, I nest a project within the gem in a test directory. For example, with a Rails specific gem I'd setup a directory structure like:
Rakefile
Gemfile
attached.gemspec
lib/attached.rb
lib/...
test/Gemfile
test/app/...
test/...
To test, I setup the nested project Gemfile using gem 'attached', path: '...'
and run rake test
inside the test directory. Is it possible to add a task to my main Rakefile that will allow me to run the tests in my sub project without changing into the directory first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我总是使用 gem
enginex
来帮助我使用集成的 Rails 应用程序设置我的 gem。在根
Rakefile
中,他们写道:他们不使用嵌套的
Gemfile
,而是在test_helper.rb
中加载 Rails 项目,并且 Rails 是gem 的开发/测试依赖项。要查看 gem:
或者查看来源。这个 gem 作为新的插件生成器包含在 Rails 3.1 中。
I always use the gem
enginex
to help me setup my gems with an integrated rails application.In the root
Rakefile
they write:They do not use a nested
Gemfile
, instead they load the Rails project inside thetest_helper.rb
and rails is a development/test dependency of the gem.To check out the gem:
Or check the source. This gem is included in rails 3.1 as the new plugin generator.
最简单的方法(我认为最不容易出现错误)是执行如下任务:
另一种更复杂的方法是将所有“子任务”包含在根 Rakefile 中,为每个任务添加一个先决条件,这将更改当前目录,如下所示:
但我不确定这将如何与捆绑器一起使用。
我向您推荐的最后一件事是查看
bundle gem
命令创建的项目结构。我现在将它用于我所有的宝石,我相信利用它会让您的问题完全消失:)The simplest way (and I think the least bug prone), would be to have a task like:
The other, more complicated way would be to include all "subtasks" in the root Rakefile add a prerequisite to each one of them, that will change the current directory, like so:
I'm not sure how this will work with bundler though.
One last thing that I would recommend to you is to take a look at a project structure that
bundle gem
command creates. I use it for all my gems now, and I believe that taking advantage of it would make your problem go away entirely:)