如何在rakefile中包含loadpath / dependency

时间:2013-03-29 15:33:01

标签: ruby path include directory rakefile

我已经安装了Ruby 1.8.7,ci_reporter 1.8.4,测试单元2.5.4,rake 10.0.3。

我的testA.rb:

require 'includeA.rb'
require 'includeB.rb'
require 'includeC.rb'
require 'includeD.rb'

Begin of the code...
... End of the code

通常我使用以下cmd执行测试:

ruby -I E:/pathToIncludeA/includeA.rb -I E:/pathToIncludeB/includeB.rb -I E:/pathToIncludeC/includeC.rb -I E:/pathToIncludeD/includeD.rb E:/pathToTestA.rb/testA.rb

一切正常。

但现在我正在使用rake,我正在尝试以相同的方式包含目录。

这是我的rakefile:

require 'rake/testtask'
require 'rubygems'
gem 'ci_reporter'
gem 'test-unit' 
require 'test/unit' 
require 'ci/reporter/rake/test_unit'  

task :default => [:test]

task :test do
  ruby '-I E:/pathToIncludeA/includeA.rb', '-I E:/pathToIncludeB/includeB.rb, '-I E:/pathToIncludeC/includeC.rb', '-I E:/pathToIncludeD/includeD.rb', E:/pathToTestA.rb/testA.rb
end

但是当我执行以下cmd时:

rake test

我遇到以下错误:

E:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in 'gem_original_require': no such file to load -- includeA.rb (load error)
from E:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in 'require'
from E:/pathToTestA/testA.rb/

rake aborted!
command failed with status (1): [E:/Ruby/bin/ruby.exe -I pathToIncludeA/...]
Tasks: TOP => default => test

我做错了什么?有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

task :test do
  sh "ruby -I E:/pathToIncludeA/includeA.rb -I E:/pathToIncludeB/includeB.rb -I E:/pathToIncludeC/includeC.rb -I E:/pathToIncludeD/includeD.rb E:/pathToTestA.rb/testA.rb"
end

或者,如果您需要/喜欢在任务中使用ruby方法,请更改testA.rb以将路径加载到include(A|B|C|D).rb,然后:

task :test do
  # assuming the Rakefile and the testA.rb are in the same folder.
  ruby "testA.rb"
end

如果您的测试套件包含多个测试文件,例如testA.rbtestB.rb等,那么Rake::TestTask类可能会让您更轻松。检查Sinatra项目如何使用它来获得一个想法:Sinatra/Rakefile; Rake::TestTask Documentation