Why does this require fail from rake but succeed w

2019-07-13 03:11发布

问题:

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.

回答1:

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?



回答2:

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).



标签: ruby rake