为什么这个 require 从 rake 中失败但在显式运行时成功?

发布于 2024-10-27 23:30:38 字数 1798 浏览 1 评论 0原文

帮助我理解为什么这个项目的测试在直接执行时运行,但在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-11-03 23:30:38

您在这两种情况下都使用 Ruby 1.9.2 吗?

1.9.2 在路径 ($:) 中不包含 ".",而 1.9.2 之前的版本则包含。

要检查您正在使用的 Ruby 版本以及路径是什么,请

STDERR.puts "RUBY_VERSION is #{RUBY_VERSION}"
STDERR.puts "Path is #{$:}"

在引发异常的行之前

require File.dirname(__FILE__) + '/../metar_test_helper'

执行操作。如果这不能解决问题,请询问它需要什么,然后执行操作

STDERR.puts "The file I'm requiring is #{File.dirname(__FILE__) + '/../metar_test_helper'}"

并查看两种方法的情况。

您可能想在如何调试 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

STDERR.puts "RUBY_VERSION is #{RUBY_VERSION}"
STDERR.puts "Path is #{$:}"

before the exception-inducing line

require File.dirname(__FILE__) + '/../metar_test_helper'

If that doesn't solve it, ask it what it's requiring, by doing

STDERR.puts "The file I'm requiring is #{File.dirname(__FILE__) + '/../metar_test_helper'}"

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?

萌辣 2024-11-03 23:30:38

这只是您需要解决的路径问题。当您手动运行测试时,您位于 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文