为什么这个 require 从 rake 中失败但在显式运行时成功?
帮助我理解为什么这个项目的测试在直接执行时运行,但在通过 rake 运行时不运行。通过 Rake TestTask 运行时出现错误:
** Execute test
/home/myockey/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -I"lib:test" "/home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/data_test.rb" "test/unit/station_test.rb" "test/unit/raw_test.rb" "test/unit/parser_test.rb" "test/unit/report_test.rb"
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- test/unit/../metar_test_helper.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test/unit/data_test.rb:4:in `<top (required)>'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `block in <main>'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `<main>'
rake aborted!
当我直接运行脚本时,我得到以下信息:
myockey@myockey-K61IC:~/opt/joeyates-metar-parser-cdca19f/test/unit$ ruby data_test.rb
Loaded suite data_test
Started
...................................................
Finished in 0.084939 seconds.
51 tests, 121 assertions, 0 failures, 0 errors, 0 skips
ruby 文件的顶部如下所示:
#!/usr/bin/env ruby
# encoding: utf-8
require File.dirname(__FILE__) + '/../metar_test_helper'
class TestMetarData < Test::Unit::TestCase
请注意,我尝试将 .rb 文件扩展名添加到 metar_test_helper 但无济于事。请放心,该文件的父目录中存在名为metar_test_helper.rb 的文件,并且该文件具有足够的访问权限。
添加了赏金。我知道这一定是一个简单的路径问题,但我真的很感激一些解决它并帮助我理解它的指导。
Help me understand why this project's tests run when executed directly but do not when run via rake. The error when run via a Rake TestTask:
** Execute test
/home/myockey/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -I"lib:test" "/home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/data_test.rb" "test/unit/station_test.rb" "test/unit/raw_test.rb" "test/unit/parser_test.rb" "test/unit/report_test.rb"
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- test/unit/../metar_test_helper.rb (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test/unit/data_test.rb:4:in `<top (required)>'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `block in <main>'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
from /home/myockey/.rvm/gems/ruby-1.9.2-p136/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `<main>'
rake aborted!
When I run the script directly I get the following:
myockey@myockey-K61IC:~/opt/joeyates-metar-parser-cdca19f/test/unit$ ruby data_test.rb
Loaded suite data_test
Started
...................................................
Finished in 0.084939 seconds.
51 tests, 121 assertions, 0 failures, 0 errors, 0 skips
The top of the ruby files looks like this:
#!/usr/bin/env ruby
# encoding: utf-8
require File.dirname(__FILE__) + '/../metar_test_helper'
class TestMetarData < Test::Unit::TestCase
Note that I tried adding the .rb file extension to metar_test_helper to no avail. Rest assured that a file named metar_test_helper.rb exists in the parent directory of the file and that it has sufficient permissions to be accessible.
Added a bounty. I know this must be a simple path issue, but I'd really appreciate some guidance solving it and helping me understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这两种情况下都使用 Ruby 1.9.2 吗?
1.9.2 在路径 (
$:
) 中不包含"."
,而 1.9.2 之前的版本则包含。要检查您正在使用的 Ruby 版本以及路径是什么,请
在引发异常的行之前
执行操作。如果这不能解决问题,请询问它需要什么,然后执行操作
并查看两种方法的情况。
您可能想在如何调试 Ruby 脚本?
Are you using Ruby 1.9.2 in both scenarios?
1.9.2 doesn't include
"."
in the path ($:
), whereas pre-1.9.2 versions do.To check what version of Ruby you're using, and what the path is, do
before the exception-inducing line
If that doesn't solve it, ask it what it's requiring, by doing
and see what it's like in both approaches.
You may want to see other tips on debugging in How do I debug Ruby scripts?
这只是您需要解决的路径问题。当您手动运行测试时,您位于 test/unit 目录中,该目录似乎具有该文件 1 的级别。在 rake 任务中,您位于测试目录的父级(假设为 RAILS_HOME)。
This is just a path issue that you need to resolve. When you are running the test manually you are in the test/unit directory which appears to have that file 1 level up. In the rake task you are at the parent of the test dir (assuming RAILS_HOME).