Problems loading yaml file with Rake

Hi,
I have problems loading a yaml file with Rake when testing and getting the following error;
Errno::ENOENT: No such file or directory @ rb_sysopen
But there are no problems when testing from within the test directory. The Rakefile is located at the root of the project.

Hard to say without seeing your rakefile, but my first guess would be a path problem. When you try to load the yaml file, does rake know its location? e.g. if it is in the test directory?

Rakefile content in test directory:

require ā€˜rake/testtaskā€™

task :default => :test

Rake::TestTask.new do |task|
task.pattern = ā€˜test_*.rbā€™
task.warning = true
end

No problems loading yaml file.

Moving Rakefile to root directory of project:

require ā€˜rake/testtaskā€™

task :default => :test

Rake::TestTask.new do |task|
task.pattern = ā€˜lib/language_quiz/test_*.rbā€™
task.warning = true
end

continued:

Errno::ENOENT: No such file or directory @ rb_sysopen

This does not mention the yaml file.

Where is the yaml file being loaded? and how?

Remember that the ā€˜base folderā€™ that files are searched on will change depending on where you run the Rakefile from.

All files, including the yaml file are lying in the same directory(language_quiz) for now. The yaml file is loaded via a quiz-class in a quiz.rb file through its initializer i.e @data = YAML.load(File.read(ā€œpath to yaml fileā€))

I donā€™t know if this helps;
But if I put the load path to the yaml file in the quiz.rb file relative to where the rake file is, ā€œthe base folderā€, then it finds it.

class Quiz(in quiz.rb)
.
.
.

def initialize
begin
$LOG.debug(ā€œTest loading yaml dataā€¦ā€)
@data = YAML.load(File.read(ā€œpath to yaml file relative to the rakefile in the base folderā€))
end
.
.
.

end

But then I canā€™t run the test from within the ā€œlanguage_quizā€ folder properly where I find the quiz.rb and the test_quiz.rb file

Probably your best solution is to find the directory of the file in which you are loading the file, e.g. by:

CWD = File.expand_path(File.dirname(__FILE__))

Then you can write:

YAML.load(File.read(File.join(CWD, 'yaml-file')))

You can then name the ā€˜yaml-fileā€™ relative to the file in which the call is.

1 Like

That did it! Great! Thanks! :smiley: